00001 /*========================================================================== 00002 * Copyright (c) 2001 Carnegie Mellon University. All Rights Reserved. 00003 * 00004 * Use of the Lemur Toolkit for Language Modeling and Information Retrieval 00005 * is subject to the terms of the software license set forth in the LICENSE 00006 * file included with this software, and also available at 00007 * http://www.cs.cmu.edu/~lemur/license.html 00008 * 00009 *========================================================================== 00010 */ 00011 00012 00013 #ifndef _FASTLISTH_ 00014 #define _FASTLISTH_ 00015 00016 #include <cstdlib> 00017 #include <cassert> 00018 #include <Null.hpp> 00019 #include <MemList.hpp> 00020 #include <FLL.hpp> 00021 00022 template <class Type> 00023 class FastList { 00024 public: 00025 FastList() : head(NULL), tail(NULL) {} // default constructor 00026 ~FastList() {} // destructor does nothing 00027 void Clear() { head = tail = NULL; } // clear list without deleting 00028 00029 int operator!() const { return head == NULL; } 00030 00031 FastList<Type> &AddToHead(Type &d) 00032 { 00033 if (!head) { head = tail = new FLL<Type>(d); } 00034 else 00035 { FLL<Type> *tmp = new FLL<Type>(d); tmp->next = head; head = tmp; } 00036 return *this; 00037 } 00038 00039 FastList<Type> &AddToTail(Type &d) 00040 { 00041 if (!tail) { 00042 tail = head = new FLL<Type>(d); 00043 } 00044 else { 00045 tail->next = new FLL<Type>(d); tail = tail->next; 00046 } 00047 return *this; 00048 } 00049 00050 Type &PopHead() 00051 { 00052 if (head == NULL) return null(Type); 00053 Type &d = head->Data(); 00054 head = head->next; 00055 if (head == NULL) 00056 tail = NULL; 00057 return d; 00058 } 00059 00060 int ComputeLength() const 00061 { int l=0; for (FLL<Type> *ll=head; ll; ll=ll->Next()) l++; return l; } 00062 00063 // Accessor Functions 00064 FLL<Type> *HeadLL() const { return head; } 00065 00066 private: 00067 FLL<Type> *head; 00068 FLL<Type> *tail; 00069 00070 public: // Globals 00071 // Memory management 00072 void *operator new(size_t size) { return pMemList->GetMem(size); } 00073 void operator delete(void *) { } 00074 static void UseMemory(MemList &memList) 00075 { 00076 FLL<Type>::UseMemory(memList); 00077 pMemList = &memList; 00078 } 00079 private: 00080 static MemList *pMemList; 00081 }; 00082 00083 #define FMAPCAR(list, ll) for (ll = (list).HeadLL(); ll; ll = ll->Next()) 00084 00085 #endif