home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 July: Mac OS SDK / Dev.CD Jul 96 SDK / Dev.CD Jul 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc / OpenDoc Development / Debugging Support / OpenDoc Source Code / Utilities / DictList.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-22  |  6.9 KB  |  285 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        DictList.cpp
  3.  
  4.     Contains:    Implementation of DictionaryList
  5.  
  6.     Written by:    Richard Rodseth
  7.  
  8.     Copyright:    © 1993 - 1995 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     
  11. */
  12.  
  13. #ifndef _DICTLIST_
  14. #include "DictList.h"
  15. #endif
  16.  
  17. #include <somobj.xh>
  18.  
  19. #pragma segment ODCollections
  20.  
  21. //=====================================================================================
  22. // Class Association
  23. //=====================================================================================
  24.  
  25. class Association : public Link {
  26.     public:
  27.         Association(KeyType key, ValueType value);        
  28.         virtual ~Association();
  29.         KeyType    GetKey()    { return fKey;}
  30.         void        SetKey(KeyType key)    { fKey = key;}
  31.         ValueType    GetValue()    { return fValue;}
  32.         void        SetValue(ValueType value)    { fValue = value;}
  33.     
  34.     private:
  35.         KeyType         fKey;
  36.         ValueType         fValue;
  37. };
  38.     
  39. Association::Association(KeyType key, ValueType value)
  40. {
  41.     fKey = key;
  42.     fValue = value;
  43. }
  44.  
  45. Association::~Association()
  46. {
  47. }
  48.  
  49. //======================================================================================
  50. // Class DictionaryList
  51. //======================================================================================
  52.  
  53. //------------------------------------------------------------------------------
  54. // DictionaryList::DictionaryList
  55. //
  56. // Description
  57. //------------------------------------------------------------------------------
  58.  
  59. DictionaryList::DictionaryList()
  60. {
  61. }
  62.  
  63. //------------------------------------------------------------------------------
  64. // DictionaryList::~DictionaryList
  65. //
  66. // Description
  67. //------------------------------------------------------------------------------
  68.  
  69. DictionaryList::~DictionaryList()
  70. {
  71.     this->RemoveAll();
  72. }
  73.  
  74. //------------------------------------------------------------------------------
  75. // DictionaryList::AddPair
  76. //
  77. // Description
  78. //------------------------------------------------------------------------------
  79.  
  80. void DictionaryList::AddPair(KeyType key, ValueType value)
  81. {
  82.     ODBoolean found = kODFalse;
  83.     LinkedListIterator iter(&fImplementation);
  84.     Association* association = (Association*) iter.First();
  85.     while (association != kODNULL)
  86.     {
  87.         KeyType k = association->GetKey();
  88.  
  89.         if (this->KeysMatch(k,key))
  90.         {
  91.             found = kODTrue;
  92.             association->SetValue(value);
  93.             break;
  94.         }
  95.         else
  96.             association = (Association*) iter.Next();
  97.     }    
  98.     if (!found)
  99.     {
  100.         association = this->CreateNewAssociation(key,value);
  101.         fImplementation.AddLast(association);
  102.     }
  103. }
  104.  
  105. //------------------------------------------------------------------------------
  106. // DictionaryList::Count
  107. //
  108. // Description
  109. //------------------------------------------------------------------------------
  110.  
  111. ODULong DictionaryList::Count()
  112. {
  113.     return fImplementation.Count();
  114. }
  115.  
  116.  
  117. //------------------------------------------------------------------------------
  118. // DictionaryList::ContainsKey
  119. //
  120. // Description
  121. //------------------------------------------------------------------------------
  122.  
  123. ODBoolean DictionaryList::ContainsKey(KeyType key)
  124. {
  125.     LinkedListIterator iter(&fImplementation);
  126.     Association* association = (Association*) iter.First();
  127.     while (association != kODNULL)
  128.     {
  129.         KeyType k = association->GetKey();
  130.  
  131.         if (this->KeysMatch(k,key))
  132.         {
  133.             return kODTrue;    
  134.         }
  135.         else
  136.             association = (Association*) iter.Next();
  137.     }    
  138.     return kODFalse;
  139. }
  140.  
  141. //------------------------------------------------------------------------------
  142. // DictionaryList::ValueAtKey
  143. //
  144. // Description
  145. //------------------------------------------------------------------------------
  146.  
  147. ValueType DictionaryList::ValueAtKey(KeyType key)
  148. {
  149.     LinkedListIterator iter(&fImplementation);
  150.     Association* association = (Association*) iter.First();
  151.     while (association != kODNULL)
  152.     {
  153.         KeyType k = association->GetKey();
  154.  
  155.         if (this->KeysMatch(k,key))
  156.         {
  157.             return association->GetValue();    
  158.         }
  159.         else
  160.             association = (Association*) iter.Next();
  161.     }    
  162.     return kODNULL;
  163. }
  164.  
  165. //------------------------------------------------------------------------------
  166. // DictionaryList::RemoveKey
  167. //
  168. // Description
  169. //------------------------------------------------------------------------------
  170.  
  171. void DictionaryList::RemoveKey(KeyType key)
  172. {
  173.     LinkedListIterator iter(&fImplementation);
  174.     Association* association = (Association*) iter.First();
  175.     while (association != kODNULL)
  176.     {
  177.         KeyType k = association->GetKey();
  178.  
  179.         if (this->KeysMatch(k,key))
  180.         {
  181.             fImplementation.Remove(*association);
  182.             break;
  183.         }
  184.         else
  185.             association = (Association*) iter.Next();
  186.     }    
  187. }
  188.  
  189. //------------------------------------------------------------------------------
  190. // DictionaryList::RemoveAll
  191. //
  192. // Description
  193. //------------------------------------------------------------------------------
  194.  
  195. void DictionaryList::RemoveAll()
  196. {
  197.     Link* link = fImplementation.RemoveFirst();
  198.     while (link != kODNULL)
  199.     {
  200.         delete link;
  201.         link = fImplementation.RemoveFirst();
  202.     }
  203. }
  204.  
  205. //------------------------------------------------------------------------------
  206. // DictionaryList::DeleteKeys
  207. //
  208. // Description
  209. //------------------------------------------------------------------------------
  210.  
  211. void DictionaryList::DeleteKeys()
  212. {
  213.     LinkedListIterator iter(&fImplementation);
  214.     
  215.     for (Association* association = (Association*) iter.First();
  216.         iter.IsNotComplete();
  217.         association = (Association*) iter.Next())
  218.     {
  219.         KeyType key = association->GetKey();
  220.         delete key; 
  221.     }
  222. }
  223.  
  224. //------------------------------------------------------------------------------
  225. // DictionaryList::DeleteValues
  226. //
  227. // Delete non-class values; plain vanilla global operator delete will be called
  228. //------------------------------------------------------------------------------
  229.  
  230. void DictionaryList::DeleteValues()
  231. {
  232.     LinkedListIterator iter(&fImplementation);
  233.     
  234.     for (Association* association = (Association*) iter.First();
  235.         iter.IsNotComplete();
  236.         association = (Association*) iter.Next())
  237.     {
  238.         ValueType value = association->GetValue();
  239.         delete value; 
  240.     }
  241. }
  242.  
  243. //------------------------------------------------------------------------------
  244. // DictionaryList::DeleteSOMValues
  245. //
  246. // Delete values that are known to be SOM objects
  247. //------------------------------------------------------------------------------
  248.  
  249. void DictionaryList::DeleteSOMValues()
  250. {
  251.     LinkedListIterator iter(&fImplementation);
  252.     
  253.     for (Association* association = (Association*) iter.First();
  254.         iter.IsNotComplete();
  255.         association = (Association*) iter.Next())
  256.     {
  257.         SOMObject *value = (SOMObject*) association->GetValue();
  258.         if( value )
  259.             delete value;
  260.     }
  261. }
  262.  
  263. //------------------------------------------------------------------------------
  264. // DictionaryList::CreateNewAssociation
  265. //
  266. // Description
  267. //------------------------------------------------------------------------------
  268.  
  269. Association*    DictionaryList::CreateNewAssociation(KeyType key, ValueType value) const
  270. {
  271.     return new Association(key, value);
  272. }
  273.  
  274. //------------------------------------------------------------------------------
  275. // DictionaryList::KeysMatch
  276. //
  277. // Description
  278. //------------------------------------------------------------------------------
  279.  
  280. ODBoolean    DictionaryList::KeysMatch(KeyType key1,KeyType key2) const
  281. {
  282.     return (key1 == key2);
  283. }
  284.  
  285.