Last active
June 10, 2016 01:23
-
-
Save austinzheng/cbb990c99633efc63896abf0ab39bfc5 to your computer and use it in GitHub Desktop.
This file contains 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
/* | |
This is free and unencumbered software released into the public domain. | |
Anyone is free to copy, modify, publish, use, compile, sell, or | |
distribute this software, either in source code form or as a compiled | |
binary, for any purpose, commercial or non-commercial, and by any | |
means. | |
In jurisdictions that recognize copyright laws, the author or authors | |
of this software dedicate any and all copyright interest in the | |
software to the public domain. We make this dedication for the benefit | |
of the public at large and to the detriment of our heirs and | |
successors. We intend this dedication to be an overt act of | |
relinquishment in perpetuity of all present and future rights to this | |
software under copyright law. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR | |
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | |
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | |
OTHER DEALINGS IN THE SOFTWARE. | |
For more information, please refer to <http://unlicense.org/> | |
*/ | |
class StringTupleCollection : CollectionType { | |
let backing : [String : [String]] | |
lazy private(set) var startIndex : StringTupleIndex = { | |
StringTupleIndex(self.backing) | |
}() | |
lazy private(set) var endIndex : StringTupleIndex = { | |
StringTupleIndex.endIndexFor(dict: self.backing) | |
}() | |
subscript(i: StringTupleIndex) -> (String, String) { | |
let array = backing[i.dictIndex] | |
return (array.0, array.1[i.arrayIndex]) | |
} | |
init(_ backing: [String : [String]]) { | |
self.backing = backing | |
} | |
} | |
struct StringTupleIndex : ForwardIndexType, Comparable { | |
let backing : [String : [String]] | |
let dictIndex : Dictionary<String, [String]>.Index | |
let arrayIndex : Array<String>.Index | |
init(_ backing: [String : [String]], | |
dictIndex: Dictionary<String, [String]>.Index, | |
arrayIndex: Array<String>.Index) { | |
self.backing = backing | |
self.dictIndex = dictIndex | |
self.arrayIndex = arrayIndex | |
} | |
init(_ backing: [String : [String]]) { | |
self.backing = backing | |
var idx = backing.startIndex | |
while idx != backing.endIndex { | |
let array = backing[idx].1 | |
if !array.isEmpty { | |
dictIndex = idx | |
arrayIndex = 0 | |
return | |
} else { | |
idx = idx.successor() | |
} | |
} | |
dictIndex = backing.startIndex | |
arrayIndex = 0 | |
} | |
static func endIndexFor(dict backing: [String : [String]]) -> StringTupleIndex { | |
var idx = backing.startIndex | |
var prevValidIdx : DictionaryIndex<String, [String]>? | |
// TODO: make this not awful | |
while idx != backing.endIndex { | |
if backing[idx].1.count > 0 { | |
prevValidIdx = idx | |
} | |
idx = idx.successor() | |
} | |
if let prevValidIdx = prevValidIdx { | |
return StringTupleIndex(backing, dictIndex: prevValidIdx, arrayIndex: backing[prevValidIdx].1.count) | |
} else { | |
return StringTupleIndex(backing) | |
} | |
} | |
func successor() -> StringTupleIndex { | |
let array = backing[dictIndex].1 | |
if arrayIndex < array.count - 1 { | |
// Same array | |
return StringTupleIndex(backing, dictIndex: dictIndex, arrayIndex: arrayIndex + 1) | |
} else { | |
// Next dict | |
var nextIndex = dictIndex.successor() | |
while nextIndex != backing.endIndex { | |
// Does this key have an array? | |
let thisArray = backing[nextIndex].1 | |
if !thisArray.isEmpty { | |
return StringTupleIndex(backing, dictIndex: nextIndex, arrayIndex: 0) | |
} | |
nextIndex = nextIndex.successor() | |
} | |
return StringTupleIndex.endIndexFor(dict: backing) | |
} | |
} | |
} | |
func ==(lhs: StringTupleIndex, rhs: StringTupleIndex) -> Bool { | |
return lhs.dictIndex == rhs.dictIndex && lhs.arrayIndex == rhs.arrayIndex | |
} | |
func <(lhs: StringTupleIndex, rhs: StringTupleIndex) -> Bool { | |
if lhs.dictIndex == rhs.dictIndex { | |
return lhs.arrayIndex < rhs.arrayIndex | |
} | |
return lhs.dictIndex < rhs.dictIndex | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment