Esempio n. 1
0
/**
 * Adds a new element at the specified index. If index is greater than
 * the list size the element is appended.
 * The element is dinamically duplicated so that the caller can release
 * the element at any time. This method returns the position (0 based) at which
 * the element has been inserted. It can be different by index if index is out
 * of the array bounds, in that case element is appended as last element.
 * It returns -1 in case of errors.
 *
 * @param index the insertion position
 * @param element the element to insert - NULL is not allowed!
 */
int ArrayList::add(int index, ArrayElement& element) {
    if (index < 0) {
        return -1;
    }

    int s = size();
    if (index > s) {
        index = s;
    }

    Element* newElement = new Element();

    newElement->e = element.clone();
    newElement->n = NULL;

    Element* e;
    if (index == s) {
        // Inserting the new element at the end
        e = lastElement;

    }
    else {
    // Inserting the new element at the index-th position
        e = head;
    for (int i=0; i<index-1; ++i) {
        e = e->n;
    }
    }

    if (e == NULL || index == 0) {
        //
        // Insertion at the beginning of the array
        //
        newElement->n = head;
        head = newElement;

        if (e == NULL) {
            lastElement = newElement;
        }
        count++;
        return index;
    }

    if (e->n) {
        //
        // Insertion in the middle of the array
        //
        newElement->n = e->n;
        e->n = newElement;
    } else {
        //
        // Insertion at the end of the array
        //
        e->n = newElement;
        lastElement = newElement;
    }

    count++;

    return index;
}
Esempio n. 2
0
/**
 * Adds a new element at the specified index. If index is greater than
 * the list size the element is appended.
 * The element is dinamically duplicated so that the caller can release
 * the element at any time. This method returns the position (0 based) at which
 * the element has been inserted. It can be different by index if index is out
 * of the array bounds, in that case element is appended as last element.
 * It returns -1 in case of errors.
 *
 * @param index the insertion position
 * @param element the element to insert - NULL is not allowed!
 */
int ArrayList::add(int index, ArrayElement& element) {
    if (index < 0) {
        return -1;
    }

    int s = size();
    if (index > s) {
        index = s;
    }

    Element* newElement = new Element();

    newElement->e = element.clone();
    newElement->n = NULL;

    Element* e;
    if (index == s) {
        // Inserting the new element at the end
        e = lastElement;

    }
    else {
        // Inserting the new element at the index-th position
        e = head;
        for (int i=0; i<index-1; ++i) {
            e = e->n;
        }
    }

    // Check if we are replacing the ghost or one of its neighbors
    if (iterator == &ghost) {
        if (e && ghost.n == e->n) {
            iterator = newElement;
        } else if (e == NULL && ghost.n == head) {
            iterator = newElement;
        }
    }

    if (e == NULL || index == 0) {
        //
        // Insertion at the beginning of the array
        //
        newElement->n = head;
        head = newElement;

        if (e == NULL) {
            lastElement = newElement;
        }
        count++;
        return index;
    }

    if (e->n) {
        //
        // Insertion in the middle of the array
        //
        newElement->n = e->n;
        e->n = newElement;
    } else {
        //
        // Insertion at the end of the array
        //
        e->n = newElement;
        lastElement = newElement;
    }


    count++;

    return index;
}