Created
January 10, 2012 19:10
-
-
Save wesm/1590566 to your computer and use it in GitHub Desktop.
Stable O(max(n, k)) counting sort on n integers in range [0, ..., k - 1]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@cython.boundscheck(False) | |
@cython.wraparound(False) | |
def groupsort_indexer(ndarray[int32_t] index, Py_ssize_t ngroups): | |
cdef: | |
Py_ssize_t i, loc, label, n | |
ndarray[int32_t] counts, where, result | |
# count group sizes, location 0 for NA | |
counts = np.zeros(ngroups + 1, dtype='i4') | |
n = len(index) | |
for i from 0 <= i < n: | |
counts[index[i] + 1] += 1 | |
# mark the start of each contiguous group of like-indexed data | |
where = np.zeros(ngroups + 1, dtype='i4') | |
for i from 1 <= i < ngroups + 1: | |
where[i] = where[i - 1] + counts[i - 1] | |
# this is our indexer | |
result = np.zeros(n, dtype='i4') | |
for i from 0 <= i < n: | |
label = index[i] + 1 | |
result[where[label]] = i | |
where[label] += 1 | |
return result, counts |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment