List.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. ** Command & Conquer Generals(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. ////////////////////////////////////////////////////////////////////////////////
  19. // //
  20. // (c) 2001-2003 Electronic Arts Inc. //
  21. // //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. //----------------------------------------------------------------------------=
  24. //
  25. // Westwood Studios Pacific.
  26. //
  27. // Confidential Information
  28. // Copyright(C) 2001 - All Rights Reserved
  29. //
  30. //----------------------------------------------------------------------------
  31. //
  32. // Project: WSYS Library
  33. //
  34. // Module:
  35. //
  36. // File name: wsys/List.h
  37. //
  38. // Created: 10/31/01
  39. //
  40. //----------------------------------------------------------------------------
  41. #pragma once
  42. #ifndef __WSYS_LIST_H
  43. #define __WSYS_LIST_H
  44. //----------------------------------------------------------------------------
  45. // Includes
  46. //----------------------------------------------------------------------------
  47. #ifndef _BASE_TYPE_H_
  48. #include <lib/BaseType.h>
  49. #endif
  50. //----------------------------------------------------------------------------
  51. // Forward References
  52. //----------------------------------------------------------------------------
  53. //----------------------------------------------------------------------------
  54. // Type Defines
  55. //----------------------------------------------------------------------------
  56. //================================
  57. // LLNode
  58. //================================
  59. /**
  60. * Link list node abstraction for WSYS.
  61. */
  62. //================================
  63. class LListNode
  64. {
  65. friend class LList;
  66. protected:
  67. LListNode *m_next; ///< Next node in list
  68. LListNode *m_prev; ///< Previous node in list
  69. Int m_pri; ///< Node's priority, used for sorting
  70. void *m_item; ///< Item we are referencing if any
  71. Bool m_autoDelete; ///< destroy() should call delete
  72. public:
  73. LListNode(); ///< Constructor
  74. void remove( void ); ///< Removes node from list
  75. void insert( LListNode *new_node ); ///< Inserts new_node infront of itself
  76. void append( LListNode *new_node ); ///< Appends new node after itself
  77. LListNode* next( void ); ///< Returns next node in list
  78. LListNode* prev( void ); ///< Returns previous node in list
  79. LListNode* loopNext( void ); ///< Returns next node in list, warpping round to start of list if nessecary
  80. LListNode* loopPrev( void ); ///< Returns previous node in list, wrapping round to end of list if nessecary
  81. Bool inList( void ); ///< Returns whether or not node in list
  82. Bool isHead( void ); ///< Returns whether or not this node is the head/tail node
  83. Int priority( void ); ///< Returns node's priority
  84. void setPriority( Int new_pri ); ///< Sets node's priority
  85. void* item( void ); ///< Returns the item this links to, if any
  86. void setItem( void *item ); ///< Make node point to an item
  87. void destroy( void ); ///< Delete node
  88. void autoDelete( void );
  89. } ;
  90. //================================
  91. // LList
  92. //================================
  93. /**
  94. * Linked list abstraction.
  95. */
  96. //================================
  97. class LList
  98. {
  99. public:
  100. /// Enumeration of list sorting methods
  101. enum SortMode
  102. {
  103. ASCENDING, ///< Lower priority numbers to front of list
  104. DESCENDING ///< Higher priority numbers to front of list
  105. };
  106. protected:
  107. LListNode m_head; ///< List head node
  108. SortMode m_sortMode; ///< What sorting method to use for this list's Add() operation
  109. Bool m_addToEndOfGroup; ///< Add nodes to end of group of nodes with same priority
  110. public:
  111. LList( );
  112. void addToHead( LListNode *new_node ); ///< Adds new node to the front of the list
  113. void addToTail( LListNode *new_node ); ///< Adds new node to the end of the list
  114. void add( LListNode *new_node ); ///< Adds new node to list sorted by priority
  115. void addItemToHead( void *item ); ///< Adds new item to the front of the list
  116. void addItemToTail( void *item ); ///< Adds new item to the end of the list
  117. void addItem( Int pri, void *item ); ///< Adds new item to list sorted by priority
  118. Int nodeCount( void ); ///< Returns number of nodes currently in list
  119. LListNode* firstNode( void ); ///< Returns first node in list
  120. LListNode* lastNode( void ); ///< Returns last node in list
  121. LListNode* getNode( Int index ); ///< Returns node in list addressed by the zero-based index passed
  122. void setSortMode( SortMode new_mode ); ///< Sets the sorting mode for the Add() operation
  123. void addToEndOfGroup( Bool yes = TRUE ); ///< Add node to end or start of group with same priority
  124. Bool isEmpty( void ); ///< Returns whether or not the the list is empty
  125. void clear( void ); ///< Deletes all items in the list. Use with care!!
  126. void merge( LList *list ); ///< Move the contents of the specified list in to this list
  127. Bool hasItem( void *item ); ///< Tests if list has the specified item
  128. LListNode* findItem( void *item ); ///< Returns the LListNode that references item
  129. void destroy( void ); ///< Free up the list items
  130. } ;
  131. //----------------------------------------------------------------------------
  132. // Inlining
  133. //----------------------------------------------------------------------------
  134. inline Bool LListNode::inList( void ) { return m_prev != this; };
  135. inline Bool LListNode::isHead( void ) { return ( m_item == (void*)&this->m_item ); };
  136. inline Int LListNode::priority( void ) { return m_pri; };
  137. inline void LListNode::setPriority( Int new_pri ) { m_pri = new_pri; };
  138. inline void LListNode::autoDelete( void ) { m_autoDelete = TRUE; };
  139. inline void* LListNode::item( void ) { return m_item; };
  140. inline void LListNode::setItem( void *item ) { m_item = item; };
  141. inline void LList::addToHead( LListNode *new_node ) { m_head.append( new_node); };
  142. inline void LList::addToTail( LListNode *new_node ) { m_head.insert( new_node); };
  143. inline LListNode* LList::firstNode( void ) { return m_head.next();} ;
  144. inline LListNode* LList::lastNode( void ) { return m_head.prev();} ;
  145. inline void LList::setSortMode( SortMode new_mode ) { m_sortMode = new_mode; };
  146. inline Bool LList::isEmpty( void ) { return !m_head.inList(); };
  147. inline void LList::destroy( void ) { clear();};
  148. #endif // __GDF_LIST_H_