Created
December 19, 2014 02:30
-
-
Save ccorcos/ea10ef07d25107cd5e79 to your computer and use it in GitHub Desktop.
breadth first search find coffeescript
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
match = (obj, properties) -> | |
for k,v of properties | |
if not k of obj | |
return false | |
else if v instanceof Object | |
if not match obj[k], v | |
return false | |
else if obj[k] isnt v | |
return false | |
return true | |
bfsFind = (collection, edgeKey, properties) -> | |
queue = collection | |
while queue.length > 0 | |
obj = queue.shift() | |
if match obj, properties | |
return obj | |
else if edgeKey of obj | |
for child in obj[edgeKey] | |
collection.push(child) | |
return undefined | |
bfsFindAll = (collection, edgeKey, properties) -> | |
found = [] | |
queue = collection | |
while queue.length > 0 | |
obj = queue.shift() | |
if match obj, properties | |
found.push(obj) | |
else if edgeKey of obj | |
for child in obj[edgeKey] | |
collection.push(child) | |
return found |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment