Speed up ndarray.flat()

Currently, ndarray.flat() is quite slow on multi-dimensional arrays (see issue #815). This PR specializes flat() for C-contiguous arrays, with a much faster implementation (2x faster on 2d arrays, more on 3d arrays, etc.). It also speeds up the generic implementation.

This PR is based on PR #816.