class AbstractGraph::Composition::UniqueNameCollection
public UniqueNameCollection
class Note that the collection of this
class must implement #name
Attributes
Public Class Methods
d: Initializes the UNC. a: Assign itself a namespace_ticket
and start the collection by adding
the namespace a set of names and backreference of tickets
t: constant p: r: new UNC
# File lib/abstract_graph/composition/unique_name_collection/initialize.rb, line 13 def initialize @collection = Hash.new @@namespace_counter += 1 @namespace_ticket = Ticket.new(@@namespace_counter) @@namespace[@@namespace_counter] = [Set.new, Set.new([@namespace_ticket])] end
Public Instance Methods
d: Adds the object to our UNC. a: Ensure that our namespace doesn’t already have the object name and
then add the object to namespace and collection.
t: constant p: Object o that implements name r: The object o itself
# File lib/abstract_graph/composition/unique_name_collection/add.rb, line 13 def add( o ) set = @@namespace[@namespace_ticket.get][0] raise IndexError if set.include? o.name set << o.name @collection[o.name] = o self end
d: Delete the object from the collection a: Delete the object from the collection and from the nameserver t: constant p: name of object we want to delete r: object that was deleted
# File lib/abstract_graph/composition/unique_name_collection/delete.rb, line 12 def delete( name ) return nil unless @collection[name] @@namespace[@namespace_ticket.get][0].delete name @collection.delete name end
d: Deep copies our object. a: Does a deep copy of the object, in otherwords
copies every object in the collection.
t: |collection| p: r: the copy of the UNC
# File lib/abstract_graph/composition/unique_name_collection/dup.rb, line 13 def dup other = self.class.new # copy each object in our collection over @collection.each_value do |o| other.add o.dup end other end
d: Iterate through all objects. a: Enumerates through every object. t: constant p: block that we pass in each object r: Enumerable if no block, collection if yes block
# File lib/abstract_graph/composition/unique_name_collection/each.rb, line 12 def each( &block ) @collection.each_value( &block ) end
d: Finds a stored object given the name. a: Goes into collection and retrieves based on key. t: constant p: name of object r: object
# File lib/abstract_graph/composition/unique_name_collection/find.rb, line 12 def find(name) @collection[name] end
d: Links the UNC so they have unique namespace. a: check the intersection of namespace so that there’s no collision, then
reset the namespace to contain the shared namespaces and update tickets
t: |intersection and size of ticket| p: UniqueNameCollection
unc is the other collection we want to link r: false if the two collections are not already mutually unique
UNC if it succeeds
# File lib/abstract_graph/composition/unique_name_collection/link.rb, line 14 def link( unc ) our_ticket = @namespace_ticket.get other_ticket = unc.namespace_ticket.get our_namespace = @@namespace[our_ticket] other_namespace = @@namespace[other_ticket] namespace_intersection = our_namespace[0] & other_namespace[0] return nil unless namespace_intersection.empty? # Create the assumption that |our_namespace| >= |other_namespace| if other_namespace[0].size > our_namespace[0].size our_namespace, other_namespace = other_namespace, our_namespace our_ticket, other_ticket = other_ticket, our_ticket end # Assume it is fine to link now, add to the larger namespace our_namespace[0].merge other_namespace[0] other_namespace[1].map do |ticket| ticket.set our_ticket end our_namespace[1].merge other_namespace[1] @@namespace.delete other_ticket self end
d: Handles everything. a: Pass all methods into the hash. t: p: r:
# File lib/abstract_graph/composition/unique_name_collection/method_missing.rb, line 12 def method_missing( m, *args, &block ) @collection.values.send( m.to_sym, *args, &block ) end
d: Change the name of a vertex in our graph. a: Check the namespace otherwise, set collection and add to namespace t: constant p: String oldname represents the current vertex’s name
String newname represents the new name of our vertex
r: UNC itself
# File lib/abstract_graph/composition/unique_name_collection/rename.rb, line 13 def rename( oldname, newname ) return nil unless @collection.has_key? oldname nameserver = @@namespace[@namespace_ticket.get][0] throw Exception if nameserver.include? newname # change the nameserver nameserver.delete oldname nameserver << newname # rename the object itself @collection[oldname].name = newname # remap the name @collection[newname] = @collection[oldname] # clear the old name @collection.delete oldname self end