growableArray.cpp

Go to the documentation of this file.
00001 /* Copyright 1994 - 1996 LongView Technologies L.L.C. $Revision: 1.16 $ */
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 # include "incls/_precompiled.incl"
00025 # include "incls/_growableArray.cpp.incl"
00026 
00027 GenericGrowableArray::GenericGrowableArray(int initial_size, bool c_heap) {
00028   len = 0;
00029   max = initial_size;
00030   assert(len <= max, "initial_size too small");
00031   on_C_heap = c_heap;
00032   if (c_heap) {
00033     data = (void**)AllocateHeap(max * sizeof(void*), "bounded list");
00034   } else {
00035     data = NEW_RESOURCE_ARRAY( void*, max);
00036   }
00037 }
00038 
00039 GenericGrowableArray::GenericGrowableArray(int initial_size, int initial_len, void* filler, bool c_heap) {
00040   len = initial_len;
00041   max = initial_size;
00042   assert(len <= max, "initial_len too big");
00043   on_C_heap = c_heap;
00044   if (c_heap) {
00045     data = (void**)AllocateHeap(max * sizeof(void*), "bounded list");
00046   } else {
00047     data = NEW_RESOURCE_ARRAY( void*, max);
00048   }
00049   for (int i = 0; i < len; i++) data[i] = filler;
00050 }
00051 
00052 void GenericGrowableArray::grow(int j) {
00053   void** newData;
00054   if (max == 0) fatal("cannot grow array with max = 0"); // for debugging - should create such arrays with max > 0
00055   while (j >= max) max = max*2;
00056   // j < max
00057   if (on_C_heap) {
00058     newData = (void**)AllocateHeap(max * sizeof(void*), "bounded list");
00059   } else {
00060     newData = NEW_RESOURCE_ARRAY( void*, max);
00061   }
00062   for (int i = 0; i < len; i++) newData[i] = data[i];
00063   data = newData;
00064 }
00065 
00066 bool GenericGrowableArray::raw_contains(const void* elem) const {
00067   for (int i = 0; i < len; i++) {
00068     if (data[i] == elem) return true;
00069   }
00070   return false;
00071 }
00072 
00073 GenericGrowableArray* GenericGrowableArray::raw_copy() const {
00074   GenericGrowableArray* copy = new GenericGrowableArray(max, len, NULL);
00075   for (int i = 0; i < len; i++) {
00076     copy->data[i] = data[i];
00077   }
00078   return copy;
00079 }
00080 
00081 void GenericGrowableArray::raw_appendAll(GenericGrowableArray* l) {
00082   for (int i = 0; i < l->len; i++) {
00083     raw_at_put_grow(len, l->data[i], NULL);
00084   }
00085 }
00086 
00087 int GenericGrowableArray::raw_find(const void* elem) const {
00088   for (int i = 0; i < len; i++) {
00089     if (data[i] == elem) return i;
00090   }
00091   return -1;
00092 }
00093 
00094 int GenericGrowableArray::raw_find(void* token, growableArrayFindFn f) const  {
00095   for (int i = 0; i < len; i++) {
00096     if (f(token, data[i])) return i;
00097   }
00098   return -1;
00099 }
00100 
00101 void GenericGrowableArray::raw_remove(const void* elem) {
00102   for (int i = 0; i < len; i++) {
00103     if (data[i] == elem) {
00104       for (int j = i + 1; j < len; j++) data[j-1] = data[j];
00105       len--;
00106       return;
00107     }
00108   }
00109   ShouldNotReachHere();
00110 }
00111 
00112 void GenericGrowableArray::raw_apply(voidDoFn f) const {
00113   for (int i = 0; i < len; i++) f(data[i]);
00114 }
00115 
00116 void* GenericGrowableArray::raw_at_grow(int i, const void* fill) {
00117   if (i >= len) {
00118     if (i >= max) grow(i);
00119     for (int j = len; j <= i; j++) data[j] = (void*)fill;
00120     len = i+1;
00121   }
00122   return data[i];
00123 }
00124 
00125 void GenericGrowableArray::raw_at_put_grow(int i, const void* p, const void* fill) {
00126   if (i >= len) {
00127     if (i >= max) grow(i);
00128     for (int j = len; j < i; j++) data[j] = (void*)fill;
00129     len = i+1;
00130   }
00131   data[i] = (void*)p;
00132 }
00133 
00134 void GenericGrowableArray::print() {
00135   print_short();
00136   lprintf(": length %ld (max %ld) { ", len, max);
00137   for (int i = 0; i < len; i++) lprintf("%#lx ", (long)data[i]);
00138   lprintf("}\n");
00139 }
00140 
00141 
00142 void GenericGrowableArray::raw_sort(int f(const void *, const void *)) {
00143   qsort(data, length(), sizeof(void*), f);
00144 }
00145 
00146 void GenericGrowableArray::print_short() { std->print("Growable Array %#lx", this); }

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