SP_BPQUEUE_MSG spBPQueueDequeue(SPBPQueue source) {
	SPListElement first;
	SP_LIST_MSG actionStatus;

	spMinimalVerifyArguments(source != NULL, SP_BPQUEUE_INVALID_ARGUMENT);

	if (spBPQueueIsEmpty(source))
		return SP_BPQUEUE_EMPTY;

	first = spListGetFirst(source->queue);
	if (first == NULL)
		return SP_BPQUEUE_EMPTY;

	// if we have 1 items -> last is first -> we should free its pointer
	if (spBPQueueSize(source) == 1) {
		spListElementDestroy(source->maxElement);
		source->maxElement = NULL;
	}

	actionStatus = spListRemoveCurrent(source->queue);

	if (actionStatus != SP_LIST_SUCCESS)
		return SP_BPQUEUE_EMPTY;

	return SP_BPQUEUE_SUCCESS;
}
Exemple #2
0
SP_LIST_MSG spListClear(SPList list) {
	if (list == NULL) {
		return SP_LIST_NULL_ARGUMENT;
	}
	while (spListGetFirst(list)) {
		spListRemoveCurrent(list);
	}
	return SP_LIST_SUCCESS;
}
Exemple #3
0
SP_BPQUEUE_MSG spBPQueueEnqueue(SPBPQueue source, SPListElement element){

	// variables declaration//
	int i;
	SPList source_list = source->list;
	SPListElement elem_to_comp;
	int list_size = spListGetSize(source_list);

	// check for bad arguments or allocation problems//
	if (!source || !element || !source_list){
		return SP_BPQUEUE_INVALID_ARGUMENT;
	}


	// insertion process//
	if (spBPQueueIsEmpty(source)){
		spListInsertFirst(source_list, element);
		return SP_BPQUEUE_SUCCESS;
	}
	else{
		elem_to_comp = spListGetFirst(source_list);

		/*starts from the bottom and loop for next source list element up until
		you find the one which is bigger than the inserted one and insert the element
		before that one. you will stop after you ran out of elements to look for */
        
        for(i=0; i<list_size; i++){
            if (spListElementGetValue(element) <= spListElementGetValue(elem_to_comp)){// element is smaller than next elem, insert before current
                if (spBPQueueIsFull(source)){
                    if (spListInsertBeforeCurrent(source_list, element) == SP_LIST_OUT_OF_MEMORY){
                        return SP_BPQUEUE_OUT_OF_MEMORY;
                    }
                    spListGetLast(source_list);
                    spListRemoveCurrent(source_list);
                    return SP_BPQUEUE_SUCCESS;
                }
                else{
                    if (spListInsertBeforeCurrent(source_list, element) == SP_LIST_OUT_OF_MEMORY){
                        return SP_BPQUEUE_OUT_OF_MEMORY;
                    }
                    return SP_BPQUEUE_SUCCESS;
                }
            }else{
                elem_to_comp = spListGetNext(source_list);
            }
        }
    }
    if (!spBPQueueIsFull(source)){//queue isn't full but this item is the biggest
        if (spListInsertLast(source_list, element)== SP_LIST_OUT_OF_MEMORY){
            return SP_BPQUEUE_OUT_OF_MEMORY;
        }
        return SP_BPQUEUE_SUCCESS;
    }
    return SP_BPQUEUE_SUCCESS; // the element is larger than all others
}
SP_BPQUEUE_MSG spBPQueueEnqueue(SPBPQueue source, SPListElement element) {
	if (source == NULL || element == NULL) {
		return SP_BPQUEUE_INVALID_ARGUMENT;
	}

	bool wasFull = spBPQueueIsFull(source);

	SPList list = source->innerList;

	bool wasInserted = false;
	// Advance iterator to the proper element's place
	SP_LIST_FOREACH(SPListElement, currentElement, list) {

		if (spListElementCompare(element, currentElement) < 0) {

			// Modifying while iterating - bad practice but is more efficient here.
			SP_LIST_MSG returnMSG = spListInsertBeforeCurrent(list, element);

			switch (returnMSG) {
			case SP_LIST_OUT_OF_MEMORY:
				return SP_BPQUEUE_OUT_OF_MEMORY;
			default:
				break;
			}
			wasInserted = true;
			break;
		}
	}

	if (wasInserted) {
		// Remove the last element if needed
		if (wasFull) {
			spListGetLast(list);
			spListRemoveCurrent(list);
		}
		return SP_BPQUEUE_SUCCESS;
	} else {
		if (spBPQueueIsFull(source)) {
			return SP_BPQUEUE_FULL;
		} else {
			// The item is largest than everything, insert last
			SP_LIST_MSG returnMSG = spListInsertLast(list, element);
			switch (returnMSG) {
			case SP_LIST_OUT_OF_MEMORY:
				return SP_BPQUEUE_OUT_OF_MEMORY;
			default:
				return SP_BPQUEUE_SUCCESS;
			}
		}
	}
}
SP_BPQUEUE_MSG spBPQueueDequeue(SPBPQueue source) {
	if (source == NULL) {
		return SP_BPQUEUE_INVALID_ARGUMENT;
	}

	if (spBPQueueIsEmpty(source)) {
		return SP_BPQUEUE_EMPTY;
	}

	spListGetFirst(source->innerList);
	spListRemoveCurrent(source->innerList);

	return SP_BPQUEUE_SUCCESS;
}
static bool testListGetSize() {
	SPList list = quickList(0);
	ASSERT_TRUE(0 == spListGetSize(list));
	SPListElement e1 = spListElementCreate(1, 1.0);
	spListInsertFirst(list, e1);
	ASSERT_TRUE(1 == spListGetSize(list));
	spListElementSetIndex(e1, 2);
	spListElementSetValue(e1, 2.0);
	spListInsertFirst(list, e1);
	ASSERT_TRUE(2 == spListGetSize(list));
	spListGetFirst(list);
	spListRemoveCurrent(list);
	ASSERT_TRUE(1 == spListGetSize(list));
	spListDestroy(list);
	spListElementDestroy(e1);
	return true;
}
Exemple #7
0
SP_BPQUEUE_MSG spBPQueueDequeue(SPBPQueue source){
    
    // check for bad arguments//
    if(!source || !source->list){
        return SP_BPQUEUE_INVALID_ARGUMENT;
    }
	
    // variables declaration//
	SPList source_list = source->list;

	if (spBPQueueIsEmpty(source)){
		return SP_BPQUEUE_EMPTY;
	}
	else{
		spListGetFirst(source_list);
		spListRemoveCurrent(source_list);
		return SP_BPQUEUE_SUCCESS;
	}
}
SP_BPQUEUE_MSG spBPQueueHandleFullCapacity(SPBPQueue source) {
	int i;
	SPListElement currElemInQueue, prevElemInQueue;
	currElemInQueue = spListGetFirst(source->queue);
	prevElemInQueue = currElemInQueue;

	for (i=1;i<spBPQueueSize(source);i++) {
		prevElemInQueue = currElemInQueue;
		currElemInQueue = spListGetNext(source->queue);
	}

	// if we get here we assume we never reached the end of the list (meaning null)
	// because we assume spBPQueueSize is valid
	spListRemoveCurrent(source->queue);

	spListElementDestroy(source->maxElement);
	source->maxElement = spListElementCopy(prevElemInQueue);
	if (source->maxElement == NULL)
		return SP_BPQUEUE_OUT_OF_MEMORY;

	return SP_BPQUEUE_SUCCESS;

}