PsmAddress Sm_list_insert(const char *fileName, int lineNbr, PsmPartition partition, PsmAddress list, PsmAddress data, SmListCompareFn compare, void *argData) { SmList *listBuffer; PsmAddress elt; SmListElt *eltBuffer; if (compare == (SmListCompareFn) NULL) { /* List is assumed to be unsorted. We simply add the new element at the end of the list. */ return Sm_list_insert_last(fileName, lineNbr, partition, list, data); } /* Using user-specified comparison function. List is assumed to be in sorted order. */ CHKZERO(partition); CHKZERO(list); listBuffer = (SmList *) psp(partition, list); CHKZERO(listBuffer); if (lockSmlist(listBuffer) == ERROR) { putErrmsg(_cannotLockMsg(), NULL); return 0; } /* Find position to insert new data into list. Start from end of list to keep sort stable; sort sequence is implicitly FIFO within key, i.e., we insert element AFTER the last element in the table with the same key value. */ for (elt = listBuffer->last; elt != 0; elt = eltBuffer->prev) { eltBuffer = (SmListElt *) psp(partition, elt); CHKZERO(eltBuffer); if (compare(partition, eltBuffer->data, argData) <= 0) { break; } } /* insert into list */ if (elt == 0) { return finishInsertingFirst(fileName, lineNbr, partition, list, listBuffer, data); } return finishInsertingAfter(fileName, lineNbr, partition, elt, eltBuffer, list, listBuffer, data); }
Object Sdr_list_insert_last(const char *file, int line, Sdr sdr, Object list, Address data) { return Sm_list_insert_last(file, line, sdr, list, data); }