X-Git-Url: http://git.cinelerra-gg.org/git/?a=blobdiff_plain;f=cinelerra-5.1%2Fguicast%2Farraylist.h;fp=cinelerra-5.1%2Fguicast%2Farraylist.h;h=5f66b34102a5d96163242f9b815627101d5e4df7;hb=30bdb85eb33a8ee7ba675038a86c6be59c43d7bd;hp=0000000000000000000000000000000000000000;hpb=52fcc46226f9df46f9ce9d0566dc568455a7db0b;p=goodguy%2Fhistory.git diff --git a/cinelerra-5.1/guicast/arraylist.h b/cinelerra-5.1/guicast/arraylist.h new file mode 100644 index 00000000..5f66b341 --- /dev/null +++ b/cinelerra-5.1/guicast/arraylist.h @@ -0,0 +1,105 @@ +#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; + for(int i=n+1; i::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