 
 # Set implementation
 
-dependencySet = ->
-  numDependencies: 0
-  dependencies: {}
-
-addDependency = (set, parentName) ->
-  return if set.dependencies[parentName]
-  set.numDependencies += 1
-  set.dependencies[parentName] = true
-
-removeDependency = (set, parentName) ->
-  return if !set.dependencies[parentName]
-  set.numDependencies -= 1
-  delete set.dependencies[parentName]
-
-addAllTo = (set, array) ->
+newSet = ->
+  size: 0
+  values: {}
+
+addToSet = (set, value) ->
+  return if set.values[value]
+  set.size += 1
+  set.values[value] = true
+
+removeFromSet = (set, value) ->
+  return if !set.values[value]
+  set.size -= 1
+  delete set.values[value]
+
+addAllToSet = (set, array) ->
   for v in array
-    addDependency set, v
+    addToSet set, v
   set
 
-removeAll = (set, array) ->
+removeAllFromSet = (set, array) ->
   for v in array
-    removeDependency set, v
+    removeFromSet set, v
   set
 
 setToArray = (set) ->
-  for n of set.dependencies
-    n
+  set.values
 
 cloneSet = (set) ->
-  clone = dependencySet()
-  addAllTo clone, setToArray set
+  clone = newSet()
+  addAllToSet clone, setToArray set
 
 inSet = (set, name) ->
-  set.dependencies[name]
+  set.values[name]
+
+isSetEmpty = (set) ->
+  set.size is 0
+
+# end of Set
 
 # mycroft = (context, node, environment) ->
 #   if node.label is 'numerical'

reverseGraph = (nodes) ->
  reversed = {}
  for child in nodes
    for dependencyName of child.set.dependencies
      (reversed[dependencyName] ?= []).push child
  reversed


#   Map (name -> GraphElement)
constructDependencyGraph = (wheres) ->
  lookupByName = {}
  deps = newSet()
  # find all defined names
  graph = for [pattern, def], i in wheres
    def: [pattern, def]
    set: newSet()
    names: findNames pattern
    missing: newSet()

  lookupByName = lookupTableForGraph graph

