Created
January 15, 2012 09:15
-
-
Save devongovett/1615172 to your computer and use it in GitHub Desktop.
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
class Set | |
constructor: -> | |
@items = {} | |
@count = 0 | |
guid = 0 | |
stringCache = {} | |
hash = (object) -> | |
switch typeof object | |
when 'number', 'boolean' | |
return '' + object | |
when 'string' | |
return stringCache[object] ?= '' + (guid++) | |
when 'object' | |
return object['__setGuid'] ?= '' + (guid++) | |
contains: (item) -> | |
return @items[hash(item)] is item | |
add: (item) -> | |
return if @contains item | |
@items[hash(item)] = item | |
@count++ | |
remove: (item) -> | |
return unless @contains item | |
delete @items[hash(item)] | |
@count-- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment