#ifndef ARRAYLIST_H #define ARRAYLIST_H #include #include template class ArrayList { enum { d_none, d_delete, d_array, d_free, }; int avail, dtype; void reallocate(int n); void del(TYPE &value) { switch( dtype ) { case d_delete: delete value; break; case d_array: delete [] value; break; case d_free: free((void*)value); break; } } void del_value(int i) { del(values[i]); } static int cmpr(TYPE *a, TYPE *b) { if( *a == *b ) return 0; return *a > *b ? 1 : -1; } public: int total; TYPE* values; void allocate(int total) { if( total >= avail ) reallocate(total); } TYPE &append() { int i = total++; if( total >= avail ) reallocate(total*2); return values[i]; } TYPE &append(TYPE value) { return append() = value; } TYPE &insert(TYPE value, int n) { append(); for(int i=total; --i>n; ) values[i]=values[i-1]; return values[n] = value; } void remove() { --total; } void remove_object() { if( total > 0 ) { del_value(total-1); remove(); } else fprintf(stderr, "ArrayList::remove_object: array is 0 length.\n"); } void remove_number(int n) { if( n >= total ) return; while( ++n::remove_object_number:" " number %d out of range %d.\n", i, total); } int number_of(TYPE value) { for( int i=0; i::get number=%d total=%d\n",i,total); return 0; } TYPE set(int i, TYPE value) { if( i < total ) return values[i] = value; fprintf(stderr,"ArrayList::set number=%d total=%d\n",i,total); return 0; } TYPE &operator [](int i) { return values[i]; } void sort(int (*cmp)(TYPE *a, TYPE *b) = 0) { return qsort(values, size(), sizeof(TYPE), (int(*)(const void *, const void *))(cmp ? cmp : cmpr)); } ArrayList() { total = 0; dtype = d_delete; values = new TYPE[avail = 16]; } ~ArrayList() { delete [] values; } }; template void ArrayList::reallocate(int n) { TYPE* newvalues = new TYPE[avail=n]; for( int i=total; --i>=0; ) newvalues[i] = values[i]; delete [] values; values = newvalues; } #endif