Index: /trunk/psLib/src/collections/psList.c
===================================================================
--- /trunk/psLib/src/collections/psList.c	(revision 2697)
+++ /trunk/psLib/src/collections/psList.c	(revision 2698)
@@ -6,6 +6,6 @@
  *  @author Robert Daniel DeSonia, MHPCC
  *
- *  @version $Revision: 1.24 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2004-12-10 21:43:16 $
+ *  @version $Revision: 1.25 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2004-12-11 01:19:06 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -41,4 +41,10 @@
     pthread_mutex_lock(&list->lock)
     ;
+
+    // remove the free function of iterators to avoid double removal from list
+    psArray* iterators = list->iterators;
+    for (int i = 0; i < iterators->n; i++) {
+        p_psMemSetDeallocator(iterators->data[i], NULL);
+    }
 
     psFree(list->iterators);
@@ -128,8 +134,8 @@
     list->head = list->tail = NULL;
     list->iterators = psArrayAlloc(16);
+    list->iterators->n = 0;
 
     // create a default iterator
-    list->iterators->data[0] = psListIteratorAlloc(list,PS_LIST_HEAD);
-    list->iterators->n = 1;
+    psListIteratorAlloc(list,PS_LIST_HEAD);
 
     pthread_mutex_init(&(list->lock), NULL)
@@ -183,12 +189,4 @@
     psList* list = iterator->list;
 
-    if (location >= (int)list->size) {
-        psLogMsg(__func__, PS_LOG_WARN,
-                 "Specified index, %d, is beyond the end of the psList, which "
-                 "has only %d elements.  Assuming tail.",
-                 location, list->size);
-        location = PS_LIST_TAIL;
-    }
-
     if (location == PS_LIST_TAIL) {
         iterator->cursor = list->tail;
@@ -198,8 +196,17 @@
     }
 
-    if (location <= 0) {   // Invalid index
-        return false;
-    }
-
+    if (location == PS_LIST_HEAD) {
+        iterator->cursor = list->head;
+        iterator->index = 0;
+        iterator->offEnd = false;
+        return true;
+    }
+
+    if (location < 0 || location >= (int)list->size) {
+        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
+                PS_ERRORTEXT_psList_LOCATION_INVALID,
+                location);
+        return false;
+    }
 
     psListElem* cursor = iterator->cursor;
@@ -250,11 +257,14 @@
     }
 
+    if (location >= (int)list->size) {
+        psLogMsg(__func__,PS_LOG_WARN,
+                 "Specified location, %d, is beyond the end of the list.  "
+                 "Adding data item to tail.",
+                 location);
+        location = PS_LIST_TAIL;
+    }
+
     // move ourselves to the given position
-    if (list->iterators->n < 1 ||
-            ! psListIteratorSet(list->iterators->data[0],location)) {
-        // oh no, I can't find where to add this!
-        psError(PS_ERR_UNKNOWN, false,
-                PS_ERRORTEXT_psList_LOCATION_INVALID,
-                location);
+    if (! psListIteratorSet(list->iterators->data[0],location)) {
         return false;
     }
@@ -308,4 +318,6 @@
         if (elem->next == NULL) {
             list->tail = elem;
+        } else {
+            elem->next->prev = elem;
         }
     }
@@ -374,4 +386,6 @@
         if (elem->prev == NULL) {
             list->head = elem;
+        } else {
+            elem->prev->next = elem;
         }
     }
@@ -411,10 +425,5 @@
     // move ourselves to the given position
     psListIterator* defaultIterator = list->iterators->data[0];
-    if (list->iterators->n < 1 ||
-            ! psListIteratorSet(defaultIterator,location)) {
-        // oh no, I can't find where to add this!
-        psError(PS_ERR_UNKNOWN, false,
-                PS_ERRORTEXT_psList_LOCATION_INVALID,
-                location);
+    if (! psListIteratorSet(defaultIterator,location)) {
         return false;
     }
@@ -438,13 +447,11 @@
     }
 
