Skip to content

Instantly share code, notes, and snippets.

@mwrites
Last active March 6, 2018 15:09
Show Gist options
  • Save mwrites/341da3c13f910d874038805f63dfa58f to your computer and use it in GitHub Desktop.
Save mwrites/341da3c13f910d874038805f63dfa58f to your computer and use it in GitHub Desktop.
Inverted Index for Array
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