src/share/vm/utilities/growableArray.hpp

changeset 7028
46bbe04d1cad
parent 6198
55fb97c4c58d
child 7041
411e30e5fbb8
     1.1 --- a/src/share/vm/utilities/growableArray.hpp	Tue Aug 12 15:17:46 2014 +0000
     1.2 +++ b/src/share/vm/utilities/growableArray.hpp	Fri Apr 11 13:52:51 2014 +0200
     1.3 @@ -147,6 +147,9 @@
     1.4    }
     1.5  };
     1.6  
     1.7 +template<class E> class GrowableArrayIterator;
     1.8 +template<class E, class UnaryPredicate> class GrowableArrayFilterIterator;
     1.9 +
    1.10  template<class E> class GrowableArray : public GenericGrowableArray {
    1.11    friend class VMStructs;
    1.12  
    1.13 @@ -243,6 +246,14 @@
    1.14      return _data[_len-1];
    1.15    }
    1.16  
    1.17 +  GrowableArrayIterator<E> begin() const {
    1.18 +    return GrowableArrayIterator<E>(this, 0);
    1.19 +  }
    1.20 +
    1.21 +  GrowableArrayIterator<E> end() const {
    1.22 +    return GrowableArrayIterator<E>(this, length());
    1.23 +  }
    1.24 +
    1.25    void push(const E& elem) { append(elem); }
    1.26  
    1.27    E pop() {
    1.28 @@ -412,4 +423,83 @@
    1.29      tty->print("}\n");
    1.30  }
    1.31  
    1.32 +// Custom STL-style iterator to iterate over GrowableArrays
    1.33 +// It is constructed by invoking GrowableArray::begin() and GrowableArray::end()
    1.34 +template<class E> class GrowableArrayIterator : public StackObj {
    1.35 +  friend class GrowableArray<E>;
    1.36 +  template<class F, class UnaryPredicate> friend class GrowableArrayFilterIterator;
    1.37 +
    1.38 + private:
    1.39 +  const GrowableArray<E>* _array; // GrowableArray we iterate over
    1.40 +  int _position;                  // The current position in the GrowableArray
    1.41 +
    1.42 +  // Private constructor used in GrowableArray::begin() and GrowableArray::end()
    1.43 +  GrowableArrayIterator(const GrowableArray<E>* array, int position) : _array(array), _position(position) {
    1.44 +    assert(0 <= position && position <= _array->length(), "illegal position");
    1.45 +  }
    1.46 +
    1.47 + public:
    1.48 +  GrowableArrayIterator<E>& operator++()  { ++_position; return *this; }
    1.49 +  E operator*()                           { return _array->at(_position); }
    1.50 +
    1.51 +  bool operator==(const GrowableArrayIterator<E>& rhs)  {
    1.52 +    assert(_array == rhs._array, "iterator belongs to different array");
    1.53 +    return _position == rhs._position;
    1.54 +  }
    1.55 +
    1.56 +  bool operator!=(const GrowableArrayIterator<E>& rhs)  {
    1.57 +    assert(_array == rhs._array, "iterator belongs to different array");
    1.58 +    return _position != rhs._position;
    1.59 +  }
    1.60 +};
    1.61 +
    1.62 +// Custom STL-style iterator to iterate over elements of a GrowableArray that satisfy a given predicate
    1.63 +template<class E, class UnaryPredicate> class GrowableArrayFilterIterator : public StackObj {
    1.64 +  friend class GrowableArray<E>;
    1.65 +
    1.66 + private:
    1.67 +  const GrowableArray<E>* _array;   // GrowableArray we iterate over
    1.68 +  int _position;                    // Current position in the GrowableArray
    1.69 +  UnaryPredicate _predicate;        // Unary predicate the elements of the GrowableArray should satisfy
    1.70 +
    1.71 + public:
    1.72 +  GrowableArrayFilterIterator(const GrowableArrayIterator<E>& begin, UnaryPredicate filter_predicate)
    1.73 +   : _array(begin._array), _position(begin._position), _predicate(filter_predicate) {
    1.74 +    // Advance to first element satisfying the predicate
    1.75 +    while(_position != _array->length() && !_predicate(_array->at(_position))) {
    1.76 +      ++_position;
    1.77 +    }
    1.78 +  }
    1.79 +
    1.80 +  GrowableArrayFilterIterator<E, UnaryPredicate>& operator++() {
    1.81 +    do {
    1.82 +      // Advance to next element satisfying the predicate
    1.83 +      ++_position;
    1.84 +    } while(_position != _array->length() && !_predicate(_array->at(_position)));
    1.85 +    return *this;
    1.86 +  }
    1.87 +
    1.88 +  E operator*()   { return _array->at(_position); }
    1.89 +
    1.90 +  bool operator==(const GrowableArrayIterator<E>& rhs)  {
    1.91 +    assert(_array == rhs._array, "iterator belongs to different array");
    1.92 +    return _position == rhs._position;
    1.93 +  }
    1.94 +
    1.95 +  bool operator!=(const GrowableArrayIterator<E>& rhs)  {
    1.96 +    assert(_array == rhs._array, "iterator belongs to different array");
    1.97 +    return _position != rhs._position;
    1.98 +  }
    1.99 +
   1.100 +  bool operator==(const GrowableArrayFilterIterator<E, UnaryPredicate>& rhs)  {
   1.101 +    assert(_array == rhs._array, "iterator belongs to different array");
   1.102 +    return _position == rhs._position;
   1.103 +  }
   1.104 +
   1.105 +  bool operator!=(const GrowableArrayFilterIterator<E, UnaryPredicate>& rhs)  {
   1.106 +    assert(_array == rhs._array, "iterator belongs to different array");
   1.107 +    return _position != rhs._position;
   1.108 +  }
   1.109 +};
   1.110 +
   1.111  #endif // SHARE_VM_UTILITIES_GROWABLEARRAY_HPP

mercurial