# HG changeset patch # User jwe # Date 815483241 0 # Node ID 1a35c8c913497d65aa47caa94c75b8a80baf4b3b # Parent 9c1fca4bd859707e979f1263a13f9ad75e5e8377 [project @ 1995-11-04 11:07:21 by jwe] diff --git a/liboctave/Array-idx.h b/liboctave/Array-idx.h --- a/liboctave/Array-idx.h +++ b/liboctave/Array-idx.h @@ -21,6 +21,7 @@ */ +#include "error.h" #include "idx-vector.h" #include "lo-error.h" @@ -38,8 +39,10 @@ template void -ArrayRep::clear_index (void) +Array::clear_index (void) { + cerr << "clearing index for " << this << "\n"; + delete [] idx; idx = 0; idx_count = 0; @@ -47,8 +50,10 @@ template void -ArrayRep::set_index (const idx_vector& i) +Array::set_index (const idx_vector& i) { + cerr << "setting index for " << this << "\n"; + if (! idx) idx = new idx_vector [max_indices]; @@ -60,6 +65,7 @@ { (*current_liboctave_error_handler) ("invalid number of indices specified"); + clear_index (); } } diff --git a/liboctave/Array.cc b/liboctave/Array.cc --- a/liboctave/Array.cc +++ b/liboctave/Array.cc @@ -49,12 +49,6 @@ { len = n; data = new T [len]; - -#ifdef HEAVYWEIGHT_INDEXING - idx = 0; - max_indices = 0; - idx_count = 0; -#endif } template @@ -66,26 +60,12 @@ data = new T [len]; for (int i = 0; i < len; i++) data[i] = a.data[i]; - -#ifdef HEAVYWEIGHT_INDEXING - max_indices = a.max_indices; - idx_count = a.idx_count; - if (a.idx) - { - idx_vector *idx = new idx_vector [max_indices]; - for (int i = 0; i < max_indices; i++) - idx[i] = a.idx[i]; - } - else - idx = 0; -#endif } template ArrayRep::~ArrayRep (void) { delete [] data; - delete [] idx; } template @@ -109,9 +89,28 @@ Array::Array (int n, const T& val) { rep = new ArrayRep (n); + rep->count = 1; + for (int i = 0; i < n; i++) rep->data[i] = val; + +#ifdef HEAVYWEIGHT_INDEXING + max_indices = 1; + idx_count = 0; + idx = 0; +#endif +} + +template +Array::~Array (void) +{ + if (--rep->count <= 0) + delete rep; + +#ifdef HEAVYWEIGHT_INDEXING + delete [] idx; +#endif } template @@ -126,6 +125,13 @@ rep = a.rep; rep->count++; } + +#ifdef HEAVYWEIGHT_INDEXING + max_indices = 1; + idx_count = 0; + idx = 0; +#endif + return *this; } @@ -190,8 +196,6 @@ rep = new ArrayRep (n); rep->count = 1; - SET_MAX_INDICES (1); - if (old_data && old_len > 0) { int min_len = old_len < n ? old_len : n; @@ -224,8 +228,6 @@ rep = new ArrayRep (n); rep->count = 1; - SET_MAX_INDICES (1); - int min_len = old_len < n ? old_len : n; if (old_data && old_len > 0) @@ -327,8 +329,6 @@ rep = new ArrayRep (r*c); rep->count = 1; - SET_MAX_INDICES (2); - d1 = r; d2 = c; @@ -368,8 +368,6 @@ rep = new ArrayRep (r*c); rep->count = 1; - SET_MAX_INDICES (2); - d1 = r; d2 = c; @@ -583,8 +581,6 @@ rep = new ArrayRep (new_len); rep->count = 1; - SET_MAX_INDICES (2); - nr = r; nc = c; @@ -622,8 +618,6 @@ rep = new ArrayRep (new_len); rep->count = 1; - SET_MAX_INDICES (2); - nr = r; nc = c; diff --git a/liboctave/Array.h b/liboctave/Array.h --- a/liboctave/Array.h +++ b/liboctave/Array.h @@ -47,12 +47,6 @@ template class DiagArray; #endif -#ifdef HEAVYWEIGHT_INDEXING -#define SET_MAX_INDICES(n) set_max_indices (n) -#else -#define SET_MAX_INDICES(n) -#endif - // The real representation of all arrays. template @@ -74,24 +68,12 @@ int count; int len; -#ifdef HEAVYWEIGHT_INDEXING - idx_vector *idx; - int max_indices; - int idx_count; -#endif - protected: ArrayRep (T *d, int l) { data = d; len = l; - -#ifdef HEAVYWEIGHT_INDEXING - idx = 0; - max_indices = 0; - idx_count = 0; -#endif } public: @@ -100,12 +82,6 @@ { data = 0; len = 0; - -#ifdef HEAVYWEIGHT_INDEXING - idx = 0; - max_indices = 0; - idx_count = 0; -#endif } ArrayRep (int n); @@ -119,18 +95,6 @@ T& elem (int n); T elem (int n) const; - -#ifdef HEAVYWEIGHT_INDEXING - void set_max_indices (int mi) { max_indices = mi; } - - void clear_index (void); - - void set_index (const idx_vector& i); - - int index_count (void) const { return idx_count; } - - idx_vector *get_idx (void) const { return idx; } -#endif }; // One dimensional array class. Handles the reference counting for @@ -139,6 +103,14 @@ template class Array { +private: + +#ifdef HEAVYWEIGHT_INDEXING + idx_vector *idx; + int max_indices; + int idx_count; +#endif + protected: ArrayRep *rep; @@ -147,7 +119,12 @@ { rep = new ArrayRep (d, l); rep->count = 1; - set_max_indices (1); + +#ifdef HEAVYWEIGHT_INDEXING + idx = 0; + max_indices = 1; + idx_count = 0; +#endif } public: @@ -156,14 +133,24 @@ { rep = new ArrayRep (); rep->count = 1; - set_max_indices (1); + +#ifdef HEAVYWEIGHT_INDEXING + idx = 0; + max_indices = 1; + idx_count = 0; +#endif } Array (int n) { rep = new ArrayRep (n); rep->count = 1; - set_max_indices (1); + +#ifdef HEAVYWEIGHT_INDEXING + idx = 0; + max_indices = 1; + idx_count = 0; +#endif } Array (int n, const T& val); @@ -172,13 +159,15 @@ { rep = a.rep; rep->count++; + +#ifdef HEAVYWEIGHT_INDEXING + max_indices = a.max_indices; + idx_count = 0; + idx = 0; +#endif } - ~Array (void) - { - if (--rep->count <= 0) - delete rep; - } + ~Array (void); Array& operator = (const Array& a); @@ -215,15 +204,15 @@ T *fortran_vec (void); #ifdef HEAVYWEIGHT_INDEXING - void set_max_indices (int mi) { rep->set_max_indices (mi); } + void set_max_indices (int mi) { max_indices = mi; } - void clear_index (void) { rep->clear_index (); } + void clear_index (void); - void set_index (const idx_vector& i) { rep->set_index (i); } + void set_index (const idx_vector& i); - int index_count (void) const { return rep->index_count (); } + int index_count (void) const { return idx_count; } - idx_vector *get_idx (void) const { return rep->get_idx (); } + idx_vector *get_idx (void) const { return idx; } void maybe_delete_elements (idx_vector& i);