Advertisement
Guest User

Untitled

a guest
May 27th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. //implementation of state object
  2. //the state acts with a backbone of an unordered set
  3. //it references values via a stringifyed name of themselves, and allows them
  4. //to be retrieved in O(1) time
  5.  
  6. var State = (function(){
  7. function State(){
  8.  
  9. }
  10. //stores the element via string key of itself
  11. State.prototype.insert = function(elem){
  12. this[JSON.stringify(elem)] = elem;
  13. };
  14. //checks if element is in the state
  15. State.prototype.check = function(elem){
  16. return JSON.stringify(elem) in this;
  17. };
  18. State.prototype.del = function(elem){
  19. delete this[JSON.stringify(elem)];
  20. };
  21. return State;
  22. })();
  23.  
  24. /* var a = new State();
  25. a
  26. => {}
  27. a.insert({foo:5})
  28. a
  29. => { '{"foo":5}': { foo: 5 } }
  30. a.check({foo:5})
  31. => true
  32. a.insert({foo:true})
  33. a.insert({foo:true})
  34. a
  35. => { '{"foo":5}': { foo: 5 }, '{"foo":true}': { foo: true } }*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement