ArrayClass.hpp
Go to the documentation of this file.
1 /*----------------------------------------------------------------------------*/
2 /* */
3 /* Copyright (c) 1995, 2004 IBM Corporation. All rights reserved. */
4 /* Copyright (c) 2005-2009 Rexx Language Association. All rights reserved. */
5 /* */
6 /* This program and the accompanying materials are made available under */
7 /* the terms of the Common Public License v1.0 which accompanies this */
8 /* distribution. A copy is also available at the following address: */
9 /* http://www.oorexx.org/license.html */
10 /* */
11 /* Redistribution and use in source and binary forms, with or */
12 /* without modification, are permitted provided that the following */
13 /* conditions are met: */
14 /* */
15 /* Redistributions of source code must retain the above copyright */
16 /* notice, this list of conditions and the following disclaimer. */
17 /* Redistributions in binary form must reproduce the above copyright */
18 /* notice, this list of conditions and the following disclaimer in */
19 /* the documentation and/or other materials provided with the distribution. */
20 /* */
21 /* Neither the name of Rexx Language Association nor the names */
22 /* of its contributors may be used to endorse or promote products */
23 /* derived from this software without specific prior written permission. */
24 /* */
25 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS */
26 /* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT */
27 /* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS */
28 /* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT */
29 /* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, */
30 /* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED */
31 /* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, */
32 /* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY */
33 /* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING */
34 /* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
35 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
36 /* */
37 /*----------------------------------------------------------------------------*/
38 /******************************************************************************/
39 /* REXX Kernel ArrayClass.hpp */
40 /* */
41 /* Primitive Array Class Definitions */
42 /* */
43 /******************************************************************************/
44 #ifndef Included_RexxArray
45 #define Included_RexxArray
46 
47 #define RaiseBoundsNone 0x00000000
48 #define RaiseBoundsUpper 0x00000001
49 #define RaiseBoundsInvalid 0x00000002
50 #define RaiseBoundsTooMany 0x00000004
51 #define RaiseBoundsAll 0x0000000F
52 #define ExtendUpper 0x00000010
53 
54 
55 typedef struct copyElementParm {
60  size_t deltaDimSize;
61  size_t copyElements;
62  size_t skipElements;
66 
68  public:
69  enum {
70  SmallRange = 10 // the size where we revert to an insertion sort
71  };
72 
73  PartitionBounds(size_t l, size_t r) : left(l), right(r) {}
74  PartitionBounds() : left(0), right(0) {}
75 
76  inline bool isSmall() { return (right - left) <= SmallRange; }
77  inline size_t midPoint() { return (left + right) / 2; }
78 
79  size_t left; // start of the range
80  size_t right;
81  };
82 
83 
85  public:
86  inline BaseSortComparator() { }
87 
88  virtual wholenumber_t compare(RexxObject *first, RexxObject *second);
89  };
90 
92  public:
94  virtual wholenumber_t compare(RexxObject *first, RexxObject *second);
95  protected:
97  };
98 
99 
100  class RexxArray : public RexxObject {
101  public:
102 
103  inline void * operator new(size_t size, void *objectPtr) { return objectPtr; };
104  void * operator new(size_t, RexxObject **, size_t, RexxClass *);
105  void * operator new(size_t, RexxObject *);
106  void * operator new(size_t, RexxObject *, RexxObject *);
107  void * operator new(size_t, RexxObject *, RexxObject *, RexxObject *);
108  void * operator new(size_t, RexxObject *, RexxObject *, RexxObject *, RexxObject *);
109  void * operator new(size_t, RexxObject *, RexxObject *, RexxObject *, RexxObject *, RexxObject *);
110  void * operator new(size_t, size_t, RexxObject **);
111  void * operator new(size_t, size_t, size_t, RexxClass *cls = TheArrayClass);
112 
113  inline void operator delete(void *) {;}
114  inline void operator delete(void *, void *) {;}
115  inline void operator delete(void *, RexxObject **, size_t, RexxClass *) {;}
116  inline void operator delete(void *, RexxObject *) {;}
117  inline void operator delete(void *, RexxObject *, RexxObject *) {;}
118  inline void operator delete(void *, RexxObject *, RexxObject *, RexxObject *) {;}
119  inline void operator delete(void *, RexxObject *, RexxObject *, RexxObject *, RexxObject *) {;}
120  inline void operator delete(void *, RexxObject *, RexxObject *, RexxObject *, RexxObject *, RexxObject *) {;}
121  inline void operator delete(void *, size_t, RexxObject **) {;}
122  inline void operator delete(void *, size_t, size_t, RexxClass *cls) {;}
123  inline void operator delete(void *, RexxObject **) { ; }
124 
125  inline RexxArray(RESTORETYPE restoreType) { ; };
126  inline RexxArray() { ; };
127  inline ~RexxArray() { ; };
128 
129  void init(size_t, size_t);
130  void live(size_t);
131  void liveGeneral(int reason);
132  void flatten(RexxEnvelope *);
133  RexxObject *copy();
134  RexxArray *makeArray();
139 // Temporary bypass for BUG #1700606
140 #if 0
142 #endif
143  RexxObject *getRexx(RexxObject **, size_t, size_t);
144  RexxObject *getApi(size_t pos);
145  void put(RexxObject * eref, size_t pos);
146  RexxObject *putRexx(RexxObject **, size_t, size_t);
147  void putApi(RexxObject * eref, size_t pos);
148  RexxObject *remove(size_t);
149  RexxObject *removeRexx(RexxObject **, size_t, size_t);
151  size_t append(RexxObject *);
152  void setExpansion(RexxObject * expansion);
153  RexxInteger *available(size_t position);
154  bool validateIndex(RexxObject **, size_t, size_t, size_t, stringsize_t &);
157  RexxObject *lastRexx();
159  RexxObject *lastItem();
160  size_t lastIndex();
161  RexxObject *nextRexx(RexxObject **, size_t, size_t);
162  RexxObject *previousRexx(RexxObject **, size_t, size_t);
163  RexxArray *section(size_t, size_t);
165  RexxObject *sectionSubclass(size_t, size_t);
166  bool hasIndexNative(size_t);
167  RexxObject *hasIndexRexx(RexxObject **, size_t, size_t);
168  bool hasIndexApi(size_t);
169  size_t items();
173  size_t getDimension();
176  RexxArray *extend(size_t);
177  void shrink(size_t);
178  size_t indexOf(RexxObject *);
179  RexxArray *extendMulti(RexxObject **, size_t, size_t);
180  void resize();
181  void ensureSpace(size_t newSize);
182  RexxObject *newRexx(RexxObject **, size_t, size_t);
183  RexxObject *of(RexxObject **, size_t, size_t);
184  RexxObject *empty();
185  RexxObject *isEmpty();
194  size_t insert(RexxObject *_value, size_t index);
196  RexxObject *deleteItem(size_t index);
197 
198  inline size_t addLast(RexxObject *item) { return this->insert(item, this->size() + 1); }
199  inline size_t addFirst(RexxObject *item) { return this->insert(item, 1); }
200  inline size_t insertAfter(RexxObject *item, size_t index) { return this->insert(item, index); }
201  inline RexxArray *array() { return this->makeArray(); }
202  inline size_t size() { return this->expansionArray->arraySize; }
203  inline RexxObject *get(size_t pos) { return (this->data())[pos-1];}
204  inline RexxObject **data() { return this->expansionArray->objects; }
205  inline RexxObject **data(size_t pos) { return &((this->data())[pos-1]);}
206  inline RexxArray *getExpansion() { return this->expansionArray; }
207  size_t findSingleIndexItem(RexxObject *item);
208  RexxObject * indexToArray(size_t idx);
209  RexxObject * convertIndex(size_t idx);
210 
211  inline bool isMultiDimensional() { return this->dimensions != OREF_NULL && this->dimensions->size() != 1; }
212  inline bool isSingleDimensional() { return !isMultiDimensional(); }
213 
214  static RexxArray *createMultidimensional(RexxObject **dims, size_t count, RexxClass *);
215 
216  static void createInstance();
217  // singleton class instance;
220 
221  static const size_t ARRAY_MIN_SIZE;
222  static const size_t ARRAY_DEFAULT_SIZE; // default size for ooRexx allocation
223 
224  protected:
225 
226  void mergeSort(BaseSortComparator &comparator, RexxArray *working, size_t left, size_t right);
227  void merge(BaseSortComparator &comparator, RexxArray *working, size_t left, size_t mid, size_t right);
228  static void arraycopy(RexxArray *source, size_t start, RexxArray *target, size_t index, size_t count);
229  size_t find(BaseSortComparator &comparator, RexxObject *val, int bnd, size_t left, size_t right);
230  void openGap(size_t index, size_t elements);
231  void closeGap(size_t index, size_t elements);
232  inline RexxObject **slotAddress(size_t index) { return &(this->data()[index - 1]); }
233  inline size_t dataSize() { return ((char *)slotAddress(size() + 1)) - ((char *)data()); }
234 
235 
236  static const size_t MAX_FIXEDARRAY_SIZE;
237 
238  size_t arraySize; /* current size of array */
239  size_t maximumSize; /* Maximum size array can grow */
240  size_t lastElement; // location of last set element
241  RexxArray *dimensions; /* Array containing dimensions - null if 1-dimensional */
242  RexxArray *expansionArray; /* actual array containing data */
243  RexxObject *objects[1]; /* Data. */
244  };
245 
246 
247 inline RexxArray *new_externalArray(size_t s, RexxClass *c)
248 {
249  return new (s, RexxArray::ARRAY_DEFAULT_SIZE, c) RexxArray;
250 }
251 
252 /**
253  * Create an array with a given size.
254  *
255  * @param s The size of the array.
256  *
257  * @return The new array item.
258  */
259 inline RexxArray *new_array(size_t s)
260 {
262 }
263 
264 
265 /**
266 * Make a zero-length array item.
267 *
268 * @return A new array.
269 */
271 {
272  return new_array((size_t)0);
273 }
274 
275 
276 /**
277  * Create an array populated with objects from another source.
278  *
279  * @param s The number of objects.
280  * @param o The pointer to the set of objects.
281  *
282  * @return A new array object.
283  */
284 inline RexxArray *new_array(size_t s, RexxObject **o)
285 {
286  return new (s, o) RexxArray;
287 }
288 
289 
290 /**
291  * Create a new array with one item.
292  *
293  * @param o1 The object to add to the array.
294  *
295  * @return A new array object.
296  */
298 {
299  return new (o1) RexxArray;
300 }
301 
302 
303 /**
304  * Create a new array with two items.
305  *
306  * @param o1 The first object to add to the array.
307  * @param o2 The second object to add
308  *
309  * @return A new array object.
310  */
312 {
313  return new (o1, o2) RexxArray;
314 }
315 
316 
317 /**
318  * Create a new array with three items.
319  *
320  * @param o1 The first object to add to the array.
321  * @param o2 The second object to add
322  * @param o3 The third object to add.
323  *
324  * @return A new array object.
325  */
327 {
328  return new (o1, o2, o3) RexxArray;
329 }
330 
331 
332 /**
333  * Create a new array with four items.
334  *
335  * @param o1 The first object to add to the array.
336  * @param o2 The second object to add
337  * @param o3 The third object to add.
338  * @param o4 The fourth object to add.
339  *
340  * @return A new array object.
341  */
343 {
344  return new (o1, o2, o3, o4) RexxArray;
345 }
346 
347 
348 /**
349  * Create a new array with five items.
350  *
351  * @param o1 The first object to add to the array.
352  * @param o2 The second object to add
353  * @param o3 The third object to add.
354  * @param o4 The fourth object to add.
355  * @param o5 The fifth object to add.
356  *
357  * @return A new array object.
358  */
360 {
361  return new (o1, o2, o3, o4, o5) RexxArray;
362 }
363 
364  #endif
RexxArray * new_externalArray(size_t s, RexxClass *c)
Definition: ArrayClass.hpp:247
struct copyElementParm COPYELEMENTPARM
RexxArray * new_array(size_t s)
Definition: ArrayClass.hpp:259
RESTORETYPE
Definition: ObjectClass.hpp:82
#define OREF_NULL
Definition: RexxCore.h:61
#define TheArrayClass
Definition: RexxCore.h:155
virtual wholenumber_t compare(RexxObject *first, RexxObject *second)
size_t midPoint()
Definition: ArrayClass.hpp:77
PartitionBounds(size_t l, size_t r)
Definition: ArrayClass.hpp:73
RexxObject * lastItem()
RexxObject ** slotAddress(size_t index)
Definition: ArrayClass.hpp:232
static const size_t ARRAY_DEFAULT_SIZE
Definition: ArrayClass.hpp:222
RexxObject * isEmpty()
Definition: ArrayClass.cpp:301
static const size_t ARRAY_MIN_SIZE
Definition: ArrayClass.hpp:221
RexxObject * getRexx(RexxObject **, size_t, size_t)
Definition: ArrayClass.cpp:506
size_t findSingleIndexItem(RexxObject *item)
size_t addFirst(RexxObject *item)
Definition: ArrayClass.hpp:199
static const size_t MAX_FIXEDARRAY_SIZE
Definition: ArrayClass.hpp:236
void flatten(RexxEnvelope *)
Definition: ArrayClass.cpp:179
RexxObject ** data(size_t pos)
Definition: ArrayClass.hpp:205
static void createInstance()
Definition: ArrayClass.cpp:95
bool hasIndexNative(size_t)
size_t getDimension()
Definition: ArrayClass.cpp:693
void live(size_t)
Definition: ArrayClass.cpp:146
void put(RexxObject *eref, size_t pos)
Definition: ArrayClass.cpp:208
RexxArray(RESTORETYPE restoreType)
Definition: ArrayClass.hpp:125
RexxObject * insertRexx(RexxObject *_value, RexxObject *index)
Definition: ArrayClass.cpp:344
void closeGap(size_t index, size_t elements)
Definition: ArrayClass.cpp:447
RexxArray * dimensions
Definition: ArrayClass.hpp:241
RexxObject * of(RexxObject **, size_t, size_t)
RexxObject * supplier(RexxObject *maxItems=OREF_NULL)
Definition: ArrayClass.cpp:786
size_t find(BaseSortComparator &comparator, RexxObject *val, int bnd, size_t left, size_t right)
size_t insertAfter(RexxObject *item, size_t index)
Definition: ArrayClass.hpp:200
static RexxClass * classInstance
Definition: ArrayClass.hpp:218
RexxObject * copy()
Definition: ArrayClass.cpp:122
RexxObject * remove(size_t)
Definition: ArrayClass.cpp:593
bool hasIndexApi(size_t)
Definition: ArrayClass.cpp:576
static void arraycopy(RexxArray *source, size_t start, RexxArray *target, size_t index, size_t count)
static RexxArray * nullArray
Definition: ArrayClass.hpp:219
void resize()
RexxObject * getApi(size_t pos)
Definition: ArrayClass.cpp:536
size_t indexOf(RexxObject *)
size_t maximumSize
Definition: ArrayClass.hpp:239
RexxObject * firstRexx()
wholenumber_t sortCompare(RexxObject *comparator, RexxObject *left, RexxObject *right)
RexxInteger * available(size_t position)
Definition: ArrayClass.cpp:834
RexxInteger * sizeRexx()
size_t lastElement
Definition: ArrayClass.hpp:240
RexxObject * putRexx(RexxObject **, size_t, size_t)
Definition: ArrayClass.cpp:228
RexxObject * join(RexxArray *)
RexxObject * newRexx(RexxObject **, size_t, size_t)
RexxArray * allItems(RexxObject *maxItems=OREF_NULL)
void openGap(size_t index, size_t elements)
Definition: ArrayClass.cpp:412
bool isMultiDimensional()
Definition: ArrayClass.hpp:211
size_t arraySize
Definition: ArrayClass.hpp:238
RexxObject * nextRexx(RexxObject **, size_t, size_t)
RexxArray * allIndexes(RexxObject *maxIndexes=OREF_NULL)
RexxObject * deleteRexx(RexxObject *index)
Definition: ArrayClass.cpp:385
RexxObject * convertIndex(size_t idx)
RexxArray * array()
Definition: ArrayClass.hpp:201
size_t append(RexxObject *)
Definition: ArrayClass.cpp:485
RexxObject * appendRexx(RexxObject *)
Definition: ArrayClass.cpp:314
RexxObject * itemsRexx()
Definition: ArrayClass.cpp:685
size_t size()
Definition: ArrayClass.hpp:202
RexxObject * empty()
Definition: ArrayClass.cpp:273
void ensureSpace(size_t newSize)
RexxObject * index(RexxObject *)
size_t dataSize()
Definition: ArrayClass.hpp:233
void setExpansion(RexxObject *expansion)
Definition: ArrayClass.cpp:826
size_t insert(RexxObject *_value, size_t index)
RexxArray * stableSortRexx()
RexxObject * sectionSubclass(size_t, size_t)
RexxObject * removeItem(RexxObject *)
bool isSingleDimensional()
Definition: ArrayClass.hpp:212
RexxArray * section(size_t, size_t)
RexxObject * hasItem(RexxObject *)
void liveGeneral(int reason)
Definition: ArrayClass.cpp:164
size_t addLast(RexxObject *item)
Definition: ArrayClass.hpp:198
void putApi(RexxObject *eref, size_t pos)
Definition: ArrayClass.cpp:554
RexxObject * deleteItem(size_t index)
RexxObject * removeRexx(RexxObject **, size_t, size_t)
Definition: ArrayClass.cpp:626
RexxObject * objects[1]
Definition: ArrayClass.hpp:243
size_t lastIndex()
RexxObject * lastRexx()
RexxObject * fill(RexxObject *)
Definition: ArrayClass.cpp:253
size_t items()
Definition: ArrayClass.cpp:663
RexxObject * getDimensions()
Definition: ArrayClass.cpp:714
RexxArray * stableSortWithRexx(RexxObject *comparator)
bool validateIndex(RexxObject **, size_t, size_t, size_t, stringsize_t &)
Definition: ArrayClass.cpp:843
RexxObject * dimension(RexxObject *)
Definition: ArrayClass.cpp:729
RexxArray * extend(size_t)
RexxArray * makeArray()
RexxObject * hasIndexRexx(RexxObject **, size_t, size_t)
RexxObject * sectionRexx(RexxObject *, RexxObject *)
RexxObject * previousRexx(RexxObject **, size_t, size_t)
RexxObject * get(size_t pos)
Definition: ArrayClass.hpp:203
RexxArray * extendMulti(RexxObject **, size_t, size_t)
RexxArray * getExpansion()
Definition: ArrayClass.hpp:206
static RexxArray * createMultidimensional(RexxObject **dims, size_t count, RexxClass *)
RexxString * toString(RexxString *, RexxString *)
void mergeSort(BaseSortComparator &comparator, RexxArray *working, size_t left, size_t right)
RexxArray * expansionArray
Definition: ArrayClass.hpp:242
RexxObject ** data()
Definition: ArrayClass.hpp:204
void merge(BaseSortComparator &comparator, RexxArray *working, size_t left, size_t mid, size_t right)
RexxObject * indexToArray(size_t idx)
RexxObject * firstItem()
void shrink(size_t)
virtual RexxString * primitiveMakeString()
RexxMessage * start(RexxObject **, size_t, size_t)
RexxString * makeString()
RexxObject * init()
virtual wholenumber_t compare(RexxObject *first, RexxObject *second)
RexxObject * comparator
Definition: ArrayClass.hpp:96
WithSortComparator(RexxObject *c)
Definition: ArrayClass.hpp:93
ssize_t wholenumber_t
Definition: rexx.h:230
size_t stringsize_t
Definition: rexx.h:228
RexxArray * oldDimArray
Definition: ArrayClass.hpp:59
RexxObject ** startOld
Definition: ArrayClass.hpp:64
size_t firstChangedDimension
Definition: ArrayClass.hpp:56
size_t skipElements
Definition: ArrayClass.hpp:62
RexxArray * newDimArray
Definition: ArrayClass.hpp:58
size_t deltaDimSize
Definition: ArrayClass.hpp:60
RexxObject ** startNew
Definition: ArrayClass.hpp:63
size_t copyElements
Definition: ArrayClass.hpp:61
RexxArray * newArray
Definition: ArrayClass.hpp:57