-    psListIterator* iterator = list->iterators->data[0];
-    psListIteratorSet(iterator,PS_LIST_HEAD);
-
-    psPtr iteratorData = psListGetNext(iterator);
-    while (iteratorData != NULL && iteratorData != data) {
-        iteratorData = psListGetNext(iterator);
-    }
-
-    if (iteratorData == NULL) {
+    psListElem* elem = list->head;
+    int index = 0;
+    while (elem != NULL && elem->data != data) {
+        elem = elem->next;
+        index++;
+    }
+    if (elem == NULL) {
         psError(PS_ERR_BAD_PARAMETER_NULL, true,
                 PS_ERRORTEXT_psList_DATA_NOT_FOUND);
@@ -452,4 +459,8 @@
     }
 
+    psListIterator* iterator = (psListIterator*)list->iterators->data[0];
+    iterator->index = index;
+    iterator->cursor = elem;
+
     return listIteratorRemove(iterator);
 }
@@ -460,5 +471,9 @@
         psError(PS_ERR_BAD_PARAMETER_NULL, true,
                 PS_ERRORTEXT_psList_LIST_NULL);
-        return false;
+        return NULL;
+    }
+
+    if (list->head == NULL) { // list empty?
+        return NULL;
     }
 
Index: /trunk/psLib/src/types/psList.c
===================================================================
--- /trunk/psLib/src/types/psList.c	(revision 2697)
+++ /trunk/psLib/src/types/psList.c	(revision 2698)
@@ -6,6 +6,6 @@
  *  @author Robert Daniel DeSonia, MHPCC
  *
- *  @version $Revision: 1.24 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2004-12-10 21:43:16 $
+ *  @version $Revision: 1.25 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2004-12-11 01:19:06 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -41,4 +41,10 @@
     pthread_mutex_lock(&list->lock)
     ;
+
+    // remove the free function of iterators to avoid double removal from list
+    psArray* iterators = list->iterators;
+    for (int i = 0; i < iterators->n; i++) {
+        p_psMemSetDeallocator(iterators->data[i], NULL);
+    }
 
     psFree(list->iterators);
@@ -128,8 +134,8 @@
     list->head = list->tail = NULL;
     list->iterators = psArrayAlloc(16);
+    list->iterators->n = 0;
 
     // create a default iterator
-    list->iterators->data[0] = psListIteratorAlloc(list,PS_LIST_HEAD);
-    list->iterators->n = 1;
+    psListIteratorAlloc(list,PS_LIST_HEAD);
 
     pthread_mutex_init(&(list->lock), NULL)
@@ -183,12 +189,4 @@
     psList* list = iterator->list;
 
-    if (location >= (int)list->size) {
-        psLogMsg(__func__, PS_LOG_WARN,
-                 "Specified index, %d, is beyond the end of the psList, which "
-                 "has only %d elements.  Assuming tail.",
-                 location, list->size);
-        location = PS_LIST_TAIL;
-    }
-
     if (location == PS_LIST_TAIL) {
         iterator->cursor = list->tail;
@@ -198,8 +196,17 @@
     }
 
-    if (location <= 0) {   // Invalid index
-        return false;
-    }
-
+    if (location == PS_LIST_HEAD) {
+        iterator->cursor = list->head;
+        iterator->index = 0;
+        iterator->offEnd = false;
+        return true;
+    }
+
+    if (location < 0 || location >= (int)list->size) {
+        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
+                PS_ERRORTEXT_psList_LOCATION_INVALID,
+                location);
+        return false;
+    }
 
     psListElem* cursor = iterator->cursor;
@@ -250,11 +257,14 @@
     }
 
+    if (location >= (int)list->size) {
+        psLogMsg(__func__,PS_LOG_WARN,
+                 "Specified location, %d, is beyond the end of the list.  "
+                 "Adding data item to tail.",
+                 location);
+        location = PS_LIST_TAIL;
+    }
+
     // move ourselves to the given position
