growableArray.hpp

Go to the documentation of this file.
00001 /* Copyright 1994, 1995 LongView Technologies L.L.C. $Revision: 1.15 $ */
00002 /* Copyright (c) 2006, Sun Microsystems, Inc.
00003 All rights reserved.
00004 
00005 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 
00006 following conditions are met:
00007 
00008     * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
00009     * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following 
00010           disclaimer in the documentation and/or other materials provided with the distribution.
00011     * Neither the name of Sun Microsystems nor the names of its contributors may be used to endorse or promote products derived 
00012           from this software without specific prior written permission.
00013 
00014 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 
00015 NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 
00016 THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
00017 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
00018 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
00019 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
00020 
00021 
00022 */
00023 
00024 // A growable array.
00025 
00026 typedef void (*voidDoFn)(void* p);
00027 typedef bool (*growableArrayFindFn)(void* token, void* elem);
00028 
00029 class GenericGrowableArray : public PrintableResourceObj {
00030  protected:
00031   int    len;           // current length
00032   int    max;           // maximum length
00033   void** data;          // data array
00034   bool   on_C_heap;     // is data allocated on C heap?
00035 
00036   void grow(int j);     // grow data array (double length until j is a valid index)
00037 
00038   bool  raw_contains(const void* p) const;
00039   int   raw_find(const void* p) const; 
00040   int   raw_find(void* token, growableArrayFindFn f) const; 
00041   void  raw_remove(const void* p);
00042   void  raw_apply(voidDoFn f) const;
00043   void* raw_at_grow(int i, const void* fill);
00044   void  raw_at_put_grow(int i, const void* p, const void* fill);
00045   void  raw_appendAll(GenericGrowableArray* l);
00046   GenericGrowableArray* raw_copy() const;
00047   void  raw_sort(int f(const void *, const void *));
00048 
00049   GenericGrowableArray(int initial_size, bool on_C_heap = false);
00050   GenericGrowableArray(int initial_size, int initial_len, void* filler, bool on_C_heap = false);
00051 
00052  public:
00053   void  clear()                 { len = 0; }
00054   int   length() const          { return len; }
00055   int   capacity() const        { return max; }
00056   bool  isEmpty() const         { return len == 0; }
00057   bool  nonEmpty() const        { return len != 0; }
00058   bool  isFull() const          { return len == max; }
00059   void** data_addr() const      { return data; }        // for sorting
00060 
00061   void print_short();
00062   void print();
00063 };
00064 
00065 template<class E> class GrowableArray : public GenericGrowableArray {
00066  public:
00067   GrowableArray(int initial_size, bool on_C_heap = false) : GenericGrowableArray(initial_size, on_C_heap) {}
00068   GrowableArray(int initial_size, int initial_len, E filler, bool on_C_heap = false) : GenericGrowableArray(initial_size, initial_len, (void*)filler, on_C_heap) {}
00069   GrowableArray() : GenericGrowableArray(2) {}
00070 
00071   void append(const E elem) {
00072     if (len == max) grow(len);
00073     data[len++] = (void*) elem;
00074   }
00075 
00076   E at(int i) const {
00077     assert(0 <= i && i < len, "illegal index");
00078     return (E) data[i];
00079   }
00080 
00081   E first() const {
00082     assert(len > 0, "empty list");
00083     return (E) data[0];
00084   }
00085 
00086   E last() const {                                                  
00087     assert(len > 0, "empty list");
00088     return (E) data[len-1];
00089   }
00090 
00091   void push(const E elem) { append(elem); }
00092 
00093   E pop() {
00094     assert(len > 0, "empty list");
00095     return (E) data[--len];
00096   }
00097 
00098   E top() const { return last(); }
00099 
00100   void at_put(int i, const E elem) {
00101     assert(0 <= i && i < len, "illegal index");
00102     data[i] = (void*) elem;
00103   }
00104 
00105   E at_grow(int i) {
00106     assert(0 <= i, "negative index");
00107     return (E) raw_at_grow(i, NULL);
00108   }
00109 
00110   void at_put_grow(int i, const E elem) {
00111     assert(0 <= i, "negative index");
00112     raw_at_put_grow(i, (void*) elem, NULL);
00113   }
00114 
00115   void apply(Closure<E>* c) const {
00116     for (int i = 0; i < len; i++) c->do_it((E)data[i]);
00117   }
00118 
00119   bool contains(const E elem) const             { return raw_contains((const void*) elem);           }
00120   int find(const E elem) const                  { return raw_find((const void*) elem);               }
00121   int find(void* token, bool f(void*, E)) const { return raw_find(token, (growableArrayFindFn)f);    }
00122   void remove(const E elem)                     { raw_remove((const void*) elem);                    }
00123   void apply(void f(E)) const                   { raw_apply((voidDoFn)f);                            }
00124   GrowableArray<E>* copy() const                { return (GrowableArray<E>*) raw_copy();             }
00125   void appendAll(GrowableArray<E>* l)           { raw_appendAll((GenericGrowableArray*)l);           }
00126   void sort(int f(E*,E*))                       { raw_sort((int (*)(const void *, const void *)) f); }
00127 };
00128 

Generated on Mon Oct 9 13:37:10 2006 for Strongtalk VM by  doxygen 1.4.7