home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ibmodf.zip / CNTNRPRT.ZIP / ORDCOLL.CPP < prev    next >
C/C++ Source or Header  |  1994-11-08  |  15KB  |  472 lines

  1. /********************************************************************/
  2. /*  Licensed Materials - Property of IBM                            */
  3. /*                                                                  */
  4. /*                                                                  */
  5. /* Copyright (C) International Business Machines Corp., 1994.       */
  6. /* Copyright (C) Apple Computer, Inc., 1994                         */
  7. /*                                                                  */
  8. /*  US Government Users Restricted Rights -                         */
  9. /*  Use, duplication, or disclosure restricted                      */
  10. /*  by GSA ADP Schedule Contract with IBM Corp.                     */
  11. /*                                                                  */
  12. /*  IBM Change History (most recent first):                         */
  13. /*  <OS2>   8/18/94 JLC First implementation.  Buggy, but works.    */
  14. /*                                                                  */
  15. /********************************************************************/
  16.  
  17. #ifndef _ORDCOLL_
  18.    #include "OrdColl.h"
  19. #endif
  20.  
  21. //#ifndef _ODNEW_
  22. //#include "ODNew.h"
  23. //#endif
  24.  
  25.  
  26. class ODxOCValueLink /*: public Link*/ {
  27.    friend ODxOrderedCollection;  
  28.  
  29.    public:
  30.                 ODxOCValueLink(ElementType value);    
  31.      virtual  ~ODxOCValueLink();
  32.                ElementType  GetValue()            { return fValue;}
  33.                void             SetValue(ElementType v)      { fValue = v;}
  34.    
  35.    private:
  36.      ElementType     fValue;
  37.      ODxOCValueLink *     fNext;
  38. };
  39.  
  40.  
  41. //======================================================================================
  42. // Class ODxOCValueLink - Implementation
  43. //======================================================================================
  44.  
  45. ODxOCValueLink::ODxOCValueLink(ElementType value)
  46. {
  47.   fValue = value;
  48.   fNext  = 0;
  49. }
  50.  
  51. ODxOCValueLink::~ODxOCValueLink()
  52. {
  53. }
  54.  
  55. //======================================================================================
  56. // Class ODxOrderedCollection
  57. //======================================================================================
  58.  
  59. //------------------------------------------------------------------------------
  60. // ODxOrderedCollection::ODxOrderedCollection
  61. //------------------------------------------------------------------------------
  62.  
  63. ODxOrderedCollection::ODxOrderedCollection()
  64. {
  65.   // fHeap = kODNULL;
  66.   fImplementation = 0;
  67. }
  68.  
  69. //------------------------------------------------------------------------------
  70. // ODxOrderedCollection::ODxOrderedCollection
  71. //------------------------------------------------------------------------------
  72.  
  73. //ODxOrderedCollection::ODxOrderedCollection(ODMemoryHeapID where)
  74. //{
  75. //  //fHeap = where;
  76. //}
  77.  
  78. // ODxOrderedCollection::~ODxOrderedCollection
  79. //------------------------------------------------------------------------------
  80.  
  81. ODxOrderedCollection::~ODxOrderedCollection()
  82. {
  83.   this->RemoveAll();
  84. }
  85.  
  86. //------------------------------------------------------------------------------
  87. // ODxOrderedCollection::AddFirst
  88. //------------------------------------------------------------------------------
  89.  
  90. void ODxOrderedCollection::AddFirst(ElementType element)
  91. {
  92.   ODxOCValueLink* newLink = this->CreateNewLink(element);
  93.   newLink->fNext = fImplementation;
  94.   fImplementation = newLink;
  95. }
  96.  
  97. //------------------------------------------------------------------------------
  98. // ODxOrderedCollection::AddLast
  99. //------------------------------------------------------------------------------
  100.  
  101. void ODxOrderedCollection::AddLast(ElementType element)
  102. {
  103.   ODxOCValueLink* newLink = CreateNewLink(element);
  104.   ODxOCValueLink* * nlr = &fImplementation;
  105.   while (*nlr) nlr = &((*nlr)->fNext);
  106.   *nlr = newLink;
  107. }
  108.  
  109.  
  110. //------------------------------------------------------------------------------
  111. // ODxOrderedCollection::AddBefore
  112. //------------------------------------------------------------------------------
  113.  
  114. void ODxOrderedCollection::AddBefore(ElementType existing, ElementType tobeadded)
  115. {
  116.   ODxOCValueLink* * aLink = &fImplementation;
  117.   while (*aLink) {
  118.      ElementType v = (*aLink)->GetValue();
  119.  
  120.      if (this->ElementsMatch(v,existing)) {
  121.        ODxOCValueLink* newLink = CreateNewLink(tobeadded);
  122.        newLink->fNext = *aLink;
  123.        *aLink = newLink;
  124.        return;
  125.      } else
  126.        aLink = &((*aLink)->fNext);
  127.   } /* endwhile */
  128. }
  129.  
  130. //------------------------------------------------------------------------------
  131. // ODxOrderedCollection::AddAfter
  132. //------------------------------------------------------------------------------
  133.  
  134. void ODxOrderedCollection::AddAfter(ElementType existing, ElementType tobeadded)
  135. {
  136.   ODxOCValueLink* aLink = fImplementation;
  137.   while (aLink) {
  138.     ElementType v = aLink->GetValue();
  139.  
  140.     if (this->ElementsMatch(v,existing)) {
  141.       ODxOCValueLink* newLink = CreateNewLink(tobeadded);
  142.       newLink->fNext = aLink->fNext;
  143.       aLink->fNext = newLink;
  144.       return;
  145.     }
  146.     aLink = aLink->fNext;
  147.   }
  148. }
  149.  
  150. //------------------------------------------------------------------------------
  151. // ODxOrderedCollection::After
  152. //------------------------------------------------------------------------------
  153.  
  154. ElementType ODxOrderedCollection::After(ElementType existing)
  155. {
  156.   for (ODxOCValueLink* link = fImplementation; link; link = link->fNext) {
  157.     ElementType v = link->GetValue();
  158.  
  159.     if (this->ElementsMatch(v,existing)) {
  160.        return (link->fNext) ? link->fNext->GetValue() : kODNULL;
  161.     } /* endif */
  162.   }
  163.  
  164.   return (ElementType)kODNULL;
  165. }
  166.  
  167. ODxOCValueLink * ODxOrderedCollection::After(ODxOCValueLink *existing)
  168. {
  169.   for (ODxOCValueLink* link = fImplementation; link; link = link->fNext) {
  170.  
  171.     if (link == existing) return link->fNext;
  172.   }
  173.   return kODNULL;
  174. }
  175.  
  176. #if 1
  177.    //------------------------------------------------------------------------------
  178.    // ODxOrderedCollection::Before
  179.    //------------------------------------------------------------------------------
  180.    
  181.    ElementType ODxOrderedCollection::Before(ElementType existing)
  182.    {
  183.      ODxOCValueLink* linkBefore = kODNULL;
  184.    
  185.      for (ODxOCValueLink* link = fImplementation; link; linkBefore = link, link = link->fNext) {
  186.        ElementType v = link->GetValue();
  187.    
  188.        if (this->ElementsMatch(v,existing)) {
  189.           if (linkBefore) {
  190.              return linkBefore->GetValue();
  191.           } else {
  192.              return kODNULL;  // there is none before
  193.           } /* endif */
  194.        }
  195.      }
  196.    
  197.      return kODNULL;  // element not found
  198.    }
  199.    ODxOCValueLink * ODxOrderedCollection::Before(ODxOCValueLink *existing)
  200.    {
  201.      ODxOCValueLink* linkBefore = kODNULL;
  202.    
  203.      for (ODxOCValueLink* link = fImplementation; link; linkBefore = link, link = link->fNext) {
  204.        if (link == existing) return linkBefore;
  205.      }
  206.    
  207.      return kODNULL;  // element not found
  208.    }
  209. #endif
  210.  
  211. //------------------------------------------------------------------------------
  212. // ODxOrderedCollection::First
  213. //------------------------------------------------------------------------------
  214.  
  215. ElementType ODxOrderedCollection::First()
  216. {
  217.   ODxOCValueLink* firstLink = fImplementation;
  218.   return firstLink ? firstLink->GetValue() : (ElementType)kODNULL;
  219. }
  220.  
  221. ODxOCValueLink * ODxOrderedCollection::FirstVL()
  222. {
  223.    return fImplementation;
  224. }
  225.  
  226. //------------------------------------------------------------------------------
  227. // ODxOrderedCollection::Last
  228. //------------------------------------------------------------------------------
  229.  
  230. ElementType ODxOrderedCollection::Last()
  231. {
  232.   ODxOCValueLink *lastLink = LastVL();
  233.   return (lastLink ? lastLink->GetValue() : (ElementType) kODNULL);
  234. }
  235.  
  236. ODxOCValueLink * ODxOrderedCollection::LastVL()
  237. {
  238.   ODxOCValueLink *lastLink = fImplementation;
  239.   if (lastLink) {
  240.      while (lastLink->fNext) lastLink = lastLink->fNext;
  241.      return lastLink;
  242.   } else {
  243.      return kODNULL;
  244.   } /* endif */
  245. }
  246.  
  247. //------------------------------------------------------------------------------
  248. // ODxOrderedCollection::RemoveFirst
  249. //------------------------------------------------------------------------------
  250.  
  251. ElementType  ODxOrderedCollection::RemoveFirst()
  252. {
  253.   ODxOCValueLink* aLink = fImplementation;
  254.   ElementType value = aLink->GetValue();
  255.   fImplementation = aLink->fNext;
  256.   delete aLink;
  257.   return value;
  258. }
  259.  
  260. //------------------------------------------------------------------------------
  261. // ODxOrderedCollection::RemoveLast
  262. //------------------------------------------------------------------------------
  263.  
  264. ElementType  ODxOrderedCollection::RemoveLast()
  265. {
  266.   if (fImplementation) {
  267.      ODxOCValueLink* *aLink = &fImplementation;
  268.      while ((*aLink)->fNext) aLink = &((*aLink)->fNext);
  269.      ElementType value = (*aLink)->GetValue();
  270.      delete (*aLink);
  271.      *aLink = 0;
  272.      return value;
  273.   } else {
  274.      return (ElementType)kODNULL;
  275.   } /* endif */
  276.   
  277. }
  278.  
  279. //------------------------------------------------------------------------------
  280. // ODxOrderedCollection::Remove
  281. //------------------------------------------------------------------------------
  282.  
  283. void ODxOrderedCollection::Remove(ElementType existing)
  284. {
  285.   ODxOCValueLink* *aLink = &fImplementation;
  286.   while (*aLink) {
  287.     ElementType v = (*aLink)->GetValue();
  288.  
  289.     if (this->ElementsMatch(v,existing)) {
  290.       ODxOCValueLink *vl = *aLink;
  291.       *aLink = vl->fNext;
  292.       delete vl;
  293.       return;
  294.     }
  295.     aLink = &((*aLink)->fNext);
  296.   }
  297. }
  298.  
  299. //------------------------------------------------------------------------------
  300. // ODxOrderedCollection::RemoveAll
  301. //------------------------------------------------------------------------------
  302.  
  303. void ODxOrderedCollection::RemoveAll()
  304. {
  305.   while (fImplementation) {
  306.      ODxOCValueLink* link = fImplementation->fNext;
  307.      delete fImplementation;
  308.      fImplementation = link;
  309.   }
  310. }
  311.  
  312. #if 0
  313.    //------------------------------------------------------------------------------
  314.    // ODxOrderedCollection::DeleteAll
  315.    //------------------------------------------------------------------------------
  316.    
  317.    void ODxOrderedCollection::DeleteAll()
  318.    {
  319.      Link* link = fImplementation.RemoveFirst();
  320.      while (link != kODNULL)
  321.      {
  322.        ElementType value = ((ODxOCValueLink*) link)->GetValue();
  323.        delete value;
  324.        delete link;
  325.        link = fImplementation.RemoveFirst();
  326.      }
  327.    }
  328. #endif
  329.  
  330. //------------------------------------------------------------------------------
  331. // ODxOrderedCollection::Contains
  332. //------------------------------------------------------------------------------
  333.  
  334. ODBoolean  ODxOrderedCollection::Contains(ElementType existing)
  335. {
  336.   ODxOCValueLink* aLink = fImplementation;
  337.   while (aLink) {
  338.     ElementType v = aLink->GetValue();
  339.  
  340.     if (this->ElementsMatch(v,existing)) return kODTrue;
  341.     aLink = aLink->fNext;
  342.   }
  343.   return kODFalse;
  344. }
  345.  
  346. long ODxOrderedCollection::Count() const
  347. {
  348.   ODxOCValueLink* aLink = fImplementation;
  349.   int ct = 0;
  350.   while (aLink) {
  351.     ct++;
  352.     aLink = aLink->fNext;
  353.   }
  354.   return ct;
  355. }
  356.  
  357. //------------------------------------------------------------------------------
  358. // ODxOrderedCollection::CreateIterator
  359. //------------------------------------------------------------------------------
  360.  
  361. ODxOrderedCollectionIterator* ODxOrderedCollection::CreateIterator()
  362. {
  363.   return new ODxOrderedCollectionIterator(this);
  364. }
  365.  
  366.  
  367. //------------------------------------------------------------------------------
  368. // ODxOrderedCollection::CreateNewLink
  369. //------------------------------------------------------------------------------
  370.  
  371. ODxOCValueLink*  ODxOrderedCollection::CreateNewLink(ElementType value) const
  372. {
  373.   return new ODxOCValueLink(value);
  374. }
  375.  
  376. //------------------------------------------------------------------------------
  377. // ODxOrderedCollection::ElementsMatch
  378. //------------------------------------------------------------------------------
  379.  
  380. ODBoolean  ODxOrderedCollection::ElementsMatch(ElementType v1,ElementType v2) const
  381. {
  382.   return (v1 == v2);
  383. }
  384.  
  385. //======================================================================================
  386. // ODxOrderedCollectionIterator
  387. //======================================================================================
  388.  
  389. //------------------------------------------------------------------------------
  390. // ODxOrderedCollectionIterator::ODxOrderedCollectionIterator
  391. //------------------------------------------------------------------------------
  392.  
  393. ODxOrderedCollectionIterator::ODxOrderedCollectionIterator(ODxOrderedCollection* collection)
  394.   : fImplementation(collection ? (collection->fImplementation) : kODNULL)
  395. {
  396.   fCollection =  collection;
  397. }
  398.  
  399. //------------------------------------------------------------------------------
  400. // ODxOrderedCollectionIterator::~ODxOrderedCollectionIterator
  401. //------------------------------------------------------------------------------
  402.  
  403. ODxOrderedCollectionIterator::~ODxOrderedCollectionIterator()
  404. {
  405. }
  406.  
  407. //------------------------------------------------------------------------------
  408. // ODxOrderedCollectionIterator::First
  409. //------------------------------------------------------------------------------
  410.  
  411. ElementType  ODxOrderedCollectionIterator::First()
  412. {
  413.   fImplementation = fCollection->FirstVL();
  414.  
  415.   return fImplementation ? fImplementation->GetValue() : (ElementType)kODNULL;
  416. }
  417.  
  418. //------------------------------------------------------------------------------
  419. // ODxOrderedCollectionIterator::Next
  420. //------------------------------------------------------------------------------
  421.  
  422. ElementType  ODxOrderedCollectionIterator::Next()
  423. {
  424.   ODxOCValueLink* link = fCollection->After(fImplementation);
  425.   fImplementation = link;
  426.   return link ? link->GetValue() : (ElementType)kODNULL;
  427. }
  428.  
  429. //------------------------------------------------------------------------------
  430. // ODxOrderedCollectionIterator::Last
  431. //------------------------------------------------------------------------------
  432.  
  433. ElementType  ODxOrderedCollectionIterator::Last()
  434. {
  435.   ODxOCValueLink* link = fCollection->LastVL();
  436.   fImplementation = link;
  437.   return link ? link->GetValue() : (ElementType)kODNULL;
  438. }
  439.  
  440. //------------------------------------------------------------------------------
  441. // ODxOrderedCollectionIterator::Previous
  442. //------------------------------------------------------------------------------
  443.  
  444. //ElementType  ODxOrderedCollectionIterator::Previous()
  445. //{
  446. //  ODxOCValueLink* link = (ODxOCValueLink*) fImplementation.Previous();
  447. //
  448. //  return link ? link->GetValue() : (ElementType)kODNULL;
  449. //}
  450.  
  451. //------------------------------------------------------------------------------
  452. // ODxOrderedCollectionIterator::IsNotComplete
  453. //------------------------------------------------------------------------------
  454.  
  455. ODBoolean  ODxOrderedCollectionIterator::IsNotComplete()
  456. {
  457.    #if 1
  458.       return (fImplementation && fCollection);
  459.    #else
  460.       if (fImplementation && fCollection) {
  461.          ODxOCValueLink* nextlink = fCollection->After(fImplementation);
  462.          if (nextlink) {
  463.             return kODTrue;
  464.          } else {
  465.             return kODFalse;
  466.          } /* endif */
  467.       } else {
  468.          return kODFalse;
  469.       } /* endif */
  470.    #endif
  471. }
  472.