38 #include "NptResults.h"    40 #include "NptConstants.h"    41 #include "NptCommon.h"    46 const int NPT_ERROR_LIST_EMPTY              = NPT_ERROR_BASE_LIST - 0;
    47 const int NPT_ERROR_LIST_OPERATION_ABORTED  = NPT_ERROR_BASE_LIST - 1;
    48 const int NPT_ERROR_LIST_OPERATION_CONTINUE = NPT_ERROR_BASE_LIST - 2;
    68         T&  operator*()
  const { 
return m_Item->m_Data; }
    69         T*  operator->()
 const { 
return &m_Item->m_Data;}
    71             m_Item = m_Item->m_Next;
    76             m_Item = m_Item->m_Next;
    80             m_Item = m_Item->m_Prev;
    85             m_Item = m_Item->m_Prev;
    88         operator bool()
 const {
    89             return m_Item != NULL;
    91         bool operator==(
const Iterator& other)
 const {
    92             return m_Item == other.m_Item;
    94         bool operator!=(
const Iterator& other)
 const {
    95             return m_Item != other.m_Item;
    97         void operator=(
const Iterator& other) {
    98             m_Item = other.m_Item;
   100         void operator=(
Item* item) {
   115     NPT_Result   Add(
const T& data);
   116     NPT_Result   Insert(
const Iterator where, 
const T& data);
   117     NPT_Result   Remove(
const T& data, 
bool all=
false);
   118     NPT_Result   Erase(
const Iterator position);
   119     NPT_Result   PopHead(T& data);
   120     bool         Contains(
const T& data) 
const;
   122     NPT_Result   Get(NPT_Ordinal index, T& data) 
const;
   123     NPT_Result   Get(NPT_Ordinal index, T*& data) 
const;
   124     NPT_Cardinal GetItemCount()
 const { 
return m_ItemCount; }
   127     Iterator     GetItem(NPT_Ordinal index) 
const;
   131     NPT_Result   Remove(
const NPT_List<T>& list, 
bool all=
false);
   132     NPT_Result   Cut(NPT_Cardinal keep, 
NPT_List<T>& cut);
   135     NPT_Result   Add(
Item& item);
   136     NPT_Result   Detach(
Item& item);
   142     template <
typename X> 
   143     NPT_Result Apply(
const X& 
function)
 const   147             function(item->m_Data);
   154     template <
typename X, 
typename P> 
   155     NPT_Result ApplyUntil(
const X& 
function, 
const P& predicate, 
bool* match = NULL)
 const   159             NPT_Result return_value;
   160             if (predicate(
function(item->m_Data), return_value)) {
   161                 if (match) *match = 
true;
   167         if (match) *match = 
false;
   171     template <
typename P> 
   172     Iterator Find(
const P& predicate, NPT_Ordinal n=0)
 const   176             if (predicate(item->m_Data)) {
   190     template <
typename X> 
   191     NPT_Result Sort(
const X& 
function)
   193         if (GetItemCount() <= 1) 
return NPT_SUCCESS;
   196         NPT_CHECK(Cut(GetItemCount() >> 1, right));
   202         right.Sort(
function);
   205         if (
function(m_Tail->m_Data, right.m_Head->m_Data) > 0) {
   206             Merge(right, 
function);
   209             right.m_Head->m_Prev = m_Tail;
   210             m_Tail->m_Next = right.m_Head;
   211             m_Tail = right.m_Tail;
   212             m_ItemCount += right.m_ItemCount;
   214             right.m_ItemCount = 0;
   215             right.m_Head = right.m_Tail = NULL;
   221     template <
typename X> 
   222     NPT_Result Merge(
NPT_List<T>& other, 
const X& 
function) 
   226         while (left && other.m_Head) {
   227             if (
function(*left, other.m_Head->m_Data) <= 0) {
   231                 Item* head = other.m_Head;
   239             other.m_Head->m_Prev = m_Tail;
   240             if (m_Tail) m_Tail->m_Next = other.m_Head;
   241             m_Tail = other.m_Tail;
   242             if (!m_Head) m_Head = other.m_Head;
   243             other.m_Head = other.m_Tail = NULL;
   245         m_ItemCount += other.m_ItemCount;
   246         other.m_ItemCount = 0;
   261         Item(
const T& data) : m_Next(0), m_Prev(0), m_Data(data) {}
   274     NPT_Cardinal m_ItemCount;
   282 template <
typename T>
   291 template <
typename T>
   301 template <
typename T>
   311 template <
typename T>
   319     Item* item = list.m_Head;
   329 template <
typename T>
   334     if (m_ItemCount != other.m_ItemCount) 
return false;
   337     Item* our_item = m_Head;
   338     Item* their_item = other.m_Head;
   339     while (our_item && their_item) {
   340         if (our_item->m_Data != their_item->m_Data) 
return false;
   341         our_item   = our_item->m_Next;
   342         their_item = their_item->m_Next;
   345     return our_item == NULL && their_item == NULL;
   351 template <
typename T>
   356     return !(*
this == other);
   362 template <
typename T>
   369         Item* next = item->m_Next;
   384 template <
typename T>
   390         item.m_Prev = m_Tail;
   392         m_Tail->m_Next = &item;
   410 template <
typename T>
   415     Item* item = list.m_Head;
   427 template <
typename T>
   432     return Add(*
new Item(data));
   438 template <
typename T>
   443     if (n >= m_ItemCount) 
return result;
   446     for (
unsigned int i=0; i<n; i++) {
   456 template <
typename T>
   461     return Insert(where, *
new Item(data));
   467 template <
typename T>
   472     Item* position = where.m_Item;
   475         item.m_Next = position;
   476         item.m_Prev = position->m_Prev;
   477         position->m_Prev = &item;
   479             item.m_Prev->m_Next = &item;
   498 template <
typename T>
   502     if (!position) 
return NPT_ERROR_NO_SUCH_ITEM;
   503     Detach(*position.m_Item);
   504     delete position.m_Item;
   512 template <
typename T>
   517     NPT_Cardinal matches = 0;
   520         Item* next = item->m_Next;
   521         if (item->m_Data == data) {
   531             if (!all) 
return NPT_SUCCESS;
   536     return matches?NPT_SUCCESS:NPT_ERROR_NO_SUCH_ITEM;
   542 template <
typename T>
   546     Item* item = list.m_Head;
   548         Remove(item->m_Data, all);
   558 template <
typename T>
   567             item.m_Next->m_Prev = item.m_Prev;
   568             item.m_Prev->m_Next = item.m_Next;
   571             m_Tail = item.m_Prev;
   572             m_Tail->m_Next = NULL;
   576         m_Head = item.m_Next;
   579             m_Head->m_Prev = NULL;
   595 template <
typename T>
   600     NPT_CHECK(Get(index, data_pointer));
   601     data = *data_pointer;
   608 template <
typename T>
   614     if (index < m_ItemCount) {
   615         while (index--) item = item->m_Next;
   616         data = &item->m_Data;
   620         return NPT_ERROR_NO_SUCH_ITEM;
   627 template <
typename T>
   632     if (m_Head == NULL) 
return NPT_ERROR_LIST_EMPTY;
   635     data = m_Head->m_Data;
   639     m_Head = m_Head->m_Next;
   641         m_Head->m_Prev = NULL;
   656 template <
typename T>
   662         if (item->m_Data == data) 
return true;
   672 template <
typename T> 
   679     if (keep >= GetItemCount()) 
return NPT_SUCCESS;
   682     cut.m_ItemCount = m_ItemCount-keep;
   687     while (keep--) { item = item->m_Next;}
   694     if (item == m_Head) m_Head = NULL;
   695     m_Tail = item->m_Prev;
   698     if (item->m_Prev) item->m_Prev->m_Next = NULL;
   704 #endif // _NPT_LIST_H_ Definition: NptList.h:257