Thread: [C-MPI-commits] SF.net SVN: c-mpi:[84] src/adts/list.c
Status: Pre-Alpha
Brought to you by:
jmwozniak
|
From: <jmw...@us...> - 2010-05-11 21:15:52
|
Revision: 84
http://c-mpi.svn.sourceforge.net/c-mpi/?rev=84&view=rev
Author: jmwozniak
Date: 2010-05-11 19:31:33 +0000 (Tue, 11 May 2010)
Log Message:
-----------
Fix EOLs.
Modified Paths:
--------------
src/adts/list.c
Modified: src/adts/list.c
===================================================================
--- src/adts/list.c 2010-05-11 19:18:30 UTC (rev 83)
+++ src/adts/list.c 2010-05-11 19:31:33 UTC (rev 84)
@@ -1,24 +1,24 @@
-#include "list.h"
+#include "list.h"
-#include "unistd.h"
+#include "unistd.h"
struct list*
list_create()
{
struct list* new_list = malloc(sizeof(struct list));
if (! new_list)
- return NULL;
+ return NULL;
new_list->head = NULL;
new_list->tail = NULL;
new_list->size = 0;
- return new_list;
+ return new_list;
}
/**
@return The new list_item.
*/
-struct list_item*
+struct list_item*
list_add(struct list* target, void* data)
{
struct list_item* new_item = malloc(sizeof(struct list_item));
@@ -38,7 +38,7 @@
target->tail->next = new_item;
}
- target->tail = new_item;
+ target->tail = new_item;
target->size++;
return new_item;
@@ -52,59 +52,59 @@
{
if (! list_inspect(target, data, n))
return list_add(target, data);
- return NULL;
+ return NULL;
}
/**
Add this pre-formed list_item to target.
- Convenience: sets item->next to NULL.
- @return The added item.
-*/
+ Convenience: sets item->next to NULL.
+ @return The added item.
+*/
struct list_item*
list_append(struct list* target, struct list_item* item)
{
if (target->size == 0)
target->head = item;
else
- target->tail->next = item;
+ target->tail->next = item;
target->tail = item;
- item->next = NULL;
+ item->next = NULL;
target->size++;
- return item;
+ return item;
}
/**
- Create new list from string of words.
- Parse words separated by space or tab, insert each into list.
+ Create new list from string of words.
+ Parse words separated by space or tab, insert each into list.
*/
struct list*
list_parse(char* s)
{
- struct list* result = list_create();
+ struct list* result = list_create();
char* p = s;
- char* q;
+ char* q;
while (*p)
{
- // Set p to start of word, q to end of word...
+ // Set p to start of word, q to end of word...
while (*p == ' ' || *p == '\t')
p++;
if (!*p)
break;
- q = p+1;
+ q = p+1;
while (! (*q == ' ' || *q == '\t' || *q == '\0'))
q++;
- // Insert word into list...
+ // Insert word into list...
char* data = malloc(q-p+2);
strncpy(data, p, q-p);
data[q-p] = '\0';
list_add(result, data);
- // Step forward:
+ // Step forward:
p = q;
}
- return result;
+ return result;
}
/**
@@ -113,19 +113,19 @@
void*
list_pop(struct list* target)
{
- void* data;
+ void* data;
if (target->size == 0)
return NULL;
if (target->size == 1)
{
- data = target->head->data;
- free(target->head);
+ data = target->head->data;
+ free(target->head);
target->head = NULL;
target->tail = NULL;
- target->size = 0;
- return data;
+ target->size = 0;
+ return data;
}
-
+
struct list_item* item;
for (item = target->head; item->next->next;
item = item->next);
@@ -133,16 +133,16 @@
free(item->next);
item->next = NULL;
target->tail = item;
- target->size--;
- return data;
+ target->size--;
+ return data;
}
-void*
-list_peek(struct list* target)
+void*
+list_peek(struct list* target)
{
if (target->size == 0)
return NULL;
- return target->head->data;
+ return target->head->data;
}
/**
@@ -150,34 +150,34 @@
void*
list_poll(struct list* target)
{
- // NOTE_FI(target->size);
- void* data;
+ // NOTE_FI(target->size);
+ void* data;
if (target->size == 0)
return NULL;
if (target->size == 1)
{
- data = target->head->data;
- free(target->head);
+ data = target->head->data;
+ free(target->head);
target->head = NULL;
target->tail = NULL;
- target->size = 0;
- return data;
+ target->size = 0;
+ return data;
}
- struct list_item* delendum = target->head;
+ struct list_item* delendum = target->head;
data = target->head->data;
target->head = target->head->next;
free(delendum);
target->size--;
- return data;
+ return data;
}
void*
list_random(struct list* target)
{
int i;
- // printf("%s %i \n", "list size: ", target->size);
-
+ // printf("%s %i \n", "list size: ", target->size);
+
if (target->size == 0)
return NULL;
@@ -185,15 +185,15 @@
struct list_item* item = target->head;
for (i = 0; i < p; i++)
item = item->next;
-
- return item->data;
+
+ return item->data;
}
-struct list_item*
-list_ordered_insert(struct list* target,
- int (*cmp)(void*,void*), void* data)
+struct list_item*
+list_ordered_insert(struct list* target,
+ int (*cmp)(void*,void*), void* data)
{
- // NOTE_F;
+ // NOTE_F;
struct list_item* new_item = malloc(sizeof(struct list_item));
if (! new_item)
return NULL;
@@ -208,43 +208,43 @@
else
{
struct list_item* item = target->head;
- // Are we the new head?
+ // Are we the new head?
if (cmp(data, item->data) == -1)
{
new_item->next = target->head;
- target->head = new_item;
+ target->head = new_item;
}
else
{
do
{
- // Are we inserting after this item?
+ // Are we inserting after this item?
if (item->next == NULL)
{
item->next = new_item;
target->tail = new_item;
- break;
+ break;
}
else
{
if (cmp(data, item->next->data) == -1)
{
- new_item->next = item->next;
+ new_item->next = item->next;
item->next = new_item;
- break;
+ break;
}
}
} while ((item = item->next));
}
}
target->size++;
- return new_item;
+ return new_item;
}
/**
- Untested.
+ Untested.
*/
-struct list_item*
+struct list_item*
list_ordered_insert_unique(struct list* target, int (*cmp)(void*,void*),
void* data)
{
@@ -262,70 +262,70 @@
else
{
struct list_item* item = target->head;
- // Are we the new head?
+ // Are we the new head?
if (cmp(data, item->data) == -1)
{
new_item->next = target->head;
- target->head = new_item;
+ target->head = new_item;
}
else
{
do
{
- // Are we inserting after this item?
+ // Are we inserting after this item?
if (item->next == NULL)
{
item->next = new_item;
target->tail = new_item;
- break;
+ break;
}
else
{
- int c = cmp(data, item->next->data);
+ int c = cmp(data, item->next->data);
if (c == 0)
{
- free(new_item);
+ free(new_item);
return NULL;
}
if (c == -1)
{
- new_item->next = item->next;
+ new_item->next = item->next;
item->next = new_item;
- break;
+ break;
}
}
} while ((item = item->next));
}
}
target->size++;
- return new_item;
+ return new_item;
}
-struct list_item*
-list_add_unique(struct list* target,
+struct list_item*
+list_add_unique(struct list* target,
int (*cmp)(void*,void*), void* data)
{
if (! list_contains(target, cmp, data))
return list_add(target, data);
- return NULL;
+ return NULL;
}
/**
*/
bool
list_contains(struct list* target,
- int (*cmp)(void*,void*), void* data)
+ int (*cmp)(void*,void*), void* data)
{
struct list_item* item;
for (item = target->head;
item; item = item->next)
if (cmp(item->data, data) == 0)
return true;
- return false;
+ return false;
}
/**
- Compare data pointer addresses for match.
+ Compare data pointer addresses for match.
@return An equal data pointer or NULL if not found.
*/
void*
@@ -336,41 +336,41 @@
item; item = item->next)
if (item->data == data)
return data;
- return NULL;
+ return NULL;
}
/**
Compare data contents for match.
- @return A pointer to an equivalent object or NULL if not found.
+ @return A pointer to an equivalent object or NULL if not found.
*/
-void*
+void*
list_inspect(struct list* target, void* data, size_t n)
{
struct list_item* item;
for (item = target->head;
item; item = item->next)
if (memcmp(item->data, data, n) == 0)
- return item->data;
- return NULL;
+ return item->data;
+ return NULL;
}
/**
- Empty the list.
+ Empty the list.
*/
void
list_clear(struct list* target)
{
struct list_item* item = target->head;
-
- while (item)
+
+ while (item)
{
- struct list_item* next = item->next;
- free(item);
- item = next;
+ struct list_item* next = item->next;
+ free(item);
+ item = next;
}
target->head = NULL;
target->tail = NULL;
- target->size = 0;
+ target->size = 0;
}
/**
@@ -380,44 +380,44 @@
list_clobber(struct list* target)
{
struct list_item* item = target->head;
-
- while (item)
+
+ while (item)
{
- struct list_item* next = item->next;
+ struct list_item* next = item->next;
free(item->data);
- free(item);
- item = next;
+ free(item);
+ item = next;
}
target->head = NULL;
- target->tail = NULL;
+ target->tail = NULL;
}
/**
- Removes only one item that points to given data.
+ Removes only one item that points to given data.
Does not free the item data.
@return True iff the data pointer was matched
- and the item was freed.
+ and the item was freed.
*/
bool
list_remove(struct list* target, void* data)
{
// NOTE_F;
-
+
if (target->size == 0)
- return false;
-
+ return false;
+
struct list_item* item = target->head;
if (data == item->data)
{
struct list_item* next = item->next;
- free(item);
+ free(item);
target->head = next;
if (target->tail == next)
target->tail = NULL;
- target->size--;
- return true;
+ target->size--;
+ return true;
}
while (item->next)
@@ -427,42 +427,42 @@
{
struct list_item* nextnext = item->next->next;
if (target->tail == item->next)
- target->tail = nextnext;
- free(item->next);
+ target->tail = nextnext;
+ free(item->next);
item->next = nextnext;
- target->size--;
- return true;
+ target->size--;
+ return true;
}
- item = item->next;
- }
- return false;
+ item = item->next;
+ }
+ return false;
}
/**
- Return all elements from the list where cmp(data,arg) == 0.
+ Return all elements from the list where cmp(data,arg) == 0.
*/
-struct list*
+struct list*
list_select(struct list* target,
int (*cmp)(void*,void*), void* arg)
{
- struct list* result = list_create();
+ struct list* result = list_create();
struct list_item* item;
- assert(target != NULL);
-
-
+ assert(target != NULL);
+
+
for (item = target->head;
item; item = item->next)
{
if (cmp(item->data, arg) == 0)
- list_add(result, item->data);
+ list_add(result, item->data);
}
- return result;
+ return result;
}
/**
Remove the elements from the list where cmp(data,arg) == 0.
- @return true if one or more items were deleted.
+ @return true if one or more items were deleted.
*/
bool
list_remove_where(struct list* target,
@@ -470,21 +470,21 @@
{
bool result = false;
struct list_item* item;
-
+
if (target->size == 0)
- return false;
+ return false;
- int old_size = target->size;
-
+ int old_size = target->size;
+
// Establish next good item in list...
- struct list_item* good = NULL;
+ struct list_item* good = NULL;
for (item = target->head;
item; item = item->next)
{
if (cmp(item->data, arg) != 0)
{
good = item;
- break;
+ break;
}
}
@@ -492,36 +492,36 @@
// List should be empty
{
if (target->size > 0)
- result = true;
+ result = true;
list_clear(target);
- return result;
+ return result;
}
-
- // Establish correct head...
+
+ // Establish correct head...
struct list_item* head = target->head;
while (head && head != good)
{
struct list_item* next = head->next;
printf("free: %i \n", *(int*) head->data);
free(head);
- target->size--;
- head = next;
+ target->size--;
+ head = next;
}
target->head = good;
-
- // Now current points to the first valid item in the list.
- struct list_item* current = target->head;
+
+ // Now current points to the first valid item in the list.
+ struct list_item* current = target->head;
while (good != NULL)
{
// Move to a good item or NULL...
- struct list_item* item = good->next;
- good = NULL;
+ struct list_item* item = good->next;
+ good = NULL;
while (item)
{
if (cmp(item->data, arg) != 0)
{
good = item;
- break;
+ break;
}
item = item->next;
}
@@ -532,85 +532,85 @@
target->tail = current;
}
- // Free items between current and good:
-
+ // Free items between current and good:
+
struct list_item* link = current;
- current = current->next;
+ current = current->next;
while (current != good)
{
struct list_item* next = current->next;
free(current);
- target->size--;
- current = next;
+ target->size--;
+ current = next;
}
- link->next = good;
+ link->next = good;
}
if (target->size != old_size)
- return true;
- return false;
+ return true;
+ return false;
}
/**
Remove and return all elements from the list where
cmp(data,arg) == 0.
- @return true if one or more items were deleted.
+ @return true if one or more items were deleted.
*/
-struct list*
+struct list*
list_pop_where(struct list* target,
int (*cmp)(void*,void*), void* arg)
{
- struct list* result = list_create();
+ struct list* result = list_create();
struct list_item* item;
-
+
if (target->size == 0)
- return result;
+ return result;
// Establish next good item in list...
- struct list_item* good = NULL;
+ struct list_item* good = NULL;
for (item = target->head;
item; item = item->next)
{
if (cmp(item->data, arg) != 0)
{
good = item;
- break;
+ break;
}
}
if (! good)
- // All elements should be moved
+ // All elements should be moved
{
- list_transplant(result, target);
- return result;
+ list_transplant(result, target);
+ return result;
}
-
- // Establish correct head...
+
+ // Establish correct head...
struct list_item* head = target->head;
while (head && head != good)
{
struct list_item* next = head->next;
printf("free: %i \n", *(int*) head->data);
- list_append(result, head);
- target->size--;
- head = next;
+ list_append(result, head);
+ target->size--;
+ head = next;
}
target->head = good;
-
- // Now current points to the first valid item in the list.
- struct list_item* current = target->head;
+
+ // Now current points to the first valid item in the list.
+ struct list_item* current = target->head;
while (good != NULL)
{
// Move to a good item or NULL...
- struct list_item* item = good->next;
- good = NULL;
+ struct list_item* item = good->next;
+ good = NULL;
while (item)
{
if (cmp(item->data, arg) != 0)
{
good = item;
- break;
+ break;
}
item = item->next;
}
@@ -619,34 +619,34 @@
// No more good items were found
target->tail = current;
- if (good != NULL)
- printf("good: %i \n", *(int*) good->data);
-
- // Free items between current and good:
-
+ if (good != NULL)
+ printf("good: %i \n", *(int*) good->data);
+
+ // Free items between current and good:
+
struct list_item* link = current;
- current = current->next;
+ current = current->next;
while (current != good)
{
struct list_item* next = current->next;
list_append(result, current);
- target->size--;
- current = next;
+ target->size--;
+ current = next;
}
- link->next = good;
+ link->next = good;
}
- return result;
+ return result;
}
/**
- Attach copy of tail to target.
-*/
+ Attach copy of tail to target.
+*/
void
list_attach(struct list* target, struct list* segment);
/**
- Moves all items from tail into target structure.
+ Moves all items from tail into target structure.
*/
void
list_transplant(struct list* target, struct list* segment)
@@ -655,34 +655,34 @@
{
target->head = segment->head;
}
- target->tail->next = segment->head;
- target->size += segment->size;
+ target->tail->next = segment->head;
+ target->size += segment->size;
target->tail = segment->tail;
segment->head = NULL;
segment->tail = NULL;
- segment->size = 0;
+ segment->size = 0;
}
/**
Does not free the item data.
@return True iff the data content was matched by memcmp
- and the item was freed.
+ and the item was freed.
*/
bool
list_erase(struct list* target, void* data, size_t n)
{
struct list_item* item = target->head;
- // Are we removing the head?
+ // Are we removing the head?
if (memcmp(data, item->data, n) == 0)
{
struct list_item* next = item->next;
- free(item);
+ free(item);
target->head = next;
if (target->tail == next)
target->tail = NULL;
- target->size--;
- return true;
+ target->size--;
+ return true;
}
do
{
@@ -691,19 +691,19 @@
{
struct list_item* nextnext = item->next->next;
if (target->tail == item->next)
- target->tail = nextnext;
- free(item->next);
+ target->tail = nextnext;
+ free(item->next);
item->next = nextnext;
- target->size--;
- return true;
- }
+ target->size--;
+ return true;
+ }
} while ((item = item->next));
- return false;
+ return false;
}
/**
Function specifies the output format for the data items
- Does not free return of f.
+ Does not free return of f.
*/
void
list_output(char* (*f)(void*), struct list* target)
@@ -713,9 +713,9 @@
printf("[");
for (item = target->head; item; item = item->next)
{
- printf("%s", f(item->data));
+ printf("%s", f(item->data));
if (item->next)
- printf(",");
+ printf(",");
}
printf("]\n");
}
@@ -736,7 +736,7 @@
else if (strcmp(format, "%i") == 0)
printf(format, *((int*) (item->data)));
if (item->next)
- printf(",");
+ printf(",");
}
printf("]\n");
}
@@ -750,11 +750,11 @@
struct list_item* item = target->head;
while (item)
{
- struct list_item* next = item->next;
+ struct list_item* next = item->next;
free(item);
- item = next;
+ item = next;
}
- free(target);
+ free(target);
}
/**
@@ -764,42 +764,42 @@
list_destroy(struct list* target)
{
// NOTE_F;
-
+
struct list_item* item = target->head;
while (item)
{
struct list_item* next = item->next;
- free(item->data);
+ free(item->data);
free(item);
- item = next;
+ item = next;
}
- free(target);
+ free(target);
}
int
int_cmp(void* i1, void* i2)
{
int j1 = *(int*) i1;
- int j2 = *(int*) i2;
-
+ int j2 = *(int*) i2;
+
if (j1 > j2)
return 1;
else if (j1 < j2)
return -1;
else
- return 0;
+ return 0;
}
/**
- Returns 0 iff i1 is divisible by i2.
+ Returns 0 iff i1 is divisible by i2.
*/
int
divides_cmp(void* i1, void* i2)
{
int j1 = *(int*) i1;
- int j2 = *(int*) i2;
+ int j2 = *(int*) i2;
- return (j1 % j2);
+ return (j1 % j2);
}
#ifdef DEBUG_LIST
@@ -811,7 +811,7 @@
{
struct list* L = list_create();
- int zero = 0;
+ int zero = 0;
int one = 1;
int two = 2;
int three = 3;
@@ -819,8 +819,8 @@
int four2 = 4;
int five = 5;
int six = 6;
- int seven = 7;
- int eight = 8;
+ int seven = 7;
+ int eight = 8;
list_ordered_insert(L, &two, int_cmp);
list_ordered_insert(L, &four, int_cmp);
@@ -833,11 +833,11 @@
list_ordered_insert(L, &four2, int_cmp);
list_ordered_insert(L, &five, int_cmp);
list_ordered_insert(L, &one, int_cmp);
-
- list_push(L, &eight);
+ list_push(L, &eight);
+
list_dump("%i", L);
- printf("size: %i \n", L->size);
+ printf("size: %i \n", L->size);
// struct list* matches = list_select(L, int_cmp, &four);
// list_remove_where(L, divides_cmp, &two);
@@ -845,12 +845,12 @@
struct list* K = list_pop_where(L, divides_cmp, &two);
list_dump("%i", L);
- printf("size: %i \n", L->size);
+ printf("size: %i \n", L->size);
list_dump("%i", K);
- printf("size: %i \n", L->size);
-
- /*
+ printf("size: %i \n", L->size);
+
+ /*
list_dump("%i", L);
list_poll(L);
list_dump("%i", L);
@@ -863,26 +863,26 @@
// list_clobber(L);
list_clear(L);
- printf("size(L): %i \n", L->size);
+ printf("size(L): %i \n", L->size);
list_clear(K);
- printf("size(K): %i \n", K->size);
-
- list_dump("%i", L);
+ printf("size(K): %i \n", K->size);
+
+ list_dump("%i", L);
}
-#endif
+#endif
-/*
+/*
char* append_pair(char* ptr, struct list_item* item, char* s)
{
ptr += sprintf(ptr, "(%s,", item->data);
ptr += sprintf(ptr, "%s)", s);
-
+
if (item->next)
ptr += sprintf(ptr, ",");
- return ptr;
+ return ptr;
}
** Dump list to string a la snprintf()
@@ -890,36 +890,36 @@
format specifies the output format for the data items
returns int greater than size if size limits are exceeded
indicating result is garbage
-
+
int list_tostring(char* str, size_t size,
char* format, struct list* target)
{
- int error = size+1;
- char* ptr = str;
+ int error = size+1;
+ char* ptr = str;
struct list_item* item;
if (size <= 2)
- return error;
-
+ return error;
+
ptr += sprintf(ptr, "[");
char* s = (char*) malloc(sizeof(char)*LIST_MAX_DATUM);
-
+
for (item = target->head; item; item = item->next,
item && ptr-str < size)
{
int r = snprintf(s, LIST_MAX_DATUM, format, item->data);
if (r > LIST_MAX_DATUM)
- return size+1;
+ return size+1;
if ((ptr-str) + strlen(item->data) + r + 4 < size)
- ptr = append_pair(ptr, item, s);
+ ptr = append_pair(ptr, item, s);
else
- return error;
+ return error;
}
sprintf(ptr, "]");
- // free(s);
- return (ptr-str);
+ // free(s);
+ return (ptr-str);
}
*/
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|