Skip to content

Instantly share code, notes, and snippets.

@devongovett
Created January 15, 2012 09:15
Show Gist options
  • Save devongovett/1615172 to your computer and use it in GitHub Desktop.
Save devongovett/1615172 to your computer and use it in GitHub Desktop.
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