Exemplo n.º 1
0
void *
Coll_List_popFront(
    Coll_List *_this)
{
    Coll_Iter *element_to_remove;
    void *object = NULL;

    assert(_this);

    element_to_remove = _this->_first_element;

    if (element_to_remove){
        object = Coll_Iter_getObject(element_to_remove);

        /*  detach element from linked list */
        _this->_first_element = Coll_Iter_getNext(element_to_remove);
        if (_this->_first_element){
          Coll_Iter_setPrev(_this->_first_element, NULL);
        } else {
          _this->_last_element = NULL;
        }
        _this->_nr_elements--;

        /*  delete the element */
        Coll_Iter_delete(element_to_remove);

    }
    return object;
}
Exemplo n.º 2
0
/* but sometimes you are just into italian food... */
void*
Coll_Set_remove(
    Coll_Set *_this,
    void *object)
{
    void* returnVal = NULL;
    Coll_Iter *iter;
    Coll_Iter *prev;
    Coll_Iter *next;

    DLRL_INFO_OBJECT(INF_ENTER);

    assert(_this);
    /* object may be null */

    iter = Coll_Set_find(_this, object);
    if (iter){
        returnVal = Coll_Iter_getObject(iter);
        prev = Coll_Iter_getPrev(iter);
        next = Coll_Iter_getNext(iter);

        /*  delink the found element from the set */
        if (prev){
            Coll_Iter_setNext(prev, next);
        }
        if (next){
            Coll_Iter_setPrev(next, prev);
        }

        /*  update set administration */
        if (iter == _this->_first_element){
            _this->_first_element = next;
        }
        if (iter == _this->_last_element){
            _this->_last_element = prev;
        }
        _this->_nr_elements--;

        /*  Free element */
        Coll_Iter_delete(iter);
    }
    DLRL_INFO(INF_EXIT);
    return returnVal;
}
Exemplo n.º 3
0
long
Coll_Set_add(
    Coll_Set *_this,
    void *object)
{
    long retValue;
    Coll_Iter *new_element;

    DLRL_INFO_OBJECT(INF_ENTER);

    assert(_this);
    /* object may be null */

    if (!_this->_is_sorted && !Coll_Set_find(_this, object)){
        /*  set is unsorted and does not contain the to be added object => the object can be added as a unique object */
        retValue = Coll_Set_addUniqueObject(_this, object);
    } else {
        /*  set is sorted, add the object at the right place */
        new_element = Coll_Iter_new();
        if (!new_element){
            retValue = COLL_ERROR_ALLOC;
        } else {
            Coll_Iter_setObject(new_element, object);

            if ( (_this->_nr_elements == 0) || (_this->_isLess(object, Coll_Iter_getObject(_this->_first_element) ) ) ){
                /*  first object to insert */
                Coll_Iter_setNext(new_element, _this->_first_element);
                if (_this->_first_element){
                    Coll_Iter_setPrev(_this->_first_element, new_element);
                }
                _this->_first_element = new_element;

                /*  update set administration */
                _this->_nr_elements++;
            } else {
                int ready = 0;

                /*  insert object at the right place */
                Coll_Iter *iter = _this->_first_element;

                while (!ready) {
                    int isLess = _this->_isLess(object, Coll_Iter_getObject(iter));

                    if ( !(isLess || _this->_isLess(Coll_Iter_getObject(iter), object)) ) {
                        /*  equal => element already exists in set => delete iterator object again */
                        Coll_Iter_delete(new_element);
                        ready = 1;
                    } else if (isLess) {
                        /*  smaller => insert the new element here */
                        Coll_Iter_setNext(new_element, iter);
                        Coll_Iter_setPrev(new_element, Coll_Iter_getPrev(iter));
                        Coll_Iter_setNext(Coll_Iter_getPrev(iter), new_element);
                        Coll_Iter_setPrev(iter, new_element);

                        /*  update set administration */
                       _this->_nr_elements++;

                        ready = 1;
                    } else if (Coll_Iter_getNext(iter) == NULL){
                        /*  append new element to the end of the set */
                        Coll_Iter_setPrev(new_element, iter);
                        Coll_Iter_setNext(iter, new_element);

                        /*  update set administration */
                        _this->_last_element = new_element;
                        _this->_nr_elements++;

                        ready = 1;
                    } else {
                        /*  do nothing */
                    }
                    iter = Coll_Iter_getNext(iter);
                }
            }
            retValue = COLL_OK;
        }
    }
    DLRL_INFO(INF_EXIT);
    return retValue;
}