home *** CD-ROM | disk | FTP | other *** search
- /*---------------------------------------------------------------------------
- *
- * Copyright (c) 1994 by Westmount Technology B.V., Delft, The Netherlands.
- *
- * This software is furnished under a license and may be used only in
- * accordance with the terms of such license and with the inclusion of
- * the above copyright notice. This software or any other copies thereof
- * may not be provided or otherwise made available to any other person.
- * No title to and ownership of the software is hereby transferred.
- *
- * The information in this software is subject to change without notice
- * and should not be construed as a commitment by Westmount Technology B.V.
- *
- *---------------------------------------------------------------------------
- *
- * File : @(#)CList.cxx 1.1
- * Author : peku
- * Original date : 5-1-1994
- * Description : Compact Lists
- *
- *---------------------------------------------------------------------------
- */
- static const char SccsId[]="@(#)CList.cxx 1.1 18 Oct 1994 Copyright 1994 Westmount Technology";
-
- #ifndef CLIST_HXX
- #include "CList.hxx"
- #endif
-
- static void
- shift_left ( register void** p, register void** q, void** endp)
- {
- while ( q < endp ) *p++ = *q++;
- }
-
- static void
- shift_right ( void** startp, register void** q, void** p)
- {
- while ( q >= startp ) *p-- = *q--;
- }
-
-
- GenCList::GenCList(unsigned size, int gf):
- GenFlexArray(size),
- idx (0)
- {
- if ( gf > 0 ) grow_factor = gf * DEFAULT_ARRAY_SIZE;
- else if ( gf < 0 ) grow_factor = gf - 1;
- }
-
-
- GenCList::GenCList(const GenCList& cpy):
- GenFlexArray(cpy),
- idx(cpy.idx),
- grow_factor(cpy.grow_factor)
- {
- /* empty */
- }
-
-
- GenCList&
- GenCList::operator= (const GenCList& asgn)
- {
- this->GenFlexArray::operator= (asgn);
- idx = asgn.idx;
- grow_factor = asgn.grow_factor;
-
- return *this;
- }
-
-
- void
- GenCList::reset()
- {
- idx = 0;
- }
-
-
- void
- GenCList::reset ( unsigned new_size )
- {
- GenFlexArray::reSize(new_size);
- idx = 0;
- }
-
-
-
- int
- GenCList::grow(unsigned min_size)
- {
- if ( sz < min_size && grow_factor == 0) {
- return 0;
- }
- while ( sz < min_size && v )
- reSize( (grow_factor>0) ? sz + grow_factor : sz * (-grow_factor) );
-
- return v != 0;
- }
-
-
- GenCList&
- GenCList::append (void* e)
- {
- if ( idx < sz || grow(sz+1) ) v[idx++] = e;
- return *this;
- }
-
- GenCList&
- GenCList::append (const GenCList& lst)
- {
- if ( lst.idx == 0 ) return *this; // appending empty list?!
- if ( idx+lst.idx < sz || grow (idx+lst.idx) ) {
- shift_left ( &v[idx], lst.v, &lst.v[lst.idx] );
- idx += lst.idx;
- }
- return *this;
- }
-
- GenCList&
- GenCList::insert_at(int i, void* elem)
- {
- if ( i == idx )
- return append(elem); // insert at the end
-
- else if ( idx < sz || grow(sz+1) ) {
- shift_right ( &v[i], &v[idx-1], &v[idx] );
- v[i] = elem;
- idx++;
- }
-
- return *this;
-
- }
-
- GenCList&
- GenCList::insert_at(int i, const GenCList& lst)
- {
- if ( lst.idx == 0 ) return *this; // appending empty list?!
-
- else if ( i == idx )
- return append(lst);
-
- else if ( idx+lst.idx < sz || grow (idx+lst.idx) ) {
- if ( &lst == this ) {
- // copy the same list in two parts:
- shift_right ( &v[i], &v[idx-1], &v[2*idx-1] ); // make room
- shift_right ( &v[0], &v[i-1], &v[2*i-1] ); // copy all before i
- shift_left ( &v[2*i], &v[idx+i], &v[2*idx] ); // copy shifted part
- }
- else {
- shift_right ( &v[i], &v[idx-1], &v[idx+lst.idx-1] );
- shift_left ( &v[i], lst.v, &lst.v[lst.idx] );
- }
- idx += lst.idx;
- }
- return *this;
- }
-
- GenCList&
- GenCList::remove_last()
- {
- idx--;
- return *this;
- }
-
- GenCList&
- GenCList::remove_at(int i)
- {
- if ( i == idx )
- remove_last();
-
- else {
- shift_left ( &v[i], &v[i+1], &v[idx] );
- idx--;
- }
-
- return *this;
- }
-
- GenCList&
- GenCList::remove(int i, int j)
- {
- if ( 0 > i || i > j || j >= idx ) { return *this; }
-
- if ( j == idx - 1) {
- // remove up to end: no shifting required
- idx = i;
- }
- else {
- shift_left ( &v[i], &v[j+1], &v[idx] );
- void** endp = &v[idx];
- idx = idx - 1 - j + i;
- }
- return *this;
- }
-
- GenCList&
- GenCList::remove(void *elem)
- {
- register int i, end;
- for (i = 0, end = size(); i < end; ++i)
- if (at(i) == elem) {
- remove_at(i);
- return *this;
- }
- return *this;
- }
-