Example #1
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
}
static bool testElementSetValue() {
	ASSERT_TRUE(spListElementGetValue(NULL) == -1.0);
	ASSERT_TRUE(spListElementSetValue(NULL,-1.0) ==SP_ELEMENT_INVALID_ARGUMENT);
	ASSERT_TRUE(spListElementSetValue(NULL,1.0) ==SP_ELEMENT_INVALID_ARGUMENT);
	ASSERT_TRUE(spListElementSetValue(NULL,-1.0) ==SP_ELEMENT_INVALID_ARGUMENT);
	SPListElement element = spListElementCreate(1, 0.0);
	ASSERT_TRUE(
			spListElementSetValue(element, -1.0)
					== SP_ELEMENT_INVALID_ARGUMENT);
	ASSERT_TRUE(spListElementGetValue(element) == 0.0);
	ASSERT_TRUE(spListElementSetValue(element, 1.0) == SP_ELEMENT_SUCCESS);
	ASSERT_TRUE(spListElementGetValue(element) == 1.0);
	spListElementDestroy(element);
	return true;
}
static bool testIsElementGetValue() {
	SPListElement element1 = spListElementCreate(1, 0.0);
	SPListElement element2 = spListElementCreate(2, 1.0);
	SPListElement element3 = spListElementCreate(3, 1.0);
	SPListElement element4 = spListElementCreate(4, 2.0);
	ASSERT_TRUE(spListElementGetValue(element1) == 0.0);
	ASSERT_TRUE(spListElementGetValue(element2) == 1.0);
	ASSERT_TRUE(spListElementGetValue(element3) == 1.0);
	ASSERT_TRUE(spListElementGetValue(element4) == 2.0);
	spListElementDestroy(element1);
	spListElementDestroy(element2);
	spListElementDestroy(element3);
	spListElementDestroy(element4);
	return true;
}
Example #4
0
double spBPQueueMaxValue(SPBPQueue source){
	assert(source);
	if (spBPQueueIsEmpty(source)){
		return -1;
	}
	return spListElementGetValue(spListGetLast(source->list));
}
double returnValueFrom(SPBPQueue source, SPListElement (*func)(SPBPQueue)) {
	SPListElement item;
	double returnValue;
	spMinimalVerifyArguments(source != NULL, DEFAULT_INVALID_NUMBER);

	item = (*func)(source);
	returnValue = spListElementGetValue(item);
	spListElementDestroy(item);

	return returnValue;
}
double spBPQueueMaxValue(SPBPQueue source) {
	return spListElementGetValue(spBPQueueLastElement(source));
}
double spBPQueueMinValue(SPBPQueue source) {
	return spListElementGetValue(spBPQueueFirstElement(source));
}
/**
 * Creates a separate copy of a node
 *
 * @param node to be copied
 * @return
 * NULL in case allocation failure occurred
 * Otherwise, the new node is returned
 */
 struct Qnode* copyNode(struct Qnode *n){
     return newNode(spListElementGetIndex(n->element), spListElementGetValue(n->element));
 }
double spBPQueueMaxValue(SPBPQueue source){
    if (!source || source->size==0) return -1;
    return spListElementGetValue(source->last->element);
}
SP_BPQUEUE_MSG spBPQueueEnqueue(SPBPQueue source, SPListElement element){
    int i;
    struct Qnode* new_node;
    struct Qnode* n;
    struct Qnode* oldNext;

    if (!source || !element){
        return SP_BPQUEUE_INVALID_ARGUMENT;
    }


    // create new node using the element's data
    new_node = newNode(spListElementGetIndex(element), spListElementGetValue(element));


    if (!new_node){
        return SP_BPQUEUE_OUT_OF_MEMORY;
    }

    n = source->first;

    // if queue is full
    if (source->size >= source->maxSize){
        // compare the new element to the last element
        if (spListElementCompare(new_node->element, source->last->element) == 1){
            spListElementDestroy(new_node->element);
            free(new_node);
            return SP_BPQUEUE_FULL;
        }
        // new element is smaller, therefore destroy current last node
        spListElementDestroy(source->last->element);
        free(source->last);

        // set the new last node pointer to NULL
        for(i=0;i<source->size-2;i++){
            n=n->next;
        }
        n->next=NULL;
    }

    n = source->first;

    // if queue is empty
    if (!source->first){
        source->first = new_node;
        source->last = new_node;
    } else {
        if (spListElementCompare(n->element, new_node->element) == 1){
            // in case new node is smallest
            source->first = new_node;
            new_node->next = n;
        } else {
            while (n->next && spListElementCompare(n->next->element, new_node->element) == -1){
                n = n->next;
            }
            oldNext = n->next;
            n->next = new_node;
            new_node->next = oldNext;
        }
    }

    n = source->first;
    while (n->next){
       n = n->next;
    }
    source->last = n;

    // if queue is not full, increment size
    if (source->size != source->maxSize){
        source->size += 1;
    }

	return SP_BPQUEUE_SUCCESS;
}