--- old/src/share/vm/utilities/growableArray.hpp 2013-10-11 15:44:39.085819535 +0200 +++ new/src/share/vm/utilities/growableArray.hpp 2013-10-11 15:44:39.025819537 +0200 @@ -76,6 +76,9 @@ typedef int (*_sort_Fn)(const void *, const void *); } +template class GrowableArrayIterator; +template class GrowableArrayFilterIterator; + class GenericGrowableArray : public ResourceObj { friend class VMStructs; @@ -243,6 +246,14 @@ return _data[_len-1]; } + GrowableArrayIterator begin() const { + return GrowableArrayIterator(this, 0); + } + + GrowableArrayIterator end() const { + return GrowableArrayIterator(this, length()); + } + void push(const E& elem) { append(elem); } E pop() { @@ -412,4 +423,57 @@ tty->print("}\n"); } +// Custom STL iterator to iterate over GrowableArrays +// It is constructed by invoking GrowableArray::begin() and GrowableArray::end() +template class GrowableArrayIterator { + friend class GrowableArray; + template friend class GrowableArrayFilterIterator; + + private: + const GrowableArray* _array; // GrowableArray we iterate over + int _position; // The current position in the GrowableArray + + // Private constructor used in GrowableArray::begin() and GrowableArray::end() + GrowableArrayIterator(const GrowableArray* array, int position) : _array(array), _position(position) { } + + public: + GrowableArrayIterator& operator++() { ++_position; return *this; } + bool operator==(const GrowableArrayIterator& rhs) { return _position == rhs._position; } + bool operator!=(const GrowableArrayIterator& rhs) { return _position != rhs._position; } + E operator*() { return _array->at(_position); } +}; + +// Custom STL iterator to iterate over elements of a GrowableArray that satisfy a given predicate +template class GrowableArrayFilterIterator { + friend class GrowableArray; + + private: + const GrowableArray* _array; // GrowableArray we iterate over + int _position; // Current position in the GrowableArray + UnaryPredicate _predicate; // Unary predicate the elements of the GrowableArray should satisfy + + public: + GrowableArrayFilterIterator(const GrowableArrayIterator& begin, UnaryPredicate filter_predicate) + : _array(begin._array), _position(begin._position), _predicate(filter_predicate) { + // Advance to first element satisfying the predicate + while(_position != _array->length() && !_predicate(_array->at(_position))) { + ++_position; + } + } + + GrowableArrayFilterIterator& operator++() { + do { + // Advance to next element satisfying the predicate + ++_position; + } while(_position != _array->length() && !_predicate(_array->at(_position))); + return *this; + } + + bool operator==(const GrowableArrayIterator& rhs) { return _position == rhs._position; } + bool operator!=(const GrowableArrayIterator& rhs) { return _position != rhs._position; } + bool operator==(const GrowableArrayFilterIterator& rhs) { return _position == rhs._position; } + bool operator!=(const GrowableArrayFilterIterator& rhs) { return _position != rhs._position; } + E operator*() { return _array->at(_position); } +}; + #endif // SHARE_VM_UTILITIES_GROWABLEARRAY_HPP