Example #1
0
    // Override of the List interface
    List* Append( VALUE elem ) {
#ifdef USE_CONS_OPTIMIZATIONS
        List* tail = this;
        while (!tail->Rest().IsEmpty() ) {
            tail = tail->Rest().GetList();
        }
        if (tail->RefCount < 2 && tail->Rest().IsEmpty()) {
            // We can optimize things as we can reuse this materalization.
            // Nobody is referencing it.
            if (tail->AttemptDirtyAdd(elem)) {
                return this;
            };
        }
#endif
        int originalCount = Count();
        int last = Count()-1;
        List* c = VALUE(QParenthesis,elem).GetList();
        while (last >= 0) {
            c = c->Prepend( GetAt(last) );
            last--;
        }
        int newCount = c->Count();
        assert( newCount == originalCount + 1 );
//        std::cout << "\nBefore:" << LIST(this).Print();
//        std::cout << "\nAdded:" << elem.Print();
//        std::cout << "\nAfter:" << LIST(c).Print() << "\n";
        return c;
    }