home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / modules / libfont / src / wfList.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  6.0 KB  |  313 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19.  
  20. // wfList.cpp
  21. // A doubly linked list manager
  22. //
  23. // Created by dp Suresh <dp@netscape.com>
  24.  
  25. #ifdef TEST
  26. #include <stream.h>
  27. #endif /* TEST */
  28.  
  29.  
  30. #include <string.h>         // for NULL
  31.  
  32. #include "wfList.h"
  33.  
  34.  
  35. //-------------//
  36. // Constructor //
  37. //-------------//
  38.  
  39. wfList::wfList(WF_FREE_FUNC freeFunc)
  40. : head(NULL), tail(NULL), freeItemFunc(freeFunc)
  41. {
  42. }
  43.  
  44.  
  45. //------------//
  46. // Destructor //
  47. //------------//
  48.  
  49. wfList::~wfList()
  50. {
  51.     removeAll();
  52. }
  53.  
  54.  
  55. //----------------------------------//
  56. // Implementation of public methods //
  57. //----------------------------------//
  58.  
  59.  
  60. wfList::ERROR_CODE
  61. wfList::add(void *item)
  62. {
  63.     struct wfListElement *ele = new (struct wfListElement);
  64.     
  65.     if (!ele) return (NO_MEMORY);
  66.     
  67.     ele->item = item;
  68.     ele->next = NULL;
  69.     ele->prev = NULL;
  70.     
  71.     ERROR_CODE ret = listAdd(ele);
  72.     if (ret != SUCCESS)
  73.       {
  74.         delete ele;
  75.       }
  76.     
  77.     return (ret);
  78. }
  79.  
  80. wfList::ERROR_CODE
  81. wfList::remove (void *item)
  82. {
  83.     struct wfListElement *ele = find(item);
  84.     if (ele == NULL)
  85.       {
  86.         return (NOT_FOUND);
  87.       }
  88.     
  89.     ERROR_CODE ret = listRemove(ele);
  90.     if (ret == SUCCESS)
  91.       {
  92.         listDeleteItem(ele->item);
  93.         delete ele;
  94.       }
  95.     return (ret);
  96. }
  97.  
  98. wfList::ERROR_CODE
  99. wfList::removeAll (void)
  100. {
  101.     ERROR_CODE ret = SUCCESS;
  102.     while (head && ret == SUCCESS)
  103.       {
  104.         wfListElement *ele = head;
  105.         ERROR_CODE ret = listRemove(ele);
  106.         if (ret != SUCCESS)
  107.           {
  108.             break;
  109.           }
  110.         listDeleteItem(ele->item);
  111.         delete ele;
  112.       }
  113.     return (ret);
  114. }
  115.  
  116. wfList::ERROR_CODE
  117. wfList::isEmpty()
  118. {
  119.     return ((head == NULL) ? WF_TRUE : WF_FALSE);
  120. }
  121.  
  122. wfList::ERROR_CODE
  123. wfList::isExist(void *item)
  124. {
  125.     wfListElement *tmp = find(item);
  126.     return((tmp != NULL) ? WF_TRUE : WF_FALSE);
  127. }
  128.  
  129. wfList::ERROR_CODE
  130. wfList::iterate(IteratorFunction fn)
  131. {
  132.     struct wfListElement *tmp = head;
  133.     if (!fn) return(GENERIC_ERROR);
  134.     if (!tmp) return(GENERIC_ERROR);
  135.     
  136.     while (tmp)
  137.       {
  138.         void *item = tmp->item;
  139.         struct wfListElement *next = tmp->next;
  140.         
  141.         ERROR_CODE ret = (*fn)(item);
  142.         switch (ret) {
  143.         case GENERIC_ERROR:
  144.           return (ret);
  145.         case SUCCESS:
  146.         default:
  147.           break;
  148.         }
  149.         
  150.         tmp = next;
  151.       }
  152.     return (SUCCESS);
  153. }
  154.  
  155. int
  156. wfList::count(void)
  157. {
  158.     wfListElement *tmp = head;
  159.     int n = 0;
  160.     for (; tmp; tmp = tmp->next)
  161.       {
  162.         n++;
  163.       }
  164.     return (n);
  165. }
  166.  
  167. //-----------------------------------//
  168. // Implementation of Private methods //
  169. //-----------------------------------//
  170.  
  171. wfList::ERROR_CODE
  172. wfList::listAdd(struct wfListElement *ele)
  173. {
  174.     if (isEmpty() == WF_TRUE)
  175.       {
  176.         // Empty list
  177.         head = tail = ele;
  178.       }
  179.     else
  180.       {
  181.         // Add at the tail
  182.         tail->next = ele;
  183.         ele->prev = tail;
  184.         tail = ele;
  185.       }
  186.     return (SUCCESS);
  187. }
  188.  
  189.  
  190. wfList::ERROR_CODE
  191. wfList::listRemove (struct wfListElement *ele)
  192. {
  193.     if (ele->prev)
  194.       {
  195.         // Not the first element
  196.         ele->prev->next = ele->next;
  197.       }
  198.     else
  199.       {
  200.         // First element
  201.         head = ele->next;
  202.       }
  203.     
  204.     if (ele->next)
  205.       {
  206.         // Not the last element
  207.         ele->next->prev = ele->prev;
  208.       }
  209.     else
  210.       {
  211.         // Last element
  212.         tail = ele->prev;
  213.       }
  214.     return (SUCCESS);
  215. }
  216.  
  217. void
  218. wfList::listDeleteItem(void *item)
  219. {
  220.     if (freeItemFunc != NULL)
  221.     {
  222.         (*freeItemFunc)(this, item);
  223.     }
  224.     return;
  225. }
  226.  
  227.  
  228. struct wfListElement *
  229. wfList::find(void *item)
  230. {
  231.     wfListElement *tmp = head;
  232.     while (tmp && tmp->item != item) tmp = tmp->next;
  233.     return (tmp);
  234. }
  235.  
  236.  
  237. #ifdef TEST
  238. wfList::ERROR_CODE
  239. print(void *item)
  240. {
  241.     cout << "--> " << (int)item << " ";
  242.     return (wfList::SUCCESS);
  243. }
  244.  
  245. main()
  246. {
  247.     wfList l;
  248.  
  249.     cout << "isEmpty() : " << l.isEmpty() << "\n";
  250.     cout << "isExist(5): " << l.isExist((void *)5) << "\n";
  251.     cout << "remove(5) : " << l.remove((void *)5) << "\n";
  252.     cout << "count() : " << l.count() << "\n";
  253.     l.iterate(print); cout << "\n";
  254.  
  255.     cout << "add(5) : " << l.add((void *)5) << "\n";
  256.     cout << "add(6) : " << l.add((void *)6) << "\n";
  257.     cout << "add(7) : " << l.add((void *)7) << "\n";
  258.     cout << "add(8) : " << l.add((void *)8) << "\n";
  259.     cout << "add(9) : " << l.add((void *)9) << "\n";
  260.     cout << "isEmpty() : " << l.isEmpty() << "\n";
  261.     cout << "count() : " << l.count() << "\n";
  262.  
  263.     l.iterate(print); cout << "\n";
  264.  
  265.     cout << "isExist(5): " << l.isExist((void *)5) << "\n";
  266.     cout << "isExist(8): " << l.isExist((void *)8) << "\n";
  267.     cout << "isExist(0xCAFE): " << l.isExist((void *)0xCAFE) << "\n";
  268.     cout << "isExist(9): " << l.isExist((void *)9) << "\n";
  269.  
  270.     cout << "remove(5) : " << l.remove((void *)5) << "\n";
  271.     l.iterate(print); cout << "\n";
  272.     cout << "remove(8) : " << l.remove((void *)8) << "\n";
  273.     l.iterate(print); cout << "\n";
  274.     cout << "remove(0xCAFE) : " << l.remove((void *)0xCAFE) << "\n";
  275.     l.iterate(print); cout << "\n";
  276.     cout << "remove(9) : " << l.remove((void *)9) << "\n";
  277.     cout << "count() : " << l.count() << "\n";
  278.     l.iterate(print); cout << "\n";
  279.  
  280.     return(0);
  281. }
  282.  
  283. #if 0
  284. // The above test should yeild this result
  285. isEmpty() : 1
  286. isExist(5): 0
  287. remove(5) : 4
  288.  
  289. add(5) : 2
  290. add(6) : 2
  291. add(7) : 2
  292. add(8) : 2
  293. add(9) : 2
  294. isEmpty() : 0
  295. --> 5 --> 6 --> 7 --> 8 --> 9 
  296. isExist(5): 1
  297. isExist(8): 1
  298. isExist(0xCAFE): 0
  299. isExist(9): 1
  300. remove(5) : 2
  301. --> 6 --> 7 --> 8 --> 9 
  302. remove(8) : 2
  303. --> 6 --> 7 --> 9 
  304. remove(0xCAFE) : 4
  305. --> 6 --> 7 --> 9 
  306. remove(9) : 2
  307. --> 6 --> 7 
  308. wfList::delete (deleting 6...)
  309. wfList::delete (deleting 7...)
  310. #endif /* 0 */
  311. #endif /* TEST */
  312.  
  313.