home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0040 - 0049 / ibm0040-0049 / ibm0040.tar / ibm0040 / ZINC_6.ZIP / DOSSRC.ZIP / LIST.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-01  |  5.0 KB  |  210 lines

  1. //    Zinc Interface Library - LIST.CPP
  2. //    COPYRIGHT (C) 1990, 1991.  All Rights Reserved.
  3. //    Zinc Software Incorporated.  Pleasant Grove, Utah  USA
  4.  
  5. #include "ui_gen.hpp"
  6.  
  7. // THIS COPYRIGHT NOTICE CANNOT BE REMOVED!
  8. static char *_copyright = "\
  9. COPYRIGHT (C) 1990, 1991. \
  10. All Rights Reserved. \
  11. Zinc Software Incorporated.  Pleasant Grove, Utah  USA.";
  12.  
  13. UI_ELEMENT *UI_LIST::Add(UI_ELEMENT *newElement)
  14. {
  15.     // See if it is a default insertion.
  16.     if (!first || !compareFunction)
  17.         return (UI_LIST::Add(0, newElement));
  18.  
  19.     // Get the beginning positions.
  20.     int weight;
  21.     UI_ELEMENT *element1, *element2;
  22.     UI_ELEMENT *beginElement = first;
  23.     UI_ELEMENT *endElement = last;
  24.  
  25.     // Shortcuts for elements in special orders.
  26.     if ((*compareFunction)(endElement, newElement) <= 0)
  27.         beginElement = endElement;
  28.     else if ((*compareFunction)(beginElement, newElement) >= 0)
  29.         beginElement = endElement = 0;
  30.  
  31.     // Find the proper element position.
  32.     while (beginElement != endElement)
  33.     {
  34.         // Get the middle element - The order here is important!.
  35.         element1 = beginElement;
  36.         for (element2 = endElement; element1 != element2;
  37.             element2 = element2->previous)
  38.             if (element1->next != element2)
  39.                 element1 = element1->next;
  40.  
  41.         // Increment the proper element position.
  42.         weight = (*compareFunction)(element1, newElement);
  43.         if (weight > 0 && endElement != element1)
  44.             endElement = element1;
  45.         else if (weight < 0 && beginElement != element1)
  46.             beginElement = element1;
  47.         else
  48.             beginElement = endElement = element1;
  49.     }
  50.  
  51.     // Place the element in the sorted list.
  52.     if (endElement)                    // Insert after endElement.
  53.     {
  54.         newElement->previous = endElement;
  55.         newElement->next = endElement->next;
  56.         if (newElement->next)
  57.             newElement->next->previous = newElement;
  58.         endElement->next = newElement;
  59.         if (last == endElement)
  60.             last = newElement;
  61.     }
  62.     else                            // Insert at beginning of the list.
  63.     {
  64.         newElement->previous = 0;
  65.         newElement->next = first;
  66.         first->previous = newElement;
  67.         first = newElement;
  68.     }
  69.  
  70.     // Return a pointer to the element.
  71.     return (newElement);
  72. }
  73.  
  74. UI_ELEMENT *UI_LIST::Add(UI_ELEMENT *element, UI_ELEMENT *newElement)
  75. {
  76.     // Add the element to the list.
  77.     if (!first)
  78.     {
  79.         newElement->previous = newElement->next = 0;
  80.         first = last = newElement;
  81.     }
  82.     else if (!element)
  83.     {
  84.         newElement->previous = last;
  85.         newElement->next = 0;
  86.         last->next = newElement;
  87.         last = newElement;
  88.     }
  89.     else
  90.     {
  91.         newElement->previous = element->previous;
  92.         newElement->next = element;
  93.         if (!element->previous)
  94.             first = newElement;
  95.         else
  96.             element->previous->next = newElement;
  97.         element->previous = newElement;
  98.     }
  99.  
  100.     // Return a pointer to the element.
  101.     return (newElement);
  102. }
  103.  
  104. int UI_LIST::Count()
  105. {
  106.     int count = 0;
  107.  
  108.     // Get the element index.
  109.     for (UI_ELEMENT *element = first; element; element = element->next)
  110.         count++;
  111.  
  112.     // Return the count.
  113.     return (count);
  114. }
  115.  
  116. UI_ELEMENT *UI_LIST::Subtract(UI_ELEMENT *element)
  117. {
  118.     // Make sure the element is in the list.
  119.     UI_ELEMENT *tElement = element;
  120.     while (tElement->previous)
  121.         tElement = tElement->previous;
  122.     if (tElement != first)
  123.         return (element);
  124.  
  125.     // Delete the specified element from the list.
  126.     if (element->previous)
  127.         element->previous->next = element->next;
  128.     else
  129.         first = element->next;
  130.     if (element->next)
  131.         element->next->previous = element->previous;
  132.     else
  133.         last = element->previous;
  134.     if (current == element)
  135.         current = first;
  136.  
  137.     // Return the next element.
  138.     return (element->next);
  139. }
  140.  
  141. void UI_LIST::Destroy()
  142. {
  143.     UI_ELEMENT *tElement;
  144.  
  145.     // Delete all the elements in the list.
  146.     for (UI_ELEMENT *element = first; element; )
  147.     {
  148.         tElement = element;
  149.         element = element->next;
  150.         delete tElement;
  151.     }
  152.     first = last = current = 0;
  153. }
  154.  
  155. UI_ELEMENT *UI_LIST::Get(int index)
  156. {
  157.     // Get the list element.
  158.     for (UI_ELEMENT *element = first; index && element; element = element->next)
  159.         index--;
  160.  
  161.     // Return the element.
  162.     return (element);
  163. }
  164.  
  165. UI_ELEMENT *UI_LIST::Get(int (*findFunction)(void *element1, void *matchData), void *matchData)
  166. {
  167.     // Try to find the list element.
  168.     for (UI_ELEMENT *element = first;
  169.         element && (*findFunction)(element, matchData);
  170.         element = element->next)
  171.         ;
  172.  
  173.     // Return the element.
  174.     return (element);
  175. }
  176.  
  177. int UI_LIST::Index(const UI_ELEMENT *element)
  178. {
  179.     // Get the element index.
  180.     int index = 0;
  181.     for (UI_ELEMENT *tElement = first; tElement && tElement != element; 
  182.         tElement = tElement->next)
  183.         index++;
  184.  
  185.     // Return the index.
  186.     if (tElement)
  187.         return (index);
  188.     return (-1);
  189. }
  190.  
  191. void UI_LIST::Sort()
  192. {
  193.     // Make sure there are elements in the list and a comparison routine.
  194.     if (!first || !compareFunction)
  195.         return;
  196.  
  197.     // Start the list with the first element.
  198.     UI_ELEMENT *element;
  199.     UI_ELEMENT *addElement = first;
  200.     first = last = 0;
  201.  
  202.     // Sort the list of elements using the flat binary insertion.
  203.     while (addElement)
  204.     {
  205.         element = addElement->next;
  206.         UI_LIST::Add(addElement);
  207.         addElement = element;
  208.     }
  209. }
  210.