-    if (list->iterators->n < 1 ||
-            ! psListIteratorSet(list->iterators->data[0],location)) {
-        // oh no, I can't find where to add this!
-        psError(PS_ERR_UNKNOWN, false,
-                PS_ERRORTEXT_psList_LOCATION_INVALID,
-                location);
+    if (! psListIteratorSet(list->iterators->data[0],location)) {
         return false;
     }
@@ -308,4 +318,6 @@
         if (elem->next == NULL) {
             list->tail = elem;
+        } else {
+            elem->next->prev = elem;
         }
     }
@@ -374,4 +386,6 @@
         if (elem->prev == NULL) {
             list->head = elem;
+        } else {
+            elem->prev->next = elem;
         }
     }
@@ -411,10 +425,5 @@
     // move ourselves to the given position
     psListIterator* defaultIterator = list->iterators->data[0];
-    if (list->iterators->n < 1 ||
-            ! psListIteratorSet(defaultIterator,location)) {
-        // oh no, I can't find where to add this!
-        psError(PS_ERR_UNKNOWN, false,
-                PS_ERRORTEXT_psList_LOCATION_INVALID,
-                location);
+    if (! psListIteratorSet(defaultIterator,location)) {
         return false;
     }
@@ -438,13 +447,11 @@
     }
 
-    psListIterator* iterator = list->iterators->data[0];
-    psListIteratorSet(iterator,PS_LIST_HEAD);
-
-    psPtr iteratorData = psListGetNext(iterator);
-    while (iteratorData != NULL && iteratorData != data) {
-        iteratorData = psListGetNext(iterator);
-    }
-
-    if (iteratorData == NULL) {
+    psListElem* elem = list->head;
+    int index = 0;
+    while (elem != NULL && elem->data != data) {
+        elem = elem->next;
+        index++;
+    }
+    if (elem == NULL) {
         psError(PS_ERR_BAD_PARAMETER_NULL, true,
                 PS_ERRORTEXT_psList_DATA_NOT_FOUND);
@@ -452,4 +459,8 @@
     }
 
+    psListIterator* iterator = (psListIterator*)list->iterators->data[0];
+    iterator->index = index;
+    iterator->cursor = elem;
+
     return listIteratorRemove(iterator);
 }
@@ -460,5 +471,9 @@
         psError(PS_ERR_BAD_PARAMETER_NULL, true,
                 PS_ERRORTEXT_psList_LIST_NULL);
-        return false;
+        return NULL;
+    }
+
+    if (list->head == NULL) { // list empty?
+        return NULL;
     }
 
Index: /trunk/psLib/test/collections/tst_psList.c
===================================================================
--- /trunk/psLib/test/collections/tst_psList.c	(revision 2697)
+++ /trunk/psLib/test/collections/tst_psList.c	(revision 2698)
@@ -6,6 +6,6 @@
  *  @author Robert DeSonia, MHPCC
  *
- *  @version $Revision: 1.19 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2004-12-10 02:50:16 $
+ *  @version $Revision: 1.20 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2004-12-11 01:19:06 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -981,5 +981,5 @@
 
     printf("original list = [");
-    psListIteratorSet(iter,PS_LIST_HEAD);
+    iter = psListIteratorAlloc(list,PS_LIST_HEAD);
     while( (uValue=psListGetNext(iter)) != NULL ) {
         printf(" %d",*uValue);
@@ -999,5 +999,5 @@
     psU32* prevUValue = psListGetNext(iter);
     while( (uValue=psListGetNext(iter)) != NULL ) {
-        if (*prevUValue < *uValue) {
+        if (*prevUValue > *uValue) {
             psError(PS_ERR_UNKNOWN, true,"Hey, the list wasn't sorted!?");
             return 3;
