home *** CD-ROM | disk | FTP | other *** search
/ PC Media 7 / PC MEDIA CD07.iso / share / prog / cm / cmbdict.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-06  |  4.3 KB  |  175 lines

  1. // CmBDict.cpp
  2. // -----------------------------------------------------------------
  3. // Compendium - C++ Container Class Library
  4. // Copyright (C) 1992-1994, Glenn M. Poorman, All rights reserved
  5. // -----------------------------------------------------------------
  6. // BTree dictionary implementation.
  7. // -----------------------------------------------------------------
  8.  
  9. #include <cm/include/cmbdict.h>
  10.  
  11.  
  12. // "CmBTreeDictionary" is the default dictionary constructor.
  13. //
  14. CmBTreeDictionary::CmBTreeDictionary(unsigned sz)
  15.                   : CmBTree(sz)
  16. {}
  17.  
  18.  
  19. // "CmBTreeDictionary" is the dictionary copy constructor.
  20. //
  21. CmBTreeDictionary::CmBTreeDictionary(const CmBTreeDictionary& D)
  22.                   : CmBTree(D)
  23. {}
  24.  
  25.  
  26. // "~CmBTreeDictionary" is the dictionary destructor.
  27. //
  28. CmBTreeDictionary::~CmBTreeDictionary()
  29. {
  30.   removeAll();
  31. }
  32.  
  33.  
  34. // "=" assignment operator copies the specified dictionary into
  35. // this dictionary.
  36. //
  37. CmBTreeDictionary& CmBTreeDictionary::operator=(const CmBTreeDictionary& D)
  38. {
  39.   return (CmBTreeDictionary&) CmBTree::operator=(D);
  40. }
  41.  
  42.  
  43. // "add" adds an association to the dictionary.
  44. //
  45. Bool CmBTreeDictionary::add(CmObject* pObj)
  46. {
  47.   if (pObj->isA("CmAssociation"))
  48.     if (!CmBTree::contains(pObj))
  49.       return CmBTree::add(pObj);
  50.   return FALSE;
  51. }
  52.  
  53.  
  54. // "add" adds a key/object pair to the dictionary.
  55. //
  56. Bool CmBTreeDictionary::add(CmObject* pKey, CmObject* pObj)
  57. {
  58.   CmAssociation *pAss = new CmAssociation(pKey, pObj);
  59.   if (!pAss) return FALSE;
  60.  
  61.   if (CmBTree::contains(pAss))
  62.   {
  63.     delete pAss;
  64.     return FALSE;
  65.   }
  66.   else
  67.     return CmBTree::add(pAss);
  68. }
  69.  
  70.  
  71. // "lookup" looks for the first occurrence of an association with
  72. // a key that isEqual to the specified key and returns that association's
  73. // key value.
  74. //
  75. CmObject* CmBTreeDictionary::lookup(CmObject* pKey) const
  76. {
  77.   CmAssociation *pAss = lookupAssoc(pKey);
  78.   return (pAss) ? pAss->key() : NULL;
  79. }
  80.  
  81.  
  82. // "lookupObject" looks for the first occurrence of an association with
  83. // a key that isEqual to the specified key and returns that association's
  84. // object value.
  85. //
  86. CmObject* CmBTreeDictionary::lookupObject(CmObject* pKey) const
  87. {
  88.   CmAssociation *pAss = lookupAssoc(pKey);
  89.   return (pAss) ? pAss->object() : NULL;
  90. }
  91.  
  92.  
  93. // "lookupObject" looks for the first occurrence of an association with
  94. // a key that isEqual to the specified key and returns that association.
  95. //
  96. CmAssociation* CmBTreeDictionary::lookupAssoc(CmObject* pKey) const
  97. {
  98.   CmAssociation assoc(pKey, NULL);
  99.   return (CmAssociation*) CmBTree::lookup(&assoc);
  100. }
  101.  
  102.  
  103. // "replace" looks for the first occurrence of an association with
  104. // a key that isEqual to the specified key and replaces it's object
  105. // value with the specified object.
  106. //
  107. Bool CmBTreeDictionary::replace(CmObject* pKey, CmObject* pObj)
  108. {
  109.   CmAssociation *pAss = lookupAssoc(pKey);
  110.   if (!pAss) return FALSE;
  111.  
  112.   if (ownsObjects()) delete pAss->object();
  113.   pAss->object(pObj);
  114.   return TRUE;
  115. }
  116.  
  117.  
  118. // "remove" looks for the first occurrence of an association with
  119. // a key that isEqual to the specified key and removes it.
  120. //
  121. Bool CmBTreeDictionary::remove(CmObject* pKey)
  122. {
  123.   CmAssociation *pAss = lookupAssoc(pKey);
  124.   if (pAss)
  125.   {
  126.     CmObject* pKey    = pAss->key   ();
  127.     CmObject* pObject = pAss->object();
  128.     Bool success = CmBTree::remove(pAss);
  129.     if (success && ownsObjects())
  130.     {
  131.       delete pKey;
  132.       delete pObject;
  133.     }
  134.     return success;
  135.   }
  136.   return FALSE;
  137. }
  138.  
  139.  
  140. // "contains" checks to see is an association exists with a key which
  141. // isEqual to the specified key.
  142. //
  143. Bool CmBTreeDictionary::contains(CmObject* pKey) const
  144. {
  145.   return (lookupAssoc(pKey) == NULL) ? FALSE : TRUE;
  146. }
  147.  
  148.  
  149. // "occurrences" returns the number of associations with a key which
  150. // isEqual to the specified key.  (This should always be 1).
  151. //
  152. unsigned CmBTreeDictionary::occurrences(CmObject* pKey) const
  153. {
  154.   CmAssociation assoc(pKey, NULL);
  155.   return CmBTree::occurrences(&assoc);
  156. }
  157.  
  158.  
  159. // "removeAll" removes all of the associations from the dictionary.
  160. //
  161. void CmBTreeDictionary::removeAll()
  162. {
  163.   if (ownsObjects())
  164.   {
  165.     CmBTreeIterator iterator(*this);
  166.     while (!iterator.done())
  167.     {
  168.       CmAssociation *pAss = (CmAssociation*) iterator.next();
  169.       delete pAss->key   ();
  170.       delete pAss->object();
  171.     }
  172.   }
  173.   CmBTree::removeAll();
  174. }
  175.