Created
February 20, 2015 17:02
-
-
Save drmohundro/ad39a95d917fc74c51f5 to your computer and use it in GitHub Desktop.
SWXMLHash Lazy-Loading Approach
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
class IndexOp { | |
let index: Int | |
let key: String | |
init(_ index: Int) { | |
self.index = index | |
self.key = "" | |
} | |
init(_ key: String) { | |
self.key = key | |
self.index = -1 | |
} | |
func toString() -> String { | |
if key == "" { | |
return index.description | |
} | |
return key | |
} | |
} | |
class IndexOps { | |
var ops: [IndexOp] = [] | |
func stringify() -> String { | |
var s = "" | |
for op in ops { | |
s += "[" + op.toString() + "]" | |
} | |
return s | |
} | |
} | |
enum XMLIndexer { | |
case Stream(IndexOps) | |
var element: String { | |
get { | |
switch self { | |
case .Stream(let ops): | |
return ops.stringify() | |
} | |
} | |
} | |
init(_ rawObject: AnyObject) { | |
self = .Stream(IndexOps()) | |
} | |
subscript(key: String) -> XMLIndexer { | |
get { | |
switch self { | |
case .Stream(let elem): | |
let op = IndexOp(key) | |
elem.ops.append(op) | |
return .Stream(elem) | |
} | |
} | |
} | |
subscript(index: Int) -> XMLIndexer { | |
get { | |
switch self { | |
case .Stream(let elem): | |
let op = IndexOp(index) | |
elem.ops.append(op) | |
return .Stream(elem) | |
} | |
} | |
} | |
} | |
let f = XMLIndexer("test") | |
// outputs "[test][1][test2]" | |
f["test"][1]["test2"].element |
Related to drmohundro/SWXMLHash#11.
To be honest, I do not yet see exactly how this is supposed to work. Then again, I am a mechanical engineer, not a computer scientist 😀
It for sure is a novel approach for me!
So, you parse the xml and only build these 'stringified' paths right? And only when something is requested, you get the actual information from the xml file.
Or am I completely wrong here...?
If this is indeed the case, then I guess a trie structure would be the right choice to store the path components?
Meh I guess I have studied the wrong thing ;)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My current thinking at the moment is that, when
element
is called, it would use theIndexOps
list to then parse the XML. It will also need to supportwithAttr
for attribute support.I'd imagine that calling
children
orall
would result in parsing down to that level (just as callingelement
would), but that might be solved by just lettingelement
do the parsing andchildren
/all
would delegate to children.