Team Fortress 2 Source Code as on 22/4/2020
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.

243 lines
3.6 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #include <stdio.h>
  9. #include <malloc.h>
  10. #include "ObjectList.h"
  11. #include "tier1/strtools.h"
  12. //#include "port.h"
  13. //#include "mem.h"
  14. // memdbgon must be the last include file in a .cpp file!!!
  15. #include <tier0/memdbgon.h>
  16. //////////////////////////////////////////////////////////////////////
  17. // Construction/Destruction
  18. //////////////////////////////////////////////////////////////////////
  19. ObjectList::ObjectList()
  20. {
  21. head = tail = current = NULL;
  22. number = 0;
  23. }
  24. ObjectList::~ObjectList()
  25. {
  26. Clear( false );
  27. }
  28. bool ObjectList::AddHead(void * newObject)
  29. {
  30. // create new element
  31. element_t * newElement = (element_t *) calloc(1, sizeof(element_t));
  32. if (newElement == NULL )
  33. return false; // out of memory
  34. // insert element
  35. newElement->object = newObject;
  36. if (head)
  37. {
  38. newElement->next = head;
  39. head->prev = newElement;
  40. };
  41. head = newElement;
  42. // if list was empty set new tail
  43. if (tail==NULL) tail = head;
  44. number++;
  45. return true;
  46. }
  47. void * ObjectList::RemoveHead()
  48. {
  49. void * retObj;
  50. // check head is present
  51. if (head)
  52. {
  53. retObj = head->object;
  54. element_t * newHead = head->next;
  55. if (newHead) newHead->prev = NULL;
  56. // if only one element is in list also update tail
  57. // if we remove this prev element
  58. if (tail==head) tail = NULL;
  59. free(head);
  60. head = newHead;
  61. number--;
  62. } else
  63. retObj = NULL;
  64. return retObj;
  65. }
  66. bool ObjectList::AddTail(void * newObject)
  67. {
  68. element_t * newElement = (element_t *) calloc(1, sizeof(element_t));
  69. if (newElement == NULL)
  70. return false; // out of memory;
  71. newElement->object = newObject;
  72. if (tail)
  73. {
  74. newElement->prev = tail;
  75. tail->next = newElement;
  76. }
  77. tail = newElement;
  78. // if list was empty set new head
  79. if (head==NULL) head = tail;
  80. number++;
  81. return true;
  82. }
  83. void * ObjectList::RemoveTail()
  84. {
  85. void * retObj;
  86. // check tail is present
  87. if (tail)
  88. {
  89. retObj = tail->object;
  90. element_t * newTail = tail->prev;
  91. if (newTail) newTail->next = NULL;
  92. // if only one element is in list also update tail
  93. // if we remove this prev element
  94. if (head==tail) head = NULL;
  95. free(tail);
  96. tail = newTail;
  97. number--;
  98. } else
  99. retObj = NULL;
  100. return retObj;
  101. }
  102. bool ObjectList::IsEmpty()
  103. {
  104. return ( head == NULL );
  105. }
  106. int ObjectList::CountElements()
  107. {
  108. return number;
  109. }
  110. bool ObjectList::Contains(void * object)
  111. {
  112. element_t * e = head;
  113. while(e && e->object!=object) { e = e->next;}
  114. if ( e )
  115. {
  116. current = e;
  117. return true;
  118. }
  119. else
  120. {
  121. return false;
  122. }
  123. }
  124. void ObjectList::Clear( bool freeElementsMemory )
  125. {
  126. element_t * ne;
  127. element_t * e = head;
  128. while(e)
  129. {
  130. ne = e->next;
  131. if ( freeElementsMemory && e->object )
  132. free( e->object );
  133. free(e);
  134. e = ne;
  135. }
  136. head = tail = current = NULL;
  137. number = 0;
  138. }
  139. bool ObjectList::Remove( void * object )
  140. {
  141. element_t * e = head;
  142. while(e && e->object!=object) { e = e->next;}
  143. if (e!=NULL)
  144. {
  145. if (e->prev) e->prev->next = e->next;
  146. if (e->next) e->next->prev = e->prev;
  147. if (head==e) head = e->next;
  148. if (tail==e) tail = e->prev;
  149. if (current == e) current= e->next;
  150. free(e);
  151. number--;
  152. }
  153. return (e!=NULL);
  154. }
  155. void ObjectList::Init()
  156. {
  157. head = tail = current = NULL;
  158. number = 0;
  159. }
  160. void * ObjectList::GetFirst()
  161. {
  162. if (head)
  163. {
  164. current = head->next;
  165. return head->object;
  166. }
  167. else
  168. {
  169. current = NULL;
  170. return NULL;
  171. };
  172. }
  173. void * ObjectList::GetNext()
  174. {
  175. void * retObj = NULL;
  176. if (current)
  177. {
  178. retObj = current->object;
  179. current = current->next;
  180. }
  181. return retObj;
  182. }
  183. bool ObjectList::Add(void *newObject)
  184. {
  185. return AddTail( newObject );
  186. }