src/share/vm/utilities/growableArray.hpp
Index
Unified diffs
Context diffs
Sdiffs
Patch
New
Old
Previous File
Next File
JDK-8015774 Cdiff src/share/vm/utilities/growableArray.hpp
src/share/vm/utilities/growableArray.hpp
Print this page
*** 74,83 ****
--- 74,86 ----
// Need the correct linkage to call qsort without warnings
extern "C" {
typedef int (*_sort_Fn)(const void *, const void *);
}
+ template<class E> class GrowableArrayIterator;
+ template<class E, class UnaryPredicate> class GrowableArrayFilterIterator;
+
class GenericGrowableArray : public ResourceObj {
friend class VMStructs;
protected:
int _len; // current length
*** 241,250 ****
--- 244,261 ----
E top() const {
assert(_len > 0, "empty list");
return _data[_len-1];
}
+ GrowableArrayIterator<E> begin() const {
+ return GrowableArrayIterator<E>(this, 0);
+ }
+
+ GrowableArrayIterator<E> end() const {
+ return GrowableArrayIterator<E>(this, length());
+ }
+
void push(const E& elem) { append(elem); }
E pop() {
assert(_len > 0, "empty list");
return _data[--_len];
*** 410,415 ****
--- 421,479 ----
tty->print(": length %ld (_max %ld) { ", _len, _max);
for (int i = 0; i < _len; i++) tty->print(INTPTR_FORMAT " ", *(intptr_t*)&(_data[i]));
tty->print("}\n");
}
+ // Custom STL iterator to iterate over GrowableArrays
+ // It is constructed by invoking GrowableArray::begin() and GrowableArray::end()
+ template<class E> class GrowableArrayIterator {
+ friend class GrowableArray<E>;
+ template<class A, class B> friend class GrowableArrayFilterIterator;
+
+ private:
+ const GrowableArray<E>* _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<E>* array, int position) : _array(array), _position(position) { }
+
+ public:
+ GrowableArrayIterator<E>& operator++() { ++_position; return *this; }
+ bool operator==(const GrowableArrayIterator<E>& rhs) { return _position == rhs._position; }
+ bool operator!=(const GrowableArrayIterator<E>& 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 E, class UnaryPredicate> class GrowableArrayFilterIterator {
+ friend class GrowableArray<E>;
+
+ private:
+ const GrowableArray<E>* _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<E>& 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<E, UnaryPredicate>& operator++() {
+ do {
+ // Advance to next element satisfying the predicate
+ ++_position;
+ } while(_position != _array->length() && !_predicate(_array->at(_position)));
+ return *this;
+ }
+
+ bool operator==(const GrowableArrayIterator<E>& rhs) { return _position == rhs._position; }
+ bool operator!=(const GrowableArrayIterator<E>& rhs) { return _position != rhs._position; }
+ bool operator==(const GrowableArrayFilterIterator<E, UnaryPredicate>& rhs) { return _position == rhs._position; }
+ bool operator!=(const GrowableArrayFilterIterator<E, UnaryPredicate>& rhs) { return _position != rhs._position; }
+ E operator*() { return _array->at(_position); }
+ };
+
#endif // SHARE_VM_UTILITIES_GROWABLEARRAY_HPP
src/share/vm/utilities/growableArray.hpp
Index
Unified diffs
Context diffs
Sdiffs
Patch
New
Old
Previous File
Next File