Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

351 lines
6.6 KiB

  1. // ----------------------------------------------------------------------------
  2. //
  3. // llist.hpp
  4. //
  5. // Author: Sameer Nene
  6. // Date: 05/24/96
  7. // Version: 0.1
  8. // Modification History:
  9. // Bugs:
  10. //
  11. // ----------------------------------------------------------------------------
  12. #ifndef LLIST_INCLUDED
  13. #define LLIST_INCLUDED
  14. //#include <iostream.h>
  15. // forward class declarations
  16. template<class Type> class LListIter;
  17. template<class Type> class LListManip;
  18. template<class Type> class LLink {
  19. // Private members
  20. Type d_data;
  21. LLink<Type> *d_next_p;
  22. // Links cannot be copied or assigned
  23. LLink(const LLink<Type>&);
  24. LLink<Type>& operator=(const LLink<Type>&);
  25. // Public members
  26. public:
  27. // Construct & Destroy
  28. inline LLink(LLink<Type> **addLinkPtr, const Type &data);
  29. ~LLink() {}
  30. // Modifiers
  31. inline void setData(const Type &data);
  32. inline void setNext(LLink<Type> *link);
  33. inline LLink<Type>*& getNextRef(); // generally a bad practice
  34. // Accessors
  35. inline Type& getData();
  36. inline LLink<Type>* getNext() const;
  37. };
  38. template<class Type> class LList {
  39. // Private members
  40. int d_length;
  41. LLink<Type> *d_head_p;
  42. // Friends
  43. friend LListIter<Type>;
  44. friend LListManip<Type>;
  45. // Public members
  46. public:
  47. // Construct & Destroy
  48. LList();
  49. LList(const LList<Type>&);
  50. ~LList();
  51. // Modifiers
  52. LList<Type>& operator=(const LList<Type> &list);
  53. LList<Type>& operator+=(const Type &i);
  54. LList<Type>& operator+=(const LList<Type> &list);
  55. LList<Type>& prepend(const Type &i);
  56. LList<Type>& prepend(const LList<Type> &list);
  57. // Accessors
  58. int length() const;
  59. };
  60. //template<class Type> ostream& operator<<(ostream& o, const LList<Type>& list);
  61. template<class Type> class LListIter {
  62. // Private Data
  63. LLink<Type> *d_current_p;
  64. // Public Members
  65. public:
  66. // Construct and Destroy
  67. inline LListIter(const LList<Type> &list);
  68. inline LListIter(const LListIter<Type> &iter);
  69. ~LListIter() {}
  70. // Modifiers
  71. inline LListIter<Type>& operator=(const LListIter<Type> &iter);
  72. inline void operator++();
  73. // Accessors
  74. inline operator const void* () const;
  75. inline Type& operator()() const;
  76. };
  77. template<class Type> class LListManip {
  78. // Private Data
  79. LList<Type> *d_list_p;
  80. LLink<Type> **d_current_p;
  81. // Links cannot be copied or assigned
  82. LListManip(const LListManip<Type> &manip);
  83. LListManip<Type>& operator=(const LListManip<Type> &manip);
  84. // Public Members
  85. public:
  86. // Construct and Destroy
  87. inline LListManip(LList<Type> *list);
  88. ~LListManip() {}
  89. // Modifiers
  90. inline void operator++();
  91. inline void insert (const Type &data);
  92. inline void remove ();
  93. // Accessors
  94. inline operator const void *() const;
  95. inline Type& operator()() const;
  96. };
  97. template<class Type> LLink<Type>::LLink(LLink<Type> **addLinkPtr, const Type &data) : d_next_p(*addLinkPtr), d_data(data)
  98. {
  99. *addLinkPtr = this;
  100. }
  101. template<class Type> void LLink<Type>::setData(const Type &data)
  102. {
  103. d_data = data;
  104. }
  105. template<class Type> void LLink<Type>::setNext(LLink<Type> *link)
  106. {
  107. d_next_p = link;
  108. }
  109. template<class Type> LLink<Type>*& LLink<Type>::getNextRef()
  110. {
  111. return d_next_p;
  112. }
  113. template<class Type> Type& LLink<Type>::getData()
  114. {
  115. return d_data;
  116. }
  117. template<class Type> LLink<Type>* LLink<Type>::getNext() const
  118. {
  119. return d_next_p;
  120. }
  121. template<class Type> LList<Type>::LList() : d_head_p(0), d_length(0)
  122. {
  123. }
  124. template<class Type> LList<Type>::LList(const LList<Type>& list) : d_head_p(0)
  125. {
  126. LListManip<Type> m(this);
  127. LListIter<Type> l(list);
  128. while(l) {
  129. m.insert(l());
  130. ++l;
  131. ++m;
  132. }
  133. }
  134. template<class Type> LList<Type>::~LList()
  135. {
  136. LListManip<Type> m(this);
  137. while(m != 0)
  138. m.remove();
  139. }
  140. template<class Type> LList<Type>& LList<Type>::operator=(const LList<Type>& list)
  141. {
  142. LListManip<Type> m(this);
  143. LListIter<Type> l(list);
  144. if(this != &list) {
  145. while(m)
  146. m.remove();
  147. while(l) {
  148. m.insert(l());
  149. ++l;
  150. ++m;
  151. }
  152. }
  153. return *this;
  154. }
  155. template<class Type> LList<Type>& LList<Type>::operator+=(const Type &i)
  156. {
  157. LListManip<Type> m(this);
  158. while(m)
  159. ++m;
  160. m.insert(i);
  161. return *this;
  162. }
  163. template<class Type> LList<Type>& LList<Type>::operator+=(const LList<Type>& list)
  164. {
  165. unsigned i, s;
  166. LListIter<Type> l(list);
  167. LListManip<Type> m(this);
  168. while(m)
  169. ++m;
  170. s = list.d_length;
  171. for(i = 0; i < s; ++i) {
  172. m.insert(l());
  173. ++m;
  174. ++l;
  175. }
  176. return *this;
  177. }
  178. template<class Type> LList<Type>& LList<Type>::prepend(const Type &i)
  179. {
  180. LListManip<Type> m(this);
  181. m.insert(i);
  182. return *this;
  183. }
  184. template<class Type> LList<Type>& LList<Type>::prepend(const LList<Type> &list)
  185. {
  186. LListIter<Type> l(list);
  187. LListManip<Type> m(this);
  188. while(l) {
  189. m.insert(l());
  190. ++m;
  191. ++l;
  192. }
  193. return *this;
  194. }
  195. template<class Type> int LList<Type>::length() const
  196. {
  197. return d_length;
  198. }
  199. //template<class Type> ostream& operator<<(ostream &o, const LList<Type>& list)
  200. //{
  201. // LListIter<Type> l(list);
  202. //
  203. // o << "[ ";
  204. // while(l != 0) {
  205. // o << l();
  206. // o << " ";
  207. // ++l;
  208. // }
  209. // return o << "]";
  210. //}
  211. template<class Type> LListIter<Type>::LListIter(const LList<Type> &list) : d_current_p(list.d_head_p)
  212. {
  213. }
  214. template<class Type> LListIter<Type>::LListIter(const LListIter<Type> &iter) : d_current_p(iter.d_current_p)
  215. {
  216. }
  217. template<class Type> LListIter<Type>& LListIter<Type>::operator=(const LListIter<Type> &iter)
  218. {
  219. d_current_p = iter.d_current_p;
  220. return *this;
  221. }
  222. template<class Type> void LListIter<Type>::operator++()
  223. {
  224. d_current_p = d_current_p -> getNext();
  225. }
  226. template<class Type> Type& LListIter<Type>::operator()() const
  227. {
  228. return d_current_p -> getData();
  229. }
  230. template<class Type> LListIter<Type>::operator const void* () const
  231. {
  232. return d_current_p;
  233. }
  234. template<class Type> LListManip<Type>::LListManip(LList<Type> *list) : d_current_p(&(list -> d_head_p)), d_list_p(list)
  235. {
  236. }
  237. template<class Type> void LListManip<Type>::operator++()
  238. {
  239. d_current_p = &((*d_current_p) -> getNextRef());
  240. }
  241. template<class Type> void LListManip<Type>::insert(const Type &data)
  242. {
  243. new LLink<Type>(d_current_p, data);
  244. ++(d_list_p -> d_length);
  245. }
  246. template<class Type> void LListManip<Type>::remove()
  247. {
  248. LLink<Type> *t = *d_current_p;
  249. *d_current_p = (*d_current_p) -> getNext();
  250. delete t;
  251. --(d_list_p -> d_length);
  252. }
  253. template<class Type> LListManip<Type>::operator const void* () const
  254. {
  255. return *d_current_p;
  256. }
  257. template<class Type> Type& LListManip<Type>::operator()() const
  258. {
  259. return (*d_current_p) -> getData();
  260. }
  261. #endif