ooRexx  4.2.0-source
About: ooRexx (Open Object Rexx) is a free implementation of Object Rexx. Object Rexx is an enhancement of the classic Rexx interpreter; a full-featured programming language with a human-oriented syntax.
  Fossies Dox: ooRexx-4.2.0-source.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

RexxCollection.cpp
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 RexxCollection.cpp */
40 /* */
41 /* Primitive HashTableCollection Class */
42 /* */
43 /******************************************************************************/
44 
45 #include "RexxCore.h"
46 #include "RexxCollection.hpp"
47 #include "StringClass.hpp"
48 #include "ArrayClass.hpp"
49 #include "DirectoryClass.hpp"
50 #include "Interpreter.hpp"
51 
52 void RexxHashTableCollection::live(size_t liveMark)
53 /******************************************************************************/
54 /* Function: Normal garbage collection live marking */
55 /******************************************************************************/
56 {
57  memory_mark(this->contents);
58  memory_mark(this->objectVariables);
59 }
60 
62 /******************************************************************************/
63 /* Function: Generalized object marking */
64 /******************************************************************************/
65 {
67  memory_mark_general(this->objectVariables);
68 }
69 
71 /******************************************************************************/
72 /* Function: Flatten an object */
73 /******************************************************************************/
74 {
76 
77  flatten_reference(newThis->contents, envelope);
78  flatten_reference(newThis->objectVariables, envelope);
79 
81 }
82 
84 /******************************************************************************/
85 /* Function: unflatten an object */
86 /******************************************************************************/
87 {
88  envelope->addTable(this);
89  return this;
90 }
91 
93 /******************************************************************************/
94 /* Function: Create a proxy object for a standard collection */
95 /******************************************************************************/
96 {
97  /* Determine which special object */
98  /* this is and compute code for it. */
99  if (this == TheEnvironment) /* the environment directory */
100  {
101  return new_proxy(CHAR_ENVIRONMENT);
102  }
103  else if (this == TheKernel) /* the kernel directory */
104  {
105  return new_proxy(CHAR_KERNEL);
106  }
107  else if (this == TheSystem) /* the system directory */
108  {
109  return new_proxy(CHAR_SYSTEM);
110  }
111  else
112  {
113  Interpreter::logicError("Don't know how to generate a proxy object for an object");
114  }
115  return OREF_NULL;
116 }
117 
118 
120 /******************************************************************************/
121 /* Function: Copy a hash based collection object */
122 /******************************************************************************/
123 {
124  /* make a copy of ourself (this also */
125  /* copies the object variables */
127  /* We copy the Hash table as well */
128  OrefSet(newObj, newObj->contents, (RexxHashTable *)this->contents->copy());
129  return newObj; /* return the new object */
130 }
131 
133 /******************************************************************************/
134 /* Function: Return all of the collection indices in an array */
135 /******************************************************************************/
136 {
137  return this->contents->makeArray();
138 }
139 
141 /******************************************************************************/
142 /* Arguments: Value, index */
143 /* */
144 /* Returned: Nothing */
145 /******************************************************************************/
146 {
147  // put this in with duplicate protection
148  RexxHashTable *newHash = this->contents->putNodupe(_value, _index);
149  // the put can expand, so protect against that
150  if (newHash != OREF_NULL)
151  {
152  OrefSet(this, this->contents, newHash);
153  }
154  return OREF_NULL;
155 }
156 
157 
169 {
170  requiredArgument(_index, ARG_ONE); /* make sure we have an index */
171 
172  RexxObject *removedItem = this->remove(_index); /* remove the item */
173  if (removedItem == OREF_NULL) /* If nothing found, give back .nil */
174  {
175  /* (never return OREF_NULL to REXX) */
176  return TheNilObject;
177  }
178  return removedItem; /* return removed value */
179 }
180 
181 
191 {
192  return this->contents->remove(_index);
193 }
194 
195 
197  RexxObject *_index) /* target index */
198 /******************************************************************************/
199 /* Arguments: Index */
200 /* */
201 /* Returned: Array of all values with the same index */
202 /******************************************************************************/
203 {
204  requiredArgument(_index, ARG_ONE); /* make sure we have an index */
205  /* do the get */
206  return this->contents->getAll(_index);
207 }
208 
220 {
221  requiredArgument(_index, ARG_ONE); /* make sure we have an index */
222  RexxObject *object = this->get(_index); /* get the item */
223  if (object == OREF_NULL) /* If nothing found, give back .nil */
224  {
225  return TheNilObject; /* (never return OREF_NULL to REXX) */
226  }
227  return object; /* return the item */
228 }
229 
230 
243 {
244  return this->contents->get(key);
245 }
246 
247 
259 {
260  requiredArgument(_value, ARG_ONE); /* make sure we have an value */
261  requiredArgument(_index, ARG_TWO); /* make sure we have an index */
262  /* try to place in existing hashtab */
263  return this->put(_value, _index);
264 }
265 
266 
280 {
281  /* try to place in existing hashtab */
282  RexxHashTable *newHash = this->contents->put(_value, _index);
283  if (newHash != OREF_NULL) /* have a reallocation occur? */
284  {
285  /* hook on the new hash table */
286  OrefSet(this, this->contents, newHash);
287  }
288  return OREF_NULL; /* always return nothing */
289 }
290 
292  RexxObject *_value, /* object to add */
293  RexxObject *_index) /* added index */
294 /******************************************************************************/
295 /* Arguments: Value, index */
296 /* */
297 /* Returned: Nothing */
298 /******************************************************************************/
299 {
300  requiredArgument(_value, ARG_ONE); /* make sure we have an value */
301  requiredArgument(_index, ARG_TWO); /* make sure we have an index */
302  return add(_value, _index);
303 }
304 
306  RexxObject *_value, /* object to add */
307  RexxObject *_index) /* added index */
308 /******************************************************************************/
309 /* Arguments: Value, index */
310 /* */
311 /* Returned: Nothing */
312 /******************************************************************************/
313 {
314  /* try to place in existing hashtab */
315  RexxHashTable *newHash = this->contents->add(_value, _index);
316  if (newHash != OREF_NULL) /* have a reallocation occur? */
317  {
318  /* hook on the new hash table */
319  OrefSet(this, this->contents, newHash);
320  }
321  return OREF_NULL; /* always return nothing */
322 }
323 
325  RexxHashTableCollection * target) /* merge "partner" collection */
326 /******************************************************************************/
327 /* Function: Merge a hash table collection into another similar collection. */
328 /******************************************************************************/
329 {
330  return this->contents->merge(target);
331 }
332 
333 
335  int depth) /* depth to propagate the copy to */
336 /******************************************************************************/
337 /* Arguments: Recursion depth */
338 /* */
339 /* Returned: Nothing */
340 /******************************************************************************/
341 {
342  /* Get hash table. */
343  RexxHashTable *hashTable = this->contents;
344  /* For all indices. */
345  for (HashLink i = hashTable->first(); i < hashTable->totalSlotsSize(); i = hashTable->next(i))
346  {
347  RexxObject *_value = hashTable->value(i); /* Get this value */
348  RexxObject *valueCopy = _value->copy(); /* make a copy. */
349  hashTable->replace(valueCopy, i); /* Replace original w/ copy */
350  if (depth > 1) /* gone depth requested. */
351  /* nope, copy these values */
352  ((RexxHashTableCollection *)valueCopy)->copyValues(depth-1);
353  }
354 
355  return OREF_NULL;
356 }
357 
358 
370 {
371  requiredArgument(_index, ARG_ONE); /* make sure we have an index */
372  /* try to get the item */
373  RexxObject *_value = this->get(_index);
374  /* tell caller if we succeeded */
375  return(_value != OREF_NULL) ? (RexxObject *)TheTrueObject : (RexxObject *)TheFalseObject;
376 }
377 
378 
389 {
390  // required argument
391  requiredArgument(target, ARG_ONE);
392  // retrieve this from the hash table
393  RexxObject *result = this->getIndex(target);
394  // not found, return .nil
395  if (result == OREF_NULL)
396  {
397  return TheNilObject;
398  }
399  return result;
400 }
401 
402 
413 {
414  // retrieve this from the hash table
415  return contents->getIndex(target);
416 }
417 
418 
427 {
428  // required argument
429  requiredArgument(target, ARG_ONE);
430  // the actual target class may use different semantics for this.
431  return this->removeItem(target);
432 }
433 
434 
443 {
444  // the contents handle all of this.
445  return this->contents->removeItem(target);
446 }
447 
448 
457 {
458  requiredArgument(target, ARG_ONE);
459  return this->hasItem(target);
460 }
461 
462 
471 {
472  return this->contents->hasItem(target);
473 }
474 
475 
477 /******************************************************************************/
478 /* Function: create a table supplier */
479 /******************************************************************************/
480 {
481  /* get the hashtab supplier */
482  return this->contents->supplier();
483 }
484 
486 /******************************************************************************/
487 /* Function: retrieve all items of the collection. */
488 /******************************************************************************/
489 {
490  return this->contents->allItems();
491 }
492 
494 /******************************************************************************/
495 /* Function: retrieve all indexes of the collection. */
496 /******************************************************************************/
497 {
498  return this->contents->allIndexes();
499 }
500 
507 {
508  return this->contents->uniqueIndexes();
509 }
510 
511 
518 {
519  contents->empty();
520  return OREF_NULL;
521 }
522 
523 
530 {
532 }
TheKernel
#define TheKernel
Definition: RexxCore.h:177
RexxObject::copy
RexxObject * copy()
Definition: ObjectClass.cpp:518
RexxHashTable::value
RexxObject * value(HashLink position)
Definition: RexxHashTable.cpp:1447
RexxHashTableCollection::putRexx
RexxObject * putRexx(RexxObject *, RexxObject *)
Definition: RexxCollection.cpp:258
RexxArray
Definition: ArrayClass.hpp:100
RexxHashTableCollection::indexRexx
RexxObject * indexRexx(RexxObject *value)
Definition: RexxCollection.cpp:388
RexxHashTableCollection::put
virtual RexxObject * put(RexxObject *, RexxObject *)
Definition: RexxCollection.cpp:279
CHAR_ENVIRONMENT
char CHAR_ENVIRONMENT[]
TheFalseObject
#define TheFalseObject
Definition: RexxCore.h:184
RexxHashTableCollection::allAt
RexxObject * allAt(RexxObject *)
Definition: RexxCollection.cpp:196
RexxHashTableCollection::hasItemRexx
RexxObject * hasItemRexx(RexxObject *)
Definition: RexxCollection.cpp:456
new_proxy
RexxString * new_proxy(const char *name)
Definition: StringClass.hpp:567
RexxHashTable::supplier
RexxSupplier * supplier()
Definition: RexxHashTable.cpp:1774
memory_mark_general
#define memory_mark_general(oref)
Definition: RexxMemory.hpp:437
RexxHashTableCollection::empty
RexxObject * empty()
Definition: RexxCollection.cpp:517
OrefSet
#define OrefSet(o, r, v)
Definition: RexxCore.h:94
RexxHashTableCollection::removeItem
virtual RexxObject * removeItem(RexxObject *value)
Definition: RexxCollection.cpp:442
RexxHashTableCollection::liveGeneral
void liveGeneral(int reason)
Definition: RexxCollection.cpp:61
RexxHashTableCollection::copyValues
RexxObject * copyValues(int depth)
Definition: RexxCollection.cpp:334
RexxHashTable::getIndex
RexxObject * getIndex(RexxObject *value)
Definition: RexxHashTable.cpp:1062
RexxInternalObject::copy
virtual RexxObject * copy()
Definition: ObjectClass.cpp:478
RexxHashTableCollection::mergeItem
virtual RexxObject * mergeItem(RexxObject *, RexxObject *)
Definition: RexxCollection.cpp:140
RexxHashTable::get
RexxObject * get(RexxObject *key)
Definition: RexxHashTable.cpp:1118
RexxHashTableCollection::removeRexx
RexxObject * removeRexx(RexxObject *)
Definition: RexxCollection.cpp:168
requiredArgument
void requiredArgument(RexxObject *object, size_t position)
Definition: RexxCore.h:284
RexxHashTableCollection::makeArray
RexxArray * makeArray()
Definition: RexxCollection.cpp:132
CHAR_KERNEL
char CHAR_KERNEL[]
RexxHashTable::makeArray
RexxArray * makeArray()
Definition: RexxHashTable.cpp:1719
RexxHashTable::getAll
RexxArray * getAll(RexxObject *key)
Definition: RexxHashTable.cpp:865
ARG_TWO
const int ARG_TWO
Definition: RexxCore.h:81
RexxHashTableCollection::getIndex
virtual RexxObject * getIndex(RexxObject *value)
Definition: RexxCollection.cpp:412
RexxHashTable::uniqueIndexes
RexxArray * uniqueIndexes()
Definition: RexxHashTable.cpp:1755
RexxHashTableCollection::remove
virtual RexxObject * remove(RexxObject *key)
Definition: RexxCollection.cpp:190
RexxHashTableCollection
Definition: RexxCollection.hpp:49
RexxHashTableCollection::uniqueIndexes
RexxArray * uniqueIndexes()
Definition: RexxCollection.cpp:506
RexxHashTableCollection::isEmpty
RexxObject * isEmpty()
Definition: RexxCollection.cpp:529
TheTrueObject
#define TheTrueObject
Definition: RexxCore.h:185
RexxSupplier
Definition: SupplierClass.hpp:47
RexxHashTableCollection::add
virtual RexxObject * add(RexxObject *, RexxObject *)
Definition: RexxCollection.cpp:305
RexxEnvelope
Definition: RexxEnvelope.hpp:53
RexxHashTableCollection::getRexx
RexxObject * getRexx(RexxObject *)
Definition: RexxCollection.cpp:219
RexxHashTable::allIndexes
RexxArray * allIndexes()
Definition: RexxHashTable.cpp:1729
RexxHashTableCollection::addRexx
RexxObject * addRexx(RexxObject *, RexxObject *)
Definition: RexxCollection.cpp:291
RexxHashTable::removeItem
RexxObject * removeItem(RexxObject *value, RexxObject *key)
Definition: RexxHashTable.cpp:479
RexxHashTableCollection::copy
RexxObject * copy()
Definition: RexxCollection.cpp:119
Interpreter.hpp
RexxHashTableCollection::get
virtual RexxObject * get(RexxObject *key)
Definition: RexxCollection.cpp:242
RexxHashTable::replace
RexxObject * replace(RexxObject *value, HashLink position)
Definition: RexxHashTable.cpp:1597
RexxHashTableCollection::removeItemRexx
RexxObject * removeItemRexx(RexxObject *value)
Definition: RexxCollection.cpp:426
ARG_ONE
const int ARG_ONE
Definition: RexxCore.h:80
OREF_NULL
#define OREF_NULL
Definition: RexxCore.h:60
RexxHashTableCollection::unflatten
RexxObject * unflatten(RexxEnvelope *)
Definition: RexxCollection.cpp:83
ArrayClass.hpp
TheSystem
#define TheSystem
Definition: RexxCore.h:178
RexxCollection.hpp
RexxHashTable::totalSlotsSize
size_t totalSlotsSize()
Definition: RexxHashTable.hpp:134
flatten_reference
#define flatten_reference(oref, envel)
Definition: RexxMemory.hpp:440
cleanUpFlatten
#define cleanUpFlatten
Definition: RexxMemory.hpp:432
CHAR_SYSTEM
char CHAR_SYSTEM[]
StringClass.hpp
RexxHashTable::merge
RexxObject * merge(RexxHashTableCollection *target)
Definition: RexxHashTable.cpp:1484
RexxHashTable::isEmpty
bool isEmpty()
Definition: RexxHashTable.cpp:1711
RexxHashTableCollection::hasIndexRexx
RexxObject * hasIndexRexx(RexxObject *)
Definition: RexxCollection.cpp:369
TheEnvironment
#define TheEnvironment
Definition: RexxCore.h:173
RexxHashTable
Definition: RexxHashTable.hpp:60
RexxHashTableCollection::hasItem
virtual RexxObject * hasItem(RexxObject *)
Definition: RexxCollection.cpp:470
memory_mark
#define memory_mark(oref)
Definition: RexxMemory.hpp:436
RexxHashTable::hasItem
RexxObject * hasItem(RexxObject *value, RexxObject *key)
Definition: RexxHashTable.cpp:649
RexxHashTableCollection::allItems
RexxArray * allItems()
Definition: RexxCollection.cpp:485
HashLink
size_t HashLink
Definition: RexxHashTable.hpp:49
RexxHashTable::empty
void empty()
Definition: RexxHashTable.cpp:1696
RexxEnvelope::addTable
void addTable(RexxObject *obj)
Definition: RexxEnvelope.cpp:433
RexxHashTableCollection::contents
RexxHashTable * contents
Definition: RexxCollection.hpp:91
RexxHashTableCollection::live
void live(size_t)
Definition: RexxCollection.cpp:52
RexxHashTable::remove
RexxObject * remove(RexxObject *key)
Definition: RexxHashTable.cpp:257
RexxHashTable::next
HashLink next(HashLink position)
Definition: RexxHashTable.cpp:1573
RexxHashTableCollection::merge
RexxObject * merge(RexxHashTableCollection *)
Definition: RexxCollection.cpp:324
RexxHashTableCollection::flatten
void flatten(RexxEnvelope *)
Definition: RexxCollection.cpp:70
setUpFlatten
#define setUpFlatten(type)
Definition: RexxMemory.hpp:427
RexxHashTable::add
RexxHashTable * add(RexxObject *value, RexxObject *key)
Definition: RexxHashTable.cpp:214
TheNilObject
#define TheNilObject
Definition: RexxCore.h:180
RexxHashTable::put
RexxHashTable * put(RexxObject *value, RexxObject *key)
Definition: RexxHashTable.cpp:1341
RexxCore.h
DirectoryClass.hpp
RexxHashTableCollection::makeProxy
RexxObject * makeProxy(RexxEnvelope *)
Definition: RexxCollection.cpp:92
Interpreter::logicError
static void logicError(const char *desc)
Definition: Interpreter.cpp:553
RexxHashTable::allItems
RexxArray * allItems()
Definition: RexxHashTable.cpp:1609
RexxHashTableCollection::allIndexes
RexxArray * allIndexes()
Definition: RexxCollection.cpp:493
RexxHashTable::putNodupe
RexxHashTable * putNodupe(RexxObject *value, RexxObject *key)
Definition: RexxHashTable.cpp:1413
RexxObject
Definition: ObjectClass.hpp:311
RexxHashTableCollection::supplier
RexxSupplier * supplier()
Definition: RexxCollection.cpp:476
RexxHashTable::first
HashLink first()
Definition: RexxHashTable.cpp:1561