home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / opendc12.zip / od124os2.exe / od12osr1.exe / src / DictList.cpp < prev    next >
C/C++ Source or Header  |  1997-03-21  |  9KB  |  312 lines

  1. //====START_GENERATED_PROLOG======================================
  2. //
  3. //
  4. //   COMPONENT_NAME: odutils
  5. //
  6. //   CLASSES: none
  7. //
  8. //   ORIGINS: 82,27
  9. //
  10. //
  11. //   (C) COPYRIGHT International Business Machines Corp. 1995,1996
  12. //   All Rights Reserved
  13. //   Licensed Materials - Property of IBM
  14. //   US Government Users Restricted Rights - Use, duplication or
  15. //   disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  16. //       
  17. //   IBM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  18. //   ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  19. //   PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  20. //   CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  21. //   USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  22. //   OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
  23. //   OR PERFORMANCE OF THIS SOFTWARE.
  24. //
  25. //====END_GENERATED_PROLOG========================================
  26. //
  27. // @(#) 1.3 com/src/utils/DictList.cpp, odutils, od96os2, odos29712d 7/15/96 17:57:34 [ 3/21/97 17:20:51 ]
  28. /*
  29.     File:        DictList.cpp
  30.  
  31.     Contains:    Implementation of DictionaryList
  32.  
  33.     Written by:    Richard Rodseth
  34.  
  35.     Copyright:    ⌐ 1993 - 1995 by Apple Computer, Inc., all rights reserved.
  36.  
  37.     
  38. */
  39.  
  40. #ifndef _DICTLIST_
  41. #include "DictList.h"
  42. #endif
  43.  
  44. #include <somobj.xh>
  45.  
  46. #pragma segment ODCollections
  47.  
  48. //=====================================================================================
  49. // Class Association
  50. //=====================================================================================
  51.  
  52. class Association : public Link {
  53.     public:
  54.         Association(KeyType key, ValueType value);        
  55.         virtual ~Association();
  56.         KeyType    GetKey()    { return fKey;}
  57.         void        SetKey(KeyType key)    { fKey = key;}
  58.         ValueType    GetValue()    { return fValue;}
  59.         void        SetValue(ValueType value)    { fValue = value;}
  60.     
  61.     private:
  62.         KeyType         fKey;
  63.         ValueType         fValue;
  64. };
  65.     
  66. Association::Association(KeyType key, ValueType value)
  67. {
  68.     fKey = key;
  69.     fValue = value;
  70. }
  71.  
  72. Association::~Association()
  73. {
  74. }
  75.  
  76. //======================================================================================
  77. // Class DictionaryList
  78. //======================================================================================
  79.  
  80. //------------------------------------------------------------------------------
  81. // DictionaryList::DictionaryList
  82. //
  83. // Description
  84. //------------------------------------------------------------------------------
  85.  
  86. DictionaryList::DictionaryList()
  87. {
  88. }
  89.  
  90. //------------------------------------------------------------------------------
  91. // DictionaryList::~DictionaryList
  92. //
  93. // Description
  94. //------------------------------------------------------------------------------
  95.  
  96. DictionaryList::~DictionaryList()
  97. {
  98.     this->RemoveAll();
  99. }
  100.  
  101. //------------------------------------------------------------------------------
  102. // DictionaryList::AddPair
  103. //
  104. // Description
  105. //------------------------------------------------------------------------------
  106.  
  107. void DictionaryList::AddPair(KeyType key, ValueType value)
  108. {
  109.     ODBoolean found = kODFalse;
  110.     LinkedListIterator iter(&fImplementation);
  111.     Association* association = (Association*) iter.First();
  112.     while (association != kODNULL)
  113.     {
  114.         KeyType k = association->GetKey();
  115.  
  116.         if (this->KeysMatch(k,key))
  117.         {
  118.             found = kODTrue;
  119.             association->SetValue(value);
  120.             break;
  121.         }
  122.         else
  123.             association = (Association*) iter.Next();
  124.     }    
  125.     if (!found)
  126.     {
  127.         association = this->CreateNewAssociation(key,value);
  128.         fImplementation.AddLast(association);
  129.     }
  130. }
  131.  
  132. //------------------------------------------------------------------------------
  133. // DictionaryList::Count
  134. //
  135. // Description
  136. //------------------------------------------------------------------------------
  137.  
  138. ODULong DictionaryList::Count()
  139. {
  140.     return fImplementation.Count();
  141. }
  142.  
  143.  
  144. //------------------------------------------------------------------------------
  145. // DictionaryList::ContainsKey
  146. //
  147. // Description
  148. //------------------------------------------------------------------------------
  149.  
  150. ODBoolean DictionaryList::ContainsKey(KeyType key)
  151. {
  152.     LinkedListIterator iter(&fImplementation);
  153.     Association* association = (Association*) iter.First();
  154.     while (association != kODNULL)
  155.     {
  156.         KeyType k = association->GetKey();
  157.  
  158.         if (this->KeysMatch(k,key))
  159.         {
  160.             return kODTrue;    
  161.         }
  162.         else
  163.             association = (Association*) iter.Next();
  164.     }    
  165.     return kODFalse;
  166. }
  167.  
  168. //------------------------------------------------------------------------------
  169. // DictionaryList::ValueAtKey
  170. //
  171. // Description
  172. //------------------------------------------------------------------------------
  173.  
  174. ValueType DictionaryList::ValueAtKey(KeyType key)
  175. {
  176.     LinkedListIterator iter(&fImplementation);
  177.     Association* association = (Association*) iter.First();
  178.     while (association != kODNULL)
  179.     {
  180.         KeyType k = association->GetKey();
  181.  
  182.         if (this->KeysMatch(k,key))
  183.         {
  184.             return association->GetValue();    
  185.         }
  186.         else
  187.             association = (Association*) iter.Next();
  188.     }    
  189.     return kODNULL;
  190. }
  191.  
  192. //------------------------------------------------------------------------------
  193. // DictionaryList::RemoveKey
  194. //
  195. // Description
  196. //------------------------------------------------------------------------------
  197.  
  198. void DictionaryList::RemoveKey(KeyType key)
  199. {
  200.     LinkedListIterator iter(&fImplementation);
  201.     Association* association = (Association*) iter.First();
  202.     while (association != kODNULL)
  203.     {
  204.         KeyType k = association->GetKey();
  205.  
  206.         if (this->KeysMatch(k,key))
  207.         {
  208.             fImplementation.Remove(*association);
  209.             break;
  210.         }
  211.         else
  212.             association = (Association*) iter.Next();
  213.     }    
  214. }
  215.  
  216. //------------------------------------------------------------------------------
  217. // DictionaryList::RemoveAll
  218. //
  219. // Description
  220. //------------------------------------------------------------------------------
  221.  
  222. void DictionaryList::RemoveAll()
  223. {
  224.     Link* link = fImplementation.RemoveFirst();
  225.     while (link != kODNULL)
  226.     {
  227.         delete link;
  228.         link = fImplementation.RemoveFirst();
  229.     }
  230. }
  231.  
  232. //------------------------------------------------------------------------------
  233. // DictionaryList::DeleteKeys
  234. //
  235. // Description
  236. //------------------------------------------------------------------------------
  237.  
  238. void DictionaryList::DeleteKeys()
  239. {
  240.     LinkedListIterator iter(&fImplementation);
  241.     
  242.     for (Association* association = (Association*) iter.First();
  243.         iter.IsNotComplete();
  244.         association = (Association*) iter.Next())
  245.     {
  246.         KeyType key = association->GetKey();
  247.         delete key; 
  248.     }
  249. }
  250.  
  251. //------------------------------------------------------------------------------
  252. // DictionaryList::DeleteValues
  253. //
  254. // Delete non-class values; plain vanilla global operator delete will be called
  255. //------------------------------------------------------------------------------
  256.  
  257. void DictionaryList::DeleteValues()
  258. {
  259.     LinkedListIterator iter(&fImplementation);
  260.     
  261.     for (Association* association = (Association*) iter.First();
  262.         iter.IsNotComplete();
  263.         association = (Association*) iter.Next())
  264.     {
  265.         ValueType value = association->GetValue();
  266.         delete value; 
  267.     }
  268. }
  269.  
  270. //------------------------------------------------------------------------------
  271. // DictionaryList::DeleteSOMValues
  272. //
  273. // Delete values that are known to be SOM objects
  274. //------------------------------------------------------------------------------
  275.  
  276. void DictionaryList::DeleteSOMValues()
  277. {
  278.     LinkedListIterator iter(&fImplementation);
  279.     
  280.     for (Association* association = (Association*) iter.First();
  281.         iter.IsNotComplete();
  282.         association = (Association*) iter.Next())
  283.     {
  284.         SOMObject *value = (SOMObject*) association->GetValue();
  285.         if( value )
  286.             delete value;
  287.     }
  288. }
  289.  
  290. //------------------------------------------------------------------------------
  291. // DictionaryList::CreateNewAssociation
  292. //
  293. // Description
  294. //------------------------------------------------------------------------------
  295.  
  296. Association*    DictionaryList::CreateNewAssociation(KeyType key, ValueType value) const
  297. {
  298.     return new Association(key, value);
  299. }
  300.  
  301. //------------------------------------------------------------------------------
  302. // DictionaryList::KeysMatch
  303. //
  304. // Description
  305. //------------------------------------------------------------------------------
  306.  
  307. ODBoolean    DictionaryList::KeysMatch(KeyType key1,KeyType key2) const
  308. {
  309.     return (key1 == key2);
  310. }
  311.  
  312.