home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc / Developer Documentation / Recipes, Tech Notes & Articles / Tech Notes / Utilities Documentation / Collections < prev    next >
Encoding:
Text File  |  1995-11-07  |  8.8 KB  |  274 lines  |  [TEXT/ttxt]

  1. OpenDocâ„¢ Utilities Documentation
  2.  
  3. Collection Classes
  4. by The OpenDoc Design Team
  5.  
  6. © 1993-1995  Apple Computer, Inc. All Rights Reserved.
  7. Apple, the Apple logo, and Macintosh are registered trademarks of Apple Computer, Inc.
  8. Mac and OpenDoc are trademarks of Apple Computer, Inc.
  9.  
  10.  
  11. Introduction
  12.  
  13. The OpenDoc Utilities include rudimentary C++ collection classes and iterators which may be used by part editors for such things as storing lists of display frames. Nevertheless, part editors are free to use their own collection classes.
  14.  
  15. The classes are not template-based. The OrderedCollection class implements an ordered collection of elements of type void*.  The LinkedList class is a fairly low-level class used in the implementation of the OrderedCollection class, but may be used directly by subclassing class Link for new element types.
  16.  
  17. OrderedCollection
  18.  
  19. The OrderedCollection class implements a collection of elements whose order is determined externally (i.e. by the way they are inserted). The elements are not accessible by a key, but methods are provided to access the first and last element, as well as the element before and after a given element. The elements are of type ElementType, which is define as a void*. The collection is implemented as a linked list.
  20.  
  21. Constructors and Destructors
  22.  
  23. Two constructors are provided. The second one allows the client to specify a memory heap for allocating the internal memory of the collection:
  24.  
  25.     OrderedCollection();
  26.         // Constructs an empty collection
  27.  
  28.     OrderedCollection(ODMemoryHeapID where);
  29.         // Constructs an empty collection. Will allocate memory from the specified memory heap.
  30.  
  31.     virtual ~OrderedCollection();
  32.         // Destructs the collection, deleting the links, but not the elements themselves.
  33.     
  34.  
  35. Adding elements
  36.  
  37.     ODVMethod void AddFirst(ElementType element); 
  38.         // Adds an element to the beginning of the collection.
  39.  
  40.     ODVMethod void AddLast(ElementType element);
  41.         // Adds an element to the end of the collection.
  42.  
  43.     ODVMethod void AddBefore(ElementType existing, ElementType tobeadded);
  44.         // Adds an element, inserting it before a specified element.
  45.  
  46.     ODVMethod void AddAfter(ElementType existing, ElementType tobeadded);
  47.         // Adds an element, inserting it after a specified element.
  48.  
  49. Accessing elements
  50.  
  51.     ODVMethod ElementType    After(ElementType existing) const; 
  52.         // Returns the element after the specified one, or kODNULL.
  53.  
  54.     ODVMethod ElementType    Before(ElementType existing) const;
  55.         // Returns the element before the specified one, or kODNULL.
  56.         
  57.     ODVMethod ElementType    First() const;    
  58.         // Returns the first element
  59.     ODVMethod ElementType    Last() const;    
  60.         // Returns the last element
  61.  
  62. Removing elements
  63.  
  64.     ODVMethod ElementType    RemoveFirst();     
  65.         // Removes the first element
  66.  
  67.     ODVMethod ElementType    RemoveLast();     
  68.         // Removes the last element
  69.  
  70.     ODVMethod void        RemoveAll();        
  71.         // Called from the destructor. Removes all elements, deleting the links
  72.         // Does not delete the elements themselves
  73.         
  74.     ODVMethod void    DeleteAll();    
  75.         // Removes and deletes all links and elements. 
  76.  
  77. Note: Since ElementType is void*, no element destructor will be called. Clients could iterate over the elements to free element contents, and then call RemoveAll, rather than DeleteAll.
  78.         
  79.     ODVMethod ODBoolean    Remove(ElementType existing);
  80.  
  81.         // Removes an existing element. Returns true if an element was actually removed.
  82.         
  83.     ODVMethod ODBoolean    Contains(ElementType existing) const;
  84.  
  85.         // Returns kODTrue if the specified element is in the collection.
  86.     
  87.     ODVMethod OrderedCollectionIterator* CreateIterator();
  88.  
  89.         // Returns an iterator for the elements of the collection.
  90.  
  91. Miscellaneous
  92.     
  93.     ODNVMethod ODMemoryHeapID GetHeap() const;
  94.  
  95.         // Returns the memory heap used for the links
  96.  
  97.     ODNVMethod ODULong Count() const ;
  98.  
  99.         // Returns the number of elements in the collection
  100.  
  101.      ODVMethod ValueLink*     CreateNewLink(ElementType value) const;
  102.  
  103.         // A protected method to allocate links for the elements
  104.  
  105.      ODVMethod ODBoolean    ElementsMatch(ElementType v1,ElementType v2) const;
  106.          
  107.         // A protected method to compare elements for equality. Does a pointer comparison by default.
  108.  
  109. OrderedCollectionIterator
  110.  
  111. The OrderedCollectionIterator class provides a way to enumerate the elements of the collection in either order.  If the collection is modified while iterating, a kODErrIteratorOutOfSync error will be thrown. However, it is possible to remove the current element provide this is done using the iterator's RemoveCurrent method is used.
  112.  
  113.     OrderedCollectionIterator(OrderedCollection* collection);
  114.         // Construct an iterator for the specified collection.
  115.  
  116.     ~OrderedCollectionIterator();
  117.         // Destruct the iterator
  118.  
  119.     ElementType First();
  120.         // Return the first element in the collection.
  121.  
  122.     ElementType Next();
  123.         // Return the next element in the collection, or the first element if First() has never been called.
  124.  
  125.     ElementType Last();
  126.         // Returns the last element in the collection.
  127.  
  128.     ElementType Previous();
  129.         // Returns the previous element in the collection, or the last element if Last() was never called.
  130.  
  131.     ODBoolean   IsNotComplete();
  132.         // Returns kODTrue if there are additional elements to iterate over.
  133.  
  134.     void        RemoveCurrent();
  135.         // Remove the current element from the collection
  136.  
  137. Example 1:
  138.  
  139.     OrderedCollectionIterator iter(fContentFrames);
  140.     
  141.     for (ODFrame* aFrame = (ODFrame*)iter.First(); 
  142.                     iter.IsNotComplete();
  143.                     aFrame = (ODFrame*)iter.Next())
  144.     {
  145.         // Do something with aFrame
  146.     }
  147.  
  148. Example 2:
  149.  
  150.     ODFrame* aFrame = kODNULL;
  151.     while ((aFrame = (ODFrame*) iter.Next()) != kODNULL)
  152.     {
  153.         // Do something with aFrame
  154.     }
  155.  
  156. LinkedList
  157.  
  158.  
  159. The Link, LinkedList, and LinkedListIterator classes are used in the implementation of OrderedCollection and OrderedCollectionIterator, but they can be used directly,  by subclassing  Link for new element types. The client is responsible for memory management of Links - LinkedList just strings them together.
  160.  
  161. In order to do anything useful with a LinkedList, the Link class must be subclassed to add a data member for the element. See ValueLink in OrdColl.h. The Link class is not document here, since its methods are called by LinkedList.
  162.  
  163. Constructors and Destructors
  164.  
  165.           LinkedList();
  166.             // Construct an empty LinkedList
  167.           
  168.           ~LinkedList();
  169.             // Destruct the list
  170.  
  171. Adding elements
  172.           void            AddBefore(Link& existing, Link* link);
  173.             // Adds a link before a specified link
  174.           
  175.           void            AddAfter(Link& existing, Link* link);
  176.             // Adds a link after a specified link
  177.           
  178.           void            AddFirst(Link* link);
  179.             // Adds a link at the beginning.
  180.           
  181.           void            AddLast(Link* link);
  182.             // Adds a link at the end
  183.           
  184.           void            AddLast( LinkedList &list );
  185.             // Append the contents of another list, emptying the other list.
  186.           
  187.           void            AddLastUnique( LinkedList &list );
  188.             // Append the contents of another list, eliminating duplicates
  189.  
  190. Accessing Elements
  191.  
  192.           ODBoolean        Includes( const Link* ) const;
  193.             // Return kODTrue if the list contains the specified Link
  194.  
  195.           Link*            After(const Link& link) const;
  196.             // Return the link after the specified one
  197.           
  198.           Link*             Before(const Link& link) const;
  199.             // Return the link before the specified one
  200.           
  201.           Link*            First()     const;
  202.             // Return the first link
  203.           
  204.           Link*             Last()    const;
  205.             // Return the last link
  206.  
  207. Removing Elements
  208.  
  209.           void            Remove(Link&);
  210.             // Remove the specified link
  211.           
  212.           void            RemoveAll();
  213.             // Remove all links. Deleting links is the responsibility of the client.
  214.  
  215.           void            DeleteAllLinks();
  216.             // Removes and deletes all the links
  217.           
  218.           Link*            RemoveFirst();
  219.             // Removes the first link.
  220.           
  221.           Link*            RemoveLast();
  222.             // Removes the last link
  223.  
  224. Miscellaneous
  225.           
  226.           ODBoolean        IsEmpty( )    const;
  227.             // Return kODTrue if there are no elements in the list
  228.           
  229.           ODULong            Count()        const;
  230.             // Return the number of elements in the list
  231.           
  232.           
  233.           
  234.           
  235.  
  236.  
  237. LinkedListIterator
  238.  
  239.     
  240.         LinkedListIterator(LinkedList*    list);
  241.             // Construct an iterator for the specified linked list
  242.         
  243.         ~LinkedListIterator();
  244.             // Destruct the linked list
  245.         
  246.         Link*            First();
  247.             // Return the first link
  248.         
  249.         Link*            Next();
  250.             // Return the next link, or the first link if First was never called
  251.         
  252.         Link*            Last();
  253.             // Return the last link
  254.         
  255.         Link*            Previous();
  256.             // Return the next link (going backwards), or the last link if Last() was never called
  257.  
  258.         Link*            Current();
  259.             // Return the current link
  260.  
  261.         ODBoolean         IsNotComplete();
  262.             // Returns kODTrue if there are additional elements to iterate over.
  263.         
  264.         void            RemoveCurrent();
  265.             // Remove the current link
  266.         
  267. Node and NodeTraverser
  268.  
  269. The Node and NodeTraverser classes were not really intended for use by developers, but you might find them interesting. Much like LinkedList, they are implementation classes which could be used to implement higher-level classes for managing hierarchical data. 
  270.  
  271. DictList
  272.  
  273. The DictList class is primarily intended for use within OpenDoc. Each element in the list is a pair of void* values.
  274.