📄 list.c
字号:
jmp_buf ins_b_Nth;
if (setjmp(ins_b_Nth) != 0) longjmp(ibfc, EXC_LIST_NOSUCHITEM);
List_InsertBeforeNth(this, new_item, before, 1, ins_b_Nth);
return;
} /* end of void List_InsertBeforeFirst(const List, Object, Object, jmp_buf) */
void List_InsertBeforeLast(const List this, Object new_item, Object before, jmp_buf iblc) /* throws NoSuchItem */ {
ListIterator iterator = List_ListIterator(this, ITERATOR_BACKWARD);
jmp_buf li_a, li_p;
for(; ListIterator_HasPrevious(iterator); )
if (this->compare_component(before, ListIterator_Previous(iterator, li_p)) == 0) {
ListIterator_Add(iterator, new_item, li_a);
free(iterator);
return;
}
free(iterator);
longjmp(iblc, EXC_LIST_NOSUCHITEM);
} /* end of void List_InsertBeforeLast(const List, Object, Object, jmp_buf) */
void List_InsertBeforeNth(const List this, Object new_item, Object before, int n, jmp_buf ibNc) /* throws NoSuchItem */ {
int index = 0;
ListIterator iterator = List_ListIterator(this, ITERATOR_FORWARD);
jmp_buf li_a, li_n, li_p;
for(; index < n && ListIterator_HasNext(iterator); )
if (this->compare_component(before, ListIterator_Next(iterator, li_n)) == 0) index++;
if (index < n) { free(iterator); longjmp(ibNc, EXC_LIST_NOSUCHITEM); }
ListIterator_Previous(iterator, li_p);
ListIterator_Add(iterator, new_item, li_a);
free(iterator);
return;
} /* end of void List_InsertBeforeNth(const List, Object, Object, int, jmp_buf) */
void List_InsertInAscendingOrder(const List this, Object new_item) {
ListIterator iterator = List_ListIterator(this, ITERATOR_BACKWARD);
jmp_buf li_a, li_n, li_p;
for (; ListIterator_HasPrevious(iterator); ) {
Object next_item = ListIterator_Previous(iterator, li_p);
if (this->compare_component(new_item, next_item) < 0) continue;
ListIterator_Next(iterator, li_n); ListIterator_Add(iterator, new_item, li_a);
free(iterator);
return;
}
ListIterator_Add(iterator, new_item, li_a);
free(iterator);
return;
} /* end of void List_InsertInAscendingOrder(const List, Object) */
void List_InsertInAscendingOrderEx(const List this, Object new_item, COMPARISON_FUNC cmp) {
ListIterator iterator = List_ListIterator(this, ITERATOR_BACKWARD);
jmp_buf li_a, li_n, li_p;
for (; ListIterator_HasPrevious(iterator); ) {
Object next_item = ListIterator_Previous(iterator, li_p);
if (cmp(new_item, next_item) < 0) continue;
ListIterator_Next(iterator, li_n); ListIterator_Add(iterator, new_item, li_a);
free(iterator);
return;
}
ListIterator_Add(iterator, new_item, li_a);
free(iterator);
return;
} /* end of void List_InsertInAscendingOrderEx(const List, Object, COMPARISON_FUNC) */
void List_InsertInDescendingOrder(const List this, Object new_item) {
ListIterator iterator = List_ListIterator(this, ITERATOR_BACKWARD);
jmp_buf li_a, li_n, li_p;
for (; ListIterator_HasPrevious(iterator); ) {
Object next_item = ListIterator_Previous(iterator, li_p);
if (this->compare_component(new_item, next_item) > 0) continue;
ListIterator_Next(iterator, li_n); ListIterator_Add(iterator, new_item, li_a);
free(iterator);
return;
}
ListIterator_Add(iterator, new_item, li_a);
free(iterator);
return;
} /* end of void List_InsertInDescendingOrder(const List, Object) */
void List_InsertInDescendingOrderEx(const List this, Object new_item, COMPARISON_FUNC cmp) {
ListIterator iterator = List_ListIterator(this, ITERATOR_BACKWARD);
jmp_buf li_a, li_n, li_p;
for (; ListIterator_HasPrevious(iterator); ) {
Object next_item = ListIterator_Previous(iterator, li_p);
if (cmp(new_item, next_item) > 0) continue;
ListIterator_Next(iterator, li_n); ListIterator_Add(iterator, new_item, li_a);
free(iterator);
return;
}
ListIterator_Add(iterator, new_item, li_a);
free(iterator);
return;
} /* end of void List_InsertInDescendingOrderEx(const List, Object, COMPARISON_FUNC) */
int List_RemoveAll(const List this, Object item, jmp_buf rac) /* throws ListEmpty */ {
int i = 0;
jmp_buf li_n, rmv;
ListIterator iterator;
if (List_IsEmpty(this)) longjmp(rac, EXC_LIST_EMPTY);
for (iterator = List_ListIterator(this, ITERATOR_FORWARD); ListIterator_HasNext(iterator); )
if (this->compare_component(item, ListIterator_Next(iterator, li_n)) == 0) {
ListIterator_Remove(iterator, rmv);
i++;
}
free(iterator);
return i;
} /* end of int List_RemoveAll(const List, Object, jmp_buf) */
void List_RemoveFirst(const List this, Object new_item, jmp_buf rfc) /* throws ListEmpty, NoSuchItem */ {
jmp_buf rmv_Nth_f;
switch (setjmp(rmv_Nth_f)) {
case 0: break;
case EXC_LIST_EMPTY: longjmp(rfc, EXC_LIST_EMPTY);
case EXC_LIST_NOSUCHITEM: longjmp(rfc, EXC_LIST_NOSUCHITEM);
}
List_RemoveNthFromFront(this, new_item, 1, rmv_Nth_f);
return;
} /* end of void List_RemoveFirst(const List, Object, jmp_buf) */
void List_RemoveLast(const List this, Object new_item, jmp_buf rlc) /* throws ListEmpty, NoSuchItem */ {
jmp_buf rmv_Nth_e;
int status;
status = setjmp(rmv_Nth_e);
switch (status) {
case 0: break;
case EXC_LIST_EMPTY: longjmp(rlc, EXC_LIST_EMPTY);
case EXC_LIST_NOSUCHITEM: longjmp(rlc, EXC_LIST_NOSUCHITEM);
}
List_RemoveNthFromEnd(this, new_item, 1, rmv_Nth_e);
return;
} /* end of void List_RemoveLast(const List, Object, jmp_buf) */
void List_RemoveNthFromEnd(const List this, Object new_item, int n, jmp_buf rNec) /* throws ListEmpty, NoSuchItem */ {
int i = 0;
jmp_buf li_p, rmv;
ListIterator iterator;
if (List_IsEmpty(this)) longjmp(rNec, EXC_LIST_EMPTY);
for (iterator = List_ListIterator(this, ITERATOR_BACKWARD); ListIterator_HasPrevious(iterator) && i < n; )
if (this->compare_component(new_item, ListIterator_Previous(iterator, li_p)) == 0) i++;
if (i == n) { ListIterator_Remove(iterator, rmv); free(iterator); }
else { free(iterator); longjmp(rNec, EXC_LIST_NOSUCHITEM); }
return;
} /* end of void List_RemoveNthFromEnd(const List, Object, int, jmp_buf) */
void List_RemoveNthFromFront(const List this, Object new_item, int n, jmp_buf rNfc) /* throws ListEmpty, NoSuchItem */ {
int i = 0;
jmp_buf li_n, rmv;
ListIterator iterator;
if (List_IsEmpty(this)) longjmp(rNfc, EXC_LIST_EMPTY);
for (iterator = List_ListIterator(this, ITERATOR_FORWARD); ListIterator_HasNext(iterator) && i < n; )
if (this->compare_component(new_item, ListIterator_Next(iterator, li_n)) == 0) i++;
if (i == n) { ListIterator_Remove(iterator, rmv); free(iterator); }
else { free(iterator); longjmp(rNfc, EXC_LIST_NOSUCHITEM); }
return;
} /* end of void List_RemoveNthFromFront(const List, Object, int, jmp_buf) */
void ListIterator_Destroy(ListIterator* this) {
if (*this == NULL) return;
(*this)->underlying_list = NULL;
free(*this);
*this = NULL;
return;
} /* end of void ListIterator_Destroy(ListIterator*) */
BOOL ListIterator_HasNext(const ListIterator this) {
if (this->index == this->underlying_list->size) return FALSE;
else return TRUE;
} /* end of BOOL ListIterator_HasNext(const ListIterator) */
Object ListIterator_Next(const ListIterator this, jmp_buf nc) /* throws NoSuchElement */ {
Object retVal;
if (!ListIterator_HasNext(this)) longjmp(nc, EXC_ITERATOR_NOSUCHELEMENT);
retVal = this->ptr->info;
this->ptr = this->ptr->next;
this->index++;
this->last_op = IT_OPERATION_NEXT;
return retVal;
} /* end of Object ListIterator_Next(const ListIterator, jmp_buf) */
int ListIterator_NextIndex(const ListIterator this) {
return(this->index);
} /* end of int ListIterator_NextIndex(const ListIterator) */
BOOL ListIterator_HasPrevious(const ListIterator this) {
if (this->index == 0) return FALSE;
else return TRUE;
} /* end of BOOL ListIterator_HasPrevious(const ListIterator) */
Object ListIterator_Previous(const ListIterator this, jmp_buf pc) /* throws NosuchElement */ {
if (!ListIterator_HasPrevious(this)) longjmp(pc,EXC_ITERATOR_NOSUCHELEMENT);
this->ptr = this->ptr->prev;
this->index--;
this->last_op = IT_OPERATION_PREVIOUS;
return this->ptr->info;
} /* end of BOOL ListIterator_Previous(const ListIterator, jmp_buf) */
int ListIterator_PreviousIndex(const ListIterator this) {
return (this->index - 1);
} /* end of int ListIterator_PreviousIndex(const ListIterator) */
void ListIterator_Remove(const ListIterator this, jmp_buf rc) /* throws IllegalStateException, UnsupportedOperationException */ {
_List to_be_deleted;
int index; /* index of the item to be deleted */
if (this->last_op != IT_OPERATION_NEXT && this->last_op != IT_OPERATION_PREVIOUS)
longjmp(rc, EXC_ITERATOR_ILLEGALSTATE);
switch (this->last_op) {
case IT_OPERATION_NEXT:
to_be_deleted = this->ptr->prev;
index = this->index - 1;
break;
case IT_OPERATION_PREVIOUS:
to_be_deleted = this->ptr;
this->ptr = this->ptr->next;
index = this->index++;
break;
} /* end of switch(this->lastOp) */
this->last_op = IT_OPERATION_REMOVE;
if (index + 1 == this->underlying_list->size) /* if it is the last item in the underlying list */ {
jmp_buf rmv_e;
if (setjmp(rmv_e) != 0) NO_OP;
List_RemoveFromEnd(this->underlying_list, rmv_e);
} else if (index == 0) /* if it is the first item in the list */ {
jmp_buf rmv_f;
if (setjmp(rmv_f) != 0) NO_OP;
List_RemoveFromFront(this->underlying_list, rmv_f);
} else {
_List next_list = to_be_deleted->next;
_List prev_list = to_be_deleted->prev;
prev_list->next = next_list; next_list->prev = prev_list;
this->underlying_list->destroy_component(to_be_deleted->info);
this->underlying_list->size--;
to_be_deleted->prev = to_be_deleted->next = NULL;
to_be_deleted->info = NULL;
free(to_be_deleted);
}
this->index--;
} /* end of void ListIterator_Remove(const ListIterator, jmp_buf) */
void ListIterator_Add(const ListIterator this, Object new_item, jmp_buf liac) /* throws IllegalState, UnsupportedOperation */ {
_List new_sublist;
if (!ListIterator_HasNext(this)) List_InsertAtEnd(this->underlying_list, new_item);
else if (!ListIterator_HasPrevious(this)) List_InsertInFront(this->underlying_list, new_item);
else {
new_sublist = create_sublist(new_item);
new_sublist->next = this->ptr;
new_sublist->prev = this->ptr->prev;
this->ptr->prev->next = new_sublist;
this->ptr->prev = new_sublist;
this->underlying_list->size++;
}
this->index++;
this->last_op = IT_OPERATION_ADD;
} /* end of void ListIterator_Add(const ListIterator, Object, jmp_buf) */
void ListIterator_Set(const ListIterator this, Object new_value, jmp_buf sc) /* throws IllegalArgument, IllegalState, UnsupportedOperation */ {
if (this->last_op == IT_OPERATION_NEXT) { this->underlying_list->destroy_component(this->ptr->prev->info); this->ptr->prev->info = new_value; }
else if (this->last_op == IT_OPERATION_PREVIOUS) { this->underlying_list->destroy_component(this->ptr->info); this->ptr->info = new_value; }
else longjmp(sc, EXC_ITERATOR_ILLEGALSTATE);
this->last_op = IT_OPERATION_SET;
return;
} /* end of void ListIterator_Set(const ListIterator, Object) */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -