Last active
March 6, 2018 15:09
-
-
Save mwrites/341da3c13f910d874038805f63dfa58f to your computer and use it in GitHub Desktop.
Inverted Index for Array
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
extension Array where Element : Comparable { | |
var invertedIndexes: [(index: Index, value: Element)] { | |
var res = [(index: Index, value: Element)]() | |
for (k, v) in enumerated() { | |
res.append((index: k, value: v)) | |
} | |
return res | |
} | |
} | |
// Usage | |
let invertedIndexes = A.invertedIndexes.sorted { $0.value < $1.value } | |
// Prints | |
// [(index: 3, value: 1), (index: 2, value: 2), (index: 1, value: 3), (index: 0, value: 4)] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment