hotspot/src/share/vm/utilities/array.hpp
author apetrusenko
Tue, 10 Feb 2009 18:39:09 +0300
changeset 2013 49e915da0905
parent 1623 a0dd9009e992
child 5547 f4b087cbb361
permissions -rw-r--r--
6700941: G1: allocation spec missing for some G1 classes Reviewed-by: tonyp
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
1623
a0dd9009e992 6785258: Update copyright year
xdono
parents: 1551
diff changeset
     2
 * Copyright 2000-2008 Sun Microsystems, Inc.  All Rights Reserved.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
489c9b5090e2 Initial load
duke
parents:
diff changeset
    25
// correct linkage required to compile w/o warnings
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
// (must be on file level - cannot be local)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
extern "C" { typedef int (*ftype)(const void*, const void*); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
class ResourceArray: public ResourceObj {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
 protected:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
  int   _length;                                 // the number of array elements
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
  void* _data;                                   // the array memory
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
  int   _nesting;                                // the resource area nesting level
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
  // creation
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
  ResourceArray() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
    _length  = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
    _data    = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
    DEBUG_ONLY(init_nesting();)
1551
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
    43
    // client may call initialize, at most once
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
  ResourceArray(size_t esize, int length) {
1551
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
    48
    DEBUG_ONLY(_data = NULL);
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
    49
    initialize(esize, length);
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
    50
  }
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
    51
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
    52
  void initialize(size_t esize, int length) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
    assert(length >= 0, "illegal length");
1551
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
    54
    assert(_data == NULL, "must be new object");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
    _length  = length;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
    _data    = resource_allocate_bytes(esize * length);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
    DEBUG_ONLY(init_nesting();)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
  void init_nesting();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
  // helper functions
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
  void sort     (size_t esize, ftype f);         // sort the array
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
  void expand   (size_t esize, int i, int& size);// expand the array to include slot i
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
  void remove_at(size_t esize, int i);           // remove the element in slot i
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
  // standard operations
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
  int  length() const                            { return _length; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
  bool is_empty() const                          { return length() == 0; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
class CHeapArray: public CHeapObj {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
 protected:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
  int   _length;                                 // the number of array elements
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
  void* _data;                                   // the array memory
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
  // creation
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
  CHeapArray() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
    _length  = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
    _data    = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
  CHeapArray(size_t esize, int length) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
    assert(length >= 0, "illegal length");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
    _length  = length;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
    _data    = (void*) NEW_C_HEAP_ARRAY(char *, esize * length);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
  void init_nesting();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
  // helper functions
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
  void sort     (size_t esize, ftype f);         // sort the array
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
  void expand   (size_t esize, int i, int& size);// expand the array to include slot i
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
  void remove_at(size_t esize, int i);           // remove the element in slot i
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
  // standard operations
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
  int  length() const                            { return _length; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
  bool is_empty() const                          { return length() == 0; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
#define define_generic_array(array_name,element_type, base_class)                        \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
  class array_name: public base_class {                                                  \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
   protected:                                                                            \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
    typedef element_type etype;                                                          \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
    enum { esize = sizeof(etype) };                                                      \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
                                                                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
    void base_remove_at(size_t size, int i) { base_class::remove_at(size, i); }          \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
                                                                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
   public:                                                                               \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
    /* creation */                                                                       \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
    array_name() : base_class()                       {}                                 \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
    array_name(const int length) : base_class(esize, length) {}                          \
1551
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   121
    array_name(const int length, const etype fx)      { initialize(length, fx); }        \
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   122
    void initialize(const int length)     { base_class::initialize(esize, length); }     \
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   123
    void initialize(const int length, const etype fx) {                                  \
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   124
      initialize(length);                                                                \
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
      for (int i = 0; i < length; i++) ((etype*)_data)[i] = fx;                          \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
    }                                                                                    \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
                                                                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
    /* standard operations */                                                            \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
    etype& operator [] (const int i) const {                                             \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
      assert(0 <= i && i < length(), "index out of bounds");                             \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
      return ((etype*)_data)[i];                                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
    }                                                                                    \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
                                                                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
    int index_of(const etype x) const {                                                  \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
      int i = length();                                                                  \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
      while (i-- > 0 && ((etype*)_data)[i] != x) ;                                       \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
      /* i < 0 || ((etype*)_data)_data[i] == x */                                        \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
      return i;                                                                          \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
    }                                                                                    \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
                                                                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
    void sort(int f(etype*, etype*))             { base_class::sort(esize, (ftype)f); }  \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
    bool contains(const etype x) const           { return index_of(x) >= 0; }            \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
                                                                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
    /* deprecated operations - for compatibility with GrowableArray only */              \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
    etype  at(const int i) const                 { return (*this)[i]; }                  \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
    void   at_put(const int i, const etype x)    { (*this)[i] = x; }                     \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
    etype* adr_at(const int i)                   { return &(*this)[i]; }                 \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
    int    find(const etype x)                   { return index_of(x); }                 \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
  };                                                                                     \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
#define define_array(array_name,element_type)                                            \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
  define_generic_array(array_name, element_type, ResourceArray)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
#define define_stack(stack_name,array_name)                                              \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
  class stack_name: public array_name {                                                  \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
   protected:                                                                            \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
    int _size;                                                                           \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
                                                                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
    void grow(const int i, const etype fx) {                                             \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
      assert(i >= length(), "index too small");                                          \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
      if (i >= size()) expand(esize, i, _size);                                          \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
      for (int j = length(); j <= i; j++) ((etype*)_data)[j] = fx;                       \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
      _length = i+1;                                                                     \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
    }                                                                                    \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
                                                                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
   public:                                                                               \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
    /* creation */                                                                       \
1551
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   170
    stack_name() : array_name()                     { _size = 0; }                       \
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   171
    stack_name(const int size)                      { initialize(size); }                \
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   172
    stack_name(const int size, const etype fx)      { initialize(size, fx); }            \
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   173
    void initialize(const int size, const etype fx) {                                    \
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   174
      _size = size;                                                                      \
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   175
      array_name::initialize(size, fx);                                                  \
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   176
      /* _length == size, allocation and size are the same */                            \
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   177
    }                                                                                    \
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   178
    void initialize(const int size) {                                                    \
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   179
      _size = size;                                                                      \
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   180
      array_name::initialize(size);                                                      \
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   181
      _length = 0;          /* reset length to zero; _size records the allocation */     \
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   182
    }                                                                                    \
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
                                                                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
    /* standard operations */                                                            \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
    int size() const                             { return _size; }                       \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
                                                                                         \
1551
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   187
    int push(const etype x) {                                                            \
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   188
      int len = length();                                                                \
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   189
      if (len >= size()) expand(esize, len, _size);                                      \
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   190
      ((etype*)_data)[len] = x;                                                          \
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   191
      _length = len+1;                                                                   \
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   192
      return len;                                                                        \
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
    }                                                                                    \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
                                                                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
    etype pop() {                                                                        \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
      assert(!is_empty(), "stack is empty");                                             \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
      return ((etype*)_data)[--_length];                                                 \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
    }                                                                                    \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
                                                                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
    etype top() const {                                                                  \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
      assert(!is_empty(), "stack is empty");                                             \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
      return ((etype*)_data)[length() - 1];                                              \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
    }                                                                                    \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
                                                                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
    void push_all(const stack_name* stack) {                                             \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
      const int l = stack->length();                                                     \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
      for (int i = 0; i < l; i++) push(((etype*)(stack->_data))[i]);                     \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
    }                                                                                    \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
                                                                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
    etype at_grow(const int i, const etype fx) {                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
      if (i >= length()) grow(i, fx);                                                    \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
      return ((etype*)_data)[i];                                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
    }                                                                                    \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
                                                                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
    void at_put_grow(const int i, const etype x, const etype fx) {                       \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
      if (i >= length()) grow(i, fx);                                                    \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
      ((etype*)_data)[i] = x;                                                            \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
    }                                                                                    \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
                                                                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
    void truncate(const int length) {                                                    \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
      assert(0 <= length && length <= this->length(), "illegal length");                 \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
      _length = length;                                                                  \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
    }                                                                                    \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
                                                                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
    void remove_at(int i)                        { base_remove_at(esize, i); }           \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
    void remove(etype x)                         { remove_at(index_of(x)); }             \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
                                                                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
    /* inserts the given element before the element at index i */                        \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
    void insert_before(const int i, const etype el)  {                                   \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
      int len = length();                                                                \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
      int new_length = len + 1;                                                          \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
      if (new_length >= size()) expand(esize, new_length, _size);                        \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
      for (int j = len - 1; j >= i; j--) {                                               \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
        ((etype*)_data)[j + 1] = ((etype*)_data)[j];                                     \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
      }                                                                                  \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
      _length = new_length;                                                              \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
      at_put(i, el);                                                                     \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
    }                                                                                    \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
                                                                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
    /* inserts contents of the given stack before the element at index i */              \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
    void insert_before(const int i, const stack_name *st) {                              \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
      if (st->length() == 0) return;                                                     \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
      int len = length();                                                                \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
      int st_len = st->length();                                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
      int new_length = len + st_len;                                                     \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
      if (new_length >= size()) expand(esize, new_length, _size);                        \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
      int j;                                                                             \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
      for (j = len - 1; j >= i; j--) {                                                   \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   249
        ((etype*)_data)[j + st_len] = ((etype*)_data)[j];                                \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   250
      }                                                                                  \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
      for (j = 0; j < st_len; j++) {                                                     \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
        ((etype*)_data)[i + j] = ((etype*)st->_data)[j];                                 \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
      }                                                                                  \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
      _length = new_length;                                                              \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
    }                                                                                    \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
                                                                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
    /* deprecated operations - for compatibility with GrowableArray only */              \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
    int  capacity() const                        { return size(); }                      \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
    void clear()                                 { truncate(0); }                        \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   260
    void trunc_to(const int length)              { truncate(length); }                   \
1551
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   261
    int  append(const etype x)                   { return push(x); }                     \
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
    void appendAll(const stack_name* stack)      { push_all(stack); }                    \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   263
    etype last() const                           { return top(); }                       \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
  };                                                                                     \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
489c9b5090e2 Initial load
duke
parents:
diff changeset
   266
489c9b5090e2 Initial load
duke
parents:
diff changeset
   267
#define define_resource_list(element_type)                                               \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   268
  define_generic_array(element_type##Array, element_type, ResourceArray)                 \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
  define_stack(element_type##List, element_type##Array)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
#define define_resource_pointer_list(element_type)                                       \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
  define_generic_array(element_type##Array, element_type *, ResourceArray)               \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
  define_stack(element_type##List, element_type##Array)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
#define define_c_heap_list(element_type)                                                 \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
  define_generic_array(element_type##Array, element_type, CHeapArray)                    \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   277
  define_stack(element_type##List, element_type##Array)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
489c9b5090e2 Initial load
duke
parents:
diff changeset
   279
#define define_c_heap_pointer_list(element_type)                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   280
  define_generic_array(element_type##Array, element_type *, CHeapArray)                  \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
  define_stack(element_type##List, element_type##Array)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   282
489c9b5090e2 Initial load
duke
parents:
diff changeset
   283
489c9b5090e2 Initial load
duke
parents:
diff changeset
   284
// Arrays for basic types
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
define_array(boolArray, bool)          define_stack(boolStack, boolArray)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   287
define_array(intArray , int )          define_stack(intStack , intArray )