123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444 |
- /* -*- Mode: C++; tab-width: 3; indent-tabs-mode: t; c-basic-offset: 3 -*- */
- /*================================================================
- *
- * Project : Freyja
- * Author : Mongoose
- * Website : http://www.westga.edu/~stu7440/
- * Email : stu7440@westga.edu
- * Object : List
- * License : No use w/o permission (C) 2000 Mongoose
- * Comments: mtk Template list
- *
- * UINT_MAX is an error condition, used in place of -1
- *
- * This file was generated using Mongoose's C++
- * template generator script. <stu7440@westga.edu>
- *
- *-- History ------------------------------------------------
- *
- * 2000-10-26:
- * Mongoose - Created
- ================================================================*/
-
-
- #ifndef _LIST_H_
- #define _LIST_H_
-
- #include <stdlib.h>
- #include <limits.h>
- #include <stdio.h>
-
- #ifdef DEBUG_MEMORY
- #include <memory_test.h>
- #endif
-
-
- template <class T> class ListNode
- {
- public:
-
- ListNode(T data, unsigned int id)
- {
- _data = data;
- _id = id;
- _next = NULL;
- }
-
- ~ListNode()
- {
- }
-
- void Id(unsigned int id)
- {
- _id = id;
- }
-
- unsigned int Id()
- {
- return _id;
- }
-
- void Data(T data)
- {
- _data = data;
- }
-
- T Data()
- {
- return _data;
- }
-
- ListNode<T> *Next()
- {
- return _next;
- }
-
- void Next(ListNode<T> *next)
- {
- _next = next;
- }
-
- void Print()
- {
- //printf("(%i, %p)", _id, _data);
- printf("%i", _id);
- }
-
- private:
-
- ListNode<T> *_next;
- unsigned int _id;
- T _data;
- };
-
-
- template <class T> class List
- {
- public:
-
- List()
- {
- _num_items = 0;
- _head = NULL;
- _current = NULL;
- _last = NULL;
- _cache = NULL;
- }
-
- ~List()
- {
- Clear();
- }
-
- // NOTE: this only copies data, the containers aren't the same ids
- void Copy(List<T> *list)
- {
- if (!list)
- return;
-
- for (list->Reset(); list->CurrentExists(); list->Next())
- Add(list->Current());
- }
-
- void Clear()
- {
- _num_items = 0;
- _last = _cache = NULL;
-
- while (_head)
- {
- _current = _head;
- _head = _head->Next();
- delete _current;
- }
- }
-
- T SearchId(unsigned int id)
- {
- ListNode<T> *current = _head;
- ListNode<T> *last = NULL;
-
- if (_cache)
- {
- if (id >= _cache->Id())
- current = _cache;
- }
-
- while (current)
- {
- // Found
- if (current->Id() == id)
- {
- _cache = current;
- return current->Data();
- }
-
- last = current;
- current = current->Next();
- }
-
- return 0;
- }
-
- unsigned int SearchKey(T data)
- {
- ListNode<T> *current = _head;
- ListNode<T> *last = NULL;
-
-
- if (_cache)
- {
- // Mongoose: 2001-01-31, hhmmm... fixed?
- if (data == _cache->Data())
- return _cache->Id();
- }
-
- while (current)
- {
- // Found
- if (current->Data() == data)
- {
- _cache = current;
- return current->Id();
- }
-
- last = current;
- current = current->Next();
- }
-
- return UINT_MAX;
- }
-
- T operator [] (unsigned int i)
- {
- if (_head)
- {
- return SearchId(i);
- }
- else
- {
- #ifdef DEBUG_INDEX_EMPTY_LIST
- printf("List[%i] = NULL\n", i);
- #endif
- }
-
- return 0;
- }
-
-
- void RemoveId(unsigned int id)
- {
- ListNode<T> *current = _head;
- ListNode<T> *last = NULL;
-
-
- _last = _cache = NULL;
-
- while (current)
- {
- // Remove
- if (current->Id() == id)
- {
- if (current == _head)
- _head = current->Next();
- else
- {
- last->Next(current->Next());
- }
-
- if (_current == current)
- _current = NULL;
-
- delete current;
- _num_items--;
-
- return;
- }
-
- last = current;
- current = current->Next();
- }
- }
-
- void Remove(T data)
- {
- ListNode<T> *current = _head;
- ListNode<T> *last = NULL;
-
-
- _last = _cache = NULL;
-
- while (current)
- {
- // Remove
- if (current->Data() == data)
- {
- if (current == _head)
- _head = current->Next();
- else
- {
- last->Next(current->Next());
- }
-
- if (_current == current)
- _current = NULL;
-
- delete current;
- _num_items--;
-
- return;
- }
-
- last = current;
- current = current->Next();
- }
- }
-
- bool Empty()
- {
- return (_head == NULL);
- }
-
- unsigned int NumItems()
- {
- return _num_items;
- }
-
-
- void Print(void (*print_func)(T))
- {
- ListNode<T> *current = _head;
-
- printf(" [%i] {\n", _num_items);
-
-
- while (current)
- {
- printf("#%i, ", current->Id());
-
- if (print_func)
- (*print_func)(current->Data());
-
- current = current->Next();
-
- fflush(stdout);
- }
-
- printf(" }\n");
- }
-
-
- void Print()
- {
- ListNode<T> *current = _head;
-
- printf("List %i {\n", _num_items);
-
-
- while (current)
- {
- //current->Print();
- printf("%i", current->Id());
-
- current = current->Next();
-
- if (current)
- printf(", ");
-
- fflush(stdout);
- }
-
- printf(" }\n");
- }
-
- void Reset()
- {
- _current = _head;
- _cache = _head;
- }
-
- bool operator ++ (int dummy)
- {
- return Next();
- }
-
- bool Next()
- {
- if (_current)
- _current = _current->Next();
-
- return (_current != NULL);
- }
-
- unsigned int CurrentId()
- {
- if (!_current)
- return UINT_MAX;
-
- return _current->Id();
- }
-
- bool CurrentExists()
- {
- return (_current != 0);
- }
-
- T Current()
- {
- if (_current)
- return _current->Data();
- else
- return 0;
- }
-
- unsigned int Add(T data)
- {
- ListNode<T> *node;
-
-
- node = new ListNode<T>(data, _num_items++);
- return Add(node);
- }
-
- unsigned int Add(ListNode<T> *node)
- {
- ListNode<T> *current;
- ListNode<T> *last;
- unsigned int i;
-
-
- if (_head)
- {
- current = _head;
- last = NULL;
- i = 0;
-
- //EXP
- if (_last)
- {
- i = _last->Id();
- current = _last;
- }
-
- while (current)
- {
- // Prepend
- if (current->Id() > i)
- {
- node->Id(i);
- node->Next(current);
-
- if (current == _head)
- _head = node;
- else if (last)
- last->Next(node);
-
- return node->Id();
- }
-
- i++;
- last = current;
- current = current->Next();
- }
-
- // Append
- last->Next(node);
- }
- else
- _head = node;
-
- _last = node; //EXP
-
- return node->Id();
- }
-
- private:
-
- unsigned int _num_items;
- ListNode<T> *_head;
- ListNode<T> *_current;
- ListNode<T> *_last;
- ListNode<T> *_cache;
- };
-
- #endif
|