txList.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "txList.h"
  6. //----------------------------/
  7. //- Implementation of txList -/
  8. //----------------------------/
  9. /**
  10. * Default constructor for a txList;
  11. **/
  12. txList::txList() {
  13. firstItem = 0;
  14. lastItem = 0;
  15. itemCount = 0;
  16. } //-- txList;
  17. /**
  18. * txList destructor, cleans up ListItems, but will not delete the Object
  19. * references
  20. */
  21. txList::~txList() {
  22. clear();
  23. } //-- ~txList
  24. nsresult txList::add(void* objPtr)
  25. {
  26. return insertBefore(objPtr, 0);
  27. } //-- add
  28. /**
  29. * Returns the number of items in this txList
  30. **/
  31. int32_t List::getLength() {
  32. return itemCount;
  33. } //-- getLength
  34. /**
  35. * Inserts the given Object pointer as the item just after refItem.
  36. * If refItem is a null pointer the Object will be inserted at the
  37. * beginning of the txList (ie, insert after nothing).
  38. * This method assumes refItem is a member of this list, and since this
  39. * is a private method, I feel that's a valid assumption
  40. **/
  41. nsresult txList::insertAfter(void* objPtr, ListItem* refItem)
  42. {
  43. //-- if refItem == null insert at front
  44. if (!refItem)
  45. return insertBefore(objPtr, firstItem);
  46. return insertBefore(objPtr, refItem->nextItem);
  47. } //-- insertAfter
  48. /**
  49. * Inserts the given Object pointer as the item just before refItem.
  50. * If refItem is a null pointer the Object will be inserted at the
  51. * end of the txList (ie, insert before nothing).
  52. * This method assumes refItem is a member of this list, and since this
  53. * is a private method, I feel that's a valid assumption
  54. **/
  55. nsresult txList::insertBefore(void* objPtr, ListItem* refItem)
  56. {
  57. ListItem* item = new ListItem;
  58. item->objPtr = objPtr;
  59. item->nextItem = 0;
  60. item->prevItem = 0;
  61. //-- if refItem == null insert at end
  62. if (!refItem) {
  63. //-- add to back of list
  64. if (lastItem) {
  65. lastItem->nextItem = item;
  66. item->prevItem = lastItem;
  67. }
  68. lastItem = item;
  69. if (!firstItem)
  70. firstItem = item;
  71. }
  72. else {
  73. //-- insert before given item
  74. item->nextItem = refItem;
  75. item->prevItem = refItem->prevItem;
  76. refItem->prevItem = item;
  77. if (item->prevItem)
  78. item->prevItem->nextItem = item;
  79. else
  80. firstItem = item;
  81. }
  82. // increase the item count
  83. ++itemCount;
  84. return NS_OK;
  85. } //-- insertBefore
  86. txList::ListItem* txList::remove(ListItem* item) {
  87. if (!item)
  88. return item;
  89. //-- adjust the previous item's next pointer
  90. if (item->prevItem) {
  91. item->prevItem->nextItem = item->nextItem;
  92. }
  93. //-- adjust the next item's previous pointer
  94. if (item->nextItem) {
  95. item->nextItem->prevItem = item->prevItem;
  96. }
  97. //-- adjust first and last items
  98. if (item == firstItem)
  99. firstItem = item->nextItem;
  100. if (item == lastItem)
  101. lastItem = item->prevItem;
  102. //-- decrease Item count
  103. --itemCount;
  104. return item;
  105. } //-- remove
  106. void txList::clear()
  107. {
  108. ListItem* item = firstItem;
  109. while (item) {
  110. ListItem* tItem = item;
  111. item = item->nextItem;
  112. delete tItem;
  113. }
  114. firstItem = 0;
  115. lastItem = 0;
  116. itemCount = 0;
  117. }
  118. //------------------------------------/
  119. //- Implementation of txListIterator -/
  120. //------------------------------------/
  121. /**
  122. * Creates a new txListIterator for the given txList
  123. * @param list, the txList to create an Iterator for
  124. **/
  125. txListIterator::txListIterator(txList* list) {
  126. this->list = list;
  127. currentItem = 0;
  128. atEndOfList = false;
  129. } //-- txListIterator
  130. /**
  131. * Adds the Object pointer to the txList pointed to by this txListIterator.
  132. * The Object pointer is inserted as the next item in the txList
  133. * based on the current position within the txList
  134. * @param objPtr the Object pointer to add to the list
  135. **/
  136. nsresult txListIterator::addAfter(void* objPtr)
  137. {
  138. if (currentItem || !atEndOfList)
  139. return list->insertAfter(objPtr, currentItem);
  140. return list->insertBefore(objPtr, 0);
  141. } //-- addAfter
  142. /**
  143. * Adds the Object pointer to the txList pointed to by this txListIterator.
  144. * The Object pointer is inserted as the previous item in the txList
  145. * based on the current position within the txList
  146. * @param objPtr the Object pointer to add to the list
  147. **/
  148. nsresult txListIterator::addBefore(void* objPtr)
  149. {
  150. if (currentItem || atEndOfList)
  151. return list->insertBefore(objPtr, currentItem);
  152. return list->insertAfter(objPtr, 0);
  153. } //-- addBefore
  154. /**
  155. * Returns true if a successful call to the next() method can be made
  156. * @return true if a successful call to the next() method can be made,
  157. * otherwise false
  158. **/
  159. bool txListIterator::hasNext() {
  160. bool hasNext = false;
  161. if (currentItem)
  162. hasNext = (currentItem->nextItem != 0);
  163. else if (!atEndOfList)
  164. hasNext = (list->firstItem != 0);
  165. return hasNext;
  166. } //-- hasNext
  167. /**
  168. * Returns the next Object pointer in the list
  169. **/
  170. void* txListIterator::next() {
  171. void* obj = 0;
  172. if (currentItem)
  173. currentItem = currentItem->nextItem;
  174. else if (!atEndOfList)
  175. currentItem = list->firstItem;
  176. if (currentItem)
  177. obj = currentItem->objPtr;
  178. else
  179. atEndOfList = true;
  180. return obj;
  181. } //-- next
  182. /**
  183. * Returns the previous Object in the list
  184. **/
  185. void* txListIterator::previous() {
  186. void* obj = 0;
  187. if (currentItem)
  188. currentItem = currentItem->prevItem;
  189. else if (atEndOfList)
  190. currentItem = list->lastItem;
  191. if (currentItem)
  192. obj = currentItem->objPtr;
  193. atEndOfList = false;
  194. return obj;
  195. } //-- previous
  196. /**
  197. * Returns the current Object
  198. **/
  199. void* txListIterator::current() {
  200. if (currentItem)
  201. return currentItem->objPtr;
  202. return 0;
  203. } //-- current
  204. /**
  205. * Removes the Object last returned by the next() or previous() methods;
  206. * @return the removed Object pointer
  207. **/
  208. void* txListIterator::remove() {
  209. void* obj = 0;
  210. if (currentItem) {
  211. obj = currentItem->objPtr;
  212. txList::ListItem* item = currentItem;
  213. previous(); //-- make previous item the current item
  214. list->remove(item);
  215. delete item;
  216. }
  217. return obj;
  218. } //-- remove
  219. /**
  220. * Resets the current location within the txList to the beginning of the txList
  221. **/
  222. void txListIterator::reset() {
  223. atEndOfList = false;
  224. currentItem = 0;
  225. } //-- reset
  226. /**
  227. * Move the iterator to right after the last element
  228. **/
  229. void txListIterator::resetToEnd() {
  230. atEndOfList = true;
  231. currentItem = 0;
  232. } //-- moveToEnd