Index: src/OFList.h ================================================================== --- src/OFList.h +++ src/OFList.h @@ -31,10 +31,11 @@ @interface OFList: OFObject { of_list_object_t *first; of_list_object_t *last; size_t listobj_size; + size_t count; BOOL retain_and_release; } /** * \return A new autoreleased OFList */ @@ -122,12 +123,9 @@ * \param listobj The list object returned by append / prepend */ - remove: (of_list_object_t*)listobj; /** - * Get the number of items in the list. Use with caution, as this means one - * iteration through the whole list! - * * \return The number of items in the list. */ - (size_t)count; @end Index: src/OFList.m ================================================================== --- src/OFList.m +++ src/OFList.m @@ -94,10 +94,12 @@ last->next = o; last = o; if (first == NULL) first = o; + + count++; if (retain_and_release) [obj retain]; return o; @@ -116,10 +118,12 @@ first->prev = o; first = o; if (last == NULL) last = o; + + count++; if (retain_and_release) [obj retain]; return o; @@ -140,10 +144,12 @@ listobj->prev = o; if (listobj == first) first = o; + + count++; if (retain_and_release) [obj retain]; return o; @@ -164,10 +170,12 @@ listobj->next = o; if (listobj == last) last = o; + + count++; if (retain_and_release) [obj retain]; return o; @@ -182,10 +190,12 @@ if (first == listobj) first = listobj->next; if (last == listobj) last = listobj->prev; + + count--; if (retain_and_release) [listobj->object release]; [self freeMemory: listobj]; @@ -193,17 +203,11 @@ return self; } - (size_t)count { - size_t i; - of_list_object_t *iter; - - for (i = 0, iter = first; iter != NULL; iter = iter->next) - i++; - - return i; + return count; } - (BOOL)isEqual: (id)obj { of_list_object_t *iter, *iter2; @@ -245,10 +249,13 @@ if (new->first == NULL) new->first = o; if (prev != NULL) prev->next = o; + + new->count++; + if (retain_and_release) [o->object retain]; prev = o; } Index: tests/OFList/OFList.m ================================================================== --- tests/OFList/OFList.m +++ tests/OFList/OFList.m @@ -21,11 +21,11 @@ #define ZD "%zd" #else #define ZD "%u" #endif -#define NUM_TESTS 23 +#define NUM_TESTS 27 #define SUCCESS \ { \ printf("\r\033[1;%dmTests successful: " ZD "/%d\033[0m", \ (i == NUM_TESTS - 1 ? 32 : 33), i + 1, NUM_TESTS); \ fflush(stdout); \ @@ -87,10 +87,19 @@ for (iter = [list first], j = 0; iter != NULL; iter = iter->next, j++) CHECK([iter->object isEqual: strings[j]]) CHECK([list count] == 3) + list2 = [list copy]; + CHECK([list2 count] == 3) + [list2 remove: [list2 last]]; + CHECK([list2 count] == 2) + [list2 remove: [list2 first]->next]; + CHECK([list2 count] == 1) + [list2 remove: [list2 first]]; + CHECK([list2 count] == 0) + list2 = [OFList list]; [list2 append: strings[0]]; [list2 append: strings[1]]; [list2 append: strings[2]];