home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / ODFDev / ODF / Found / FWCollec / Sources / FWDictLi.cpp next >
Encoding:
C/C++ Source or Header  |  1995-11-08  |  7.9 KB  |  288 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        FWDictLi.cpp
  3.  
  4.     Contains:    Implementation of FW_CPrivDictionaryList
  5.  
  6.     Written by:    Richard Rodseth
  7.  
  8.     Copyright:    © 1993 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.          <3>     8/17/94    jpa        Added DeleteSOMValues method to delete SOM
  13.                                     objects properly [1181515]
  14.          <2>      6/9/94    RR        Remove ASLM Stuff. XMP->OD
  15.          <2>     3/15/94    MB        Changes to support SCpp/ASLM builds,
  16.                                     #1150864.
  17.          <3>     11/4/93    RR        Moved class FW_CPrivAssociation from .h. Fixed
  18.                                     memory leak caused by DeleteValues removing
  19.                                     links so that they weren't there anymore
  20.                                     for RemoveAll to delete.
  21.          <2>    10/28/93    RR        Removed ambiguous constructor
  22.          <1>     8/16/93    RCR        first checked in
  23.  
  24.     To Do:
  25. */
  26.  
  27. #include "FWFound.hpp"
  28.  
  29. #ifndef FWDICTLI_H
  30. #include "FWDictLi.h"
  31. #endif
  32.  
  33. #if FW_LIB_EXPORT_PRAGMAS
  34. #pragma lib_export on
  35. #endif
  36.  
  37. #ifdef FW_BUILD_MAC
  38. #pragma segment fwcollec
  39. #endif
  40.  
  41.  
  42. //=====================================================================================
  43. // Class FW_CPrivAssociation
  44. //=====================================================================================
  45.  
  46. FW_CPrivAssociation::FW_CPrivAssociation(FW_PrivKeyType key, FW_PrivValueType value)
  47. {
  48.     fKey = key;
  49.     fValue = value;
  50. }
  51.  
  52. FW_CPrivAssociation::~FW_CPrivAssociation()
  53. {
  54. }
  55.  
  56. //======================================================================================
  57. // Class FW_CPrivDictionaryList
  58. //======================================================================================
  59.  
  60. //------------------------------------------------------------------------------
  61. // FW_CPrivDictionaryList::FW_CPrivDictionaryList
  62. //
  63. // Description
  64. //------------------------------------------------------------------------------
  65.  
  66. FW_CPrivDictionaryList::FW_CPrivDictionaryList()
  67. {
  68. }
  69.  
  70. //------------------------------------------------------------------------------
  71. // FW_CPrivDictionaryList::~FW_CPrivDictionaryList
  72. //
  73. // Description
  74. //------------------------------------------------------------------------------
  75.  
  76. FW_CPrivDictionaryList::~FW_CPrivDictionaryList()
  77. {
  78.     this->RemoveAll();
  79. }
  80.  
  81. //------------------------------------------------------------------------------
  82. // FW_CPrivDictionaryList::AddPair
  83. //
  84. // Description
  85. //------------------------------------------------------------------------------
  86.  
  87. void FW_CPrivDictionaryList::AddPair(FW_PrivKeyType key, FW_PrivValueType value)
  88. {
  89.     FW_Boolean found = FALSE;
  90.     FW_CPrivLinkedListIterator iter(&fImplementation);
  91.     FW_CPrivAssociation* association = (FW_CPrivAssociation*) iter.First();
  92.     while (association != NULL)
  93.     {
  94.         FW_PrivKeyType k = association->GetKey();
  95.  
  96.         if (this->KeysMatch(k,key))
  97.         {
  98.             found = TRUE;
  99.             association->SetValue(value);
  100.             break;
  101.         }
  102.         else
  103.             association = (FW_CPrivAssociation*) iter.Next();
  104.     }    
  105.     if (!found)
  106.     {
  107.         association = this->CreateNewAssociation(key,value);
  108.         fImplementation.AddLast(association);
  109.     }
  110. }
  111.  
  112. //------------------------------------------------------------------------------
  113. // FW_CPrivDictionaryList::Count
  114. //
  115. // Description
  116. //------------------------------------------------------------------------------
  117.  
  118. unsigned long FW_CPrivDictionaryList::Count()
  119. {
  120.     return fImplementation.Count();
  121. }
  122.  
  123.  
  124. //------------------------------------------------------------------------------
  125. // FW_CPrivDictionaryList::ContainsKey
  126. //
  127. // Description
  128. //------------------------------------------------------------------------------
  129.  
  130. FW_Boolean FW_CPrivDictionaryList::ContainsKey(FW_PrivKeyType key)
  131. {
  132.     FW_CPrivLinkedListIterator iter(&fImplementation);
  133.     FW_CPrivAssociation* association = (FW_CPrivAssociation*) iter.First();
  134.     while (association != NULL)
  135.     {
  136.         FW_PrivKeyType k = association->GetKey();
  137.  
  138.         if (this->KeysMatch(k,key))
  139.         {
  140.             return TRUE;    
  141.         }
  142.         else
  143.             association = (FW_CPrivAssociation*) iter.Next();
  144.     }    
  145.     return FALSE;
  146. }
  147.  
  148. //------------------------------------------------------------------------------
  149. // FW_CPrivDictionaryList::ValueAtKey
  150. //
  151. // Description
  152. //------------------------------------------------------------------------------
  153.  
  154. FW_PrivValueType FW_CPrivDictionaryList::ValueAtKey(FW_PrivKeyType key)
  155. {
  156.     FW_CPrivLinkedListIterator iter(&fImplementation);
  157.     FW_CPrivAssociation* association = (FW_CPrivAssociation*) iter.First();
  158.     while (association != NULL)
  159.     {
  160.         FW_PrivKeyType k = association->GetKey();
  161.  
  162.         if (this->KeysMatch(k,key))
  163.         {
  164.             return association->GetValue();    
  165.         }
  166.         else
  167.             association = (FW_CPrivAssociation*) iter.Next();
  168.     }    
  169.     return NULL;
  170. }
  171.  
  172. //------------------------------------------------------------------------------
  173. // FW_CPrivDictionaryList::RemoveKey
  174. //
  175. // Description
  176. //------------------------------------------------------------------------------
  177.  
  178. void FW_CPrivDictionaryList::RemoveKey(FW_PrivKeyType key)
  179. {
  180.     FW_CPrivLinkedListIterator iter(&fImplementation);
  181.     FW_CPrivAssociation* association = (FW_CPrivAssociation*) iter.First();
  182.     while (association != NULL)
  183.     {
  184.         FW_PrivKeyType k = association->GetKey();
  185.  
  186.         if (this->KeysMatch(k,key))
  187.         {
  188.             fImplementation.Remove(*association);
  189.             break;
  190.         }
  191.         else
  192.             association = (FW_CPrivAssociation*) iter.Next();
  193.     }    
  194. }
  195.  
  196. //------------------------------------------------------------------------------
  197. // FW_CPrivDictionaryList::RemoveAll
  198. //
  199. // Description
  200. //------------------------------------------------------------------------------
  201.  
  202. void FW_CPrivDictionaryList::RemoveAll()
  203. {
  204.     FW_CPrivLink* link = fImplementation.RemoveFirst();
  205.     while (link != NULL)
  206.     {
  207.         delete link;
  208.         link = fImplementation.RemoveFirst();
  209.     }
  210. }
  211.  
  212. //------------------------------------------------------------------------------
  213. // FW_CPrivDictionaryList::DeleteKeys
  214. //
  215. // Description
  216. //------------------------------------------------------------------------------
  217.  
  218. void FW_CPrivDictionaryList::DeleteKeys()
  219. {
  220.     FW_CPrivLinkedListIterator iter(&fImplementation);
  221.     
  222.     for (FW_CPrivAssociation* association = (FW_CPrivAssociation*) iter.First();
  223.         iter.IsNotComplete();
  224.         association = (FW_CPrivAssociation*) iter.Next())
  225.     {
  226.         FW_PrivKeyType key = association->GetKey();
  227.         delete key; 
  228.     }
  229. }
  230.  
  231. //------------------------------------------------------------------------------
  232. // FW_CPrivDictionaryList::DeleteValues
  233. //
  234. // Delete non-class values; plain vanilla global operator delete will be called
  235. //------------------------------------------------------------------------------
  236.  
  237. void FW_CPrivDictionaryList::DeleteValues()
  238. {
  239.     FW_CPrivLinkedListIterator iter(&fImplementation);
  240.     
  241.     for (FW_CPrivAssociation* association = (FW_CPrivAssociation*) iter.First();
  242.         iter.IsNotComplete();
  243.         association = (FW_CPrivAssociation*) iter.Next())
  244.     {
  245.         FW_PrivValueType value = association->GetValue();
  246.         delete value; 
  247.     }
  248. }
  249.  
  250. //------------------------------------------------------------------------------
  251. // FW_CPrivDictionaryList::CreateNewAssociation
  252. //
  253. // Description
  254. //------------------------------------------------------------------------------
  255.  
  256. FW_CPrivAssociation*    FW_CPrivDictionaryList::CreateNewAssociation(FW_PrivKeyType key, FW_PrivValueType value) const
  257. {
  258.     return new FW_CPrivAssociation(key, value);
  259. }
  260.  
  261. //------------------------------------------------------------------------------
  262. // FW_CPrivDictionaryList::KeysMatch
  263. //
  264. // Description
  265. //------------------------------------------------------------------------------
  266.  
  267. FW_Boolean    FW_CPrivDictionaryList::KeysMatch(FW_PrivKeyType key1,FW_PrivKeyType key2) const
  268. {
  269.     return (key1 == key2);
  270. }
  271.  
  272. //------------------------------------------------------------------------------
  273. // FW_CPrivDictionaryList::Merge
  274. //------------------------------------------------------------------------------
  275.  
  276. void FW_CPrivDictionaryList::Merge(FW_CPrivDictionaryList &other)
  277. {
  278.     FW_CPrivLinkedListIterator *iterator = other.CreateIterator();
  279.     for (FW_CPrivAssociation* association = (FW_CPrivAssociation*) iterator->First();
  280.         iterator->IsNotComplete();
  281.         association = (FW_CPrivAssociation*) iterator->Next())
  282.     {
  283.         if (!this->ContainsKey(association->GetKey()))
  284.             this->AddPair(association->GetKey(), association->GetValue());
  285.     }
  286.     delete iterator;
  287. }
  288.