00001
00002
00003 #include "list.h"
00004
00005 List *newList(Region *r) {
00006 List *l = (List *)region_alloc(r, sizeof (List));
00007 l->head = l->tail = NULL;
00008 l->size = 0;
00009 return l;
00010 }
00011
00012 List *newListNoRegion() {
00013 List *l = (List *)malloc(sizeof (List));
00014 l->head = l->tail = NULL;
00015 l->size = 0;
00016 return l;
00017 }
00018
00019 ListNode *newListNodeNoRegion(void *value) {
00020 ListNode *l = (ListNode *)malloc(sizeof (ListNode));
00021 l->next = NULL;
00022 l->value = value;
00023 return l;
00024 }
00025 ListNode *newListNode(void *value, Region *r) {
00026 ListNode *l = (ListNode *)region_alloc(r, sizeof (ListNode));
00027 l->next = NULL;
00028 l->value = value;
00029 return l;
00030 }
00031
00032
00033 void listAppendNoRegion(List *list, void *value) {
00034 ListNode *ln = newListNodeNoRegion(value);
00035 if(list->head != NULL) {
00036 list->tail = list->tail->next = ln;
00037 } else {
00038 list->head = list->tail = ln;
00039
00040 }
00041 list->size++;
00042 }
00043 void listAppend(List *list, void *value, Region *r) {
00044 ListNode *ln = newListNode(value, r);
00045 if(list->head != NULL) {
00046 list->tail = list->tail->next = ln;
00047 } else {
00048 list->head = list->tail = ln;
00049 }
00050 list->size++;
00051 }
00052
00053 void listAppendToNode(List *list, ListNode *node, void *value, Region *r) {
00054 ListNode *ln = newListNode(value, r);
00055 if(node->next != NULL) {
00056 ln->next = node->next;
00057 node->next = ln;
00058 } else {
00059 node->next = list->tail = ln;
00060 }
00061 list->size++;
00062 }
00063
00064 void listRemove(List *list, ListNode *node) {
00065 ListNode *prev = NULL, *curr = list->head;
00066 while(curr != NULL) {
00067 if(curr == node) {
00068 if(prev == NULL) {
00069 list->head = node->next;
00070 } else {
00071 prev->next = node->next;
00072 }
00073
00074 break;
00075 }
00076 prev = curr;
00077 curr = curr->next;
00078 }
00079 if(list->tail == node) {
00080 list->tail = prev;
00081 }
00082 list->size--;
00083
00084 }
00085 void listRemoveNoRegion2(List *l, void *v) {
00086 ListNode *node = l->head;
00087 while(node!=NULL) {
00088 if(node->value == v) {
00089 listRemoveNoRegion(l, node);
00090 break;
00091 }
00092 node = node->next;
00093 }
00094 }
00095 void listRemoveNoRegion(List *list, ListNode *node) {
00096 ListNode *prev = NULL, *curr = list->head;
00097 while(curr != NULL) {
00098 if(curr == node) {
00099 if(prev == NULL) {
00100 list->head = node->next;
00101 } else {
00102 prev->next = node->next;
00103 }
00104 free(node);
00105 break;
00106 }
00107 prev = curr;
00108 curr = curr->next;
00109 }
00110 if(list->tail == node) {
00111 list->tail = prev;
00112 }
00113 list->size--;
00114 }
00115
00116 void deleteListNoRegion(List *list) {
00117
00118 free(list);
00119 }
00120 void clearListNoRegion(List *list) {
00121 while(list->head != NULL) listRemoveNoRegion(list, list->head);
00122 }
00123
00124
00125