Counter Strike : Global Offensive Source Code
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.

247 lines
3.7 KiB

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