Created
July 22, 2015 13:30
-
-
Save Cengizism/0a69bb608a1d41202389 to your computer and use it in GitHub Desktop.
A helper, internal data structure that acts as a map but also allows getting / removing * elements in the LIFO order
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
/** | |
* A helper, internal data structure that acts as a map but also allows getting / removing | |
* elements in the LIFO order | |
*/ | |
.factory('$$stackedMap', function () { | |
return { | |
createNew: function () { | |
var stack = []; | |
return { | |
add: function (key, value) { | |
stack.push({ | |
key: key, | |
value: value | |
}); | |
}, | |
get: function (key) { | |
for (var i = 0; i < stack.length; i++) { | |
if (key == stack[i].key) { | |
return stack[i]; | |
} | |
} | |
}, | |
keys: function() { | |
var keys = []; | |
for (var i = 0; i < stack.length; i++) { | |
keys.push(stack[i].key); | |
} | |
return keys; | |
}, | |
top: function () { | |
return stack[stack.length - 1]; | |
}, | |
remove: function (key) { | |
var idx = -1; | |
for (var i = 0; i < stack.length; i++) { | |
if (key == stack[i].key) { | |
idx = i; | |
break; | |
} | |
} | |
return stack.splice(idx, 1)[0]; | |
}, | |
removeTop: function () { | |
return stack.splice(stack.length - 1, 1)[0]; | |
}, | |
length: function () { | |
return stack.length; | |
} | |
}; | |
} | |
}; | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment