< prev index next >

src/hotspot/share/utilities/growableArray.hpp

Print this page
*** 24,10 ***
--- 24,12 ---
  
  #ifndef SHARE_UTILITIES_GROWABLEARRAY_HPP
  #define SHARE_UTILITIES_GROWABLEARRAY_HPP
  
  #include "memory/allocation.hpp"
+ #include "oops/array.hpp"
+ #include "oops/oop.hpp"
  #include "memory/iterator.hpp"
  #include "utilities/debug.hpp"
  #include "utilities/globalDefinitions.hpp"
  #include "utilities/ostream.hpp"
  #include "utilities/powerOfTwo.hpp"

*** 100,10 ***
--- 102,11 ---
      _len = length;
    }
  };
  
  template <typename E> class GrowableArrayIterator;
+ template <typename E, typename UnaryPredicate> class GrowableArrayFilterIterator;
  
  // Extends GrowableArrayBase with a typed data array.
  //
  // E: Element type
  //

*** 478,10 ***
--- 481,16 ---
      for (int i = 0; i < l->length(); i++) {
        this->at_put_grow(this->_len, l->at(i), E());
      }
    }
  
+   void appendAll(const Array<E>* l) {
+     for (int i = 0; i < l->length(); i++) {
+       this->at_put_grow(this->_len, l->at(i), E());
+     }
+   }
+ 
    // Binary search and insertion utility.  Search array for element
    // matching key according to the static compare function.  Insert
    // that element if not already in the list.  Assumes the list is
    // already sorted according to compare function.
    template <int compare(const E&, const E&)> E insert_sorted(const E& key) {

*** 854,10 ***
--- 863,11 ---
  // Custom STL-style iterator to iterate over GrowableArrays
  // It is constructed by invoking GrowableArray::begin() and GrowableArray::end()
  template <typename E>
  class GrowableArrayIterator : public StackObj {
    friend class GrowableArrayView<E>;
+   template <typename F, typename UnaryPredicate> friend class GrowableArrayFilterIterator;
  
   private:
    const GrowableArrayView<E>* _array; // GrowableArray we iterate over
    int _position;                      // The current position in the GrowableArray
  

*** 880,10 ***
--- 890,64 ---
      assert(_array == rhs._array, "iterator belongs to different array");
      return _position != rhs._position;
    }
  };
  
+ // Custom STL-style iterator to iterate over elements of a GrowableArray that satisfy a given predicate
+ template <typename E, class UnaryPredicate>
+ class GrowableArrayFilterIterator : public StackObj {
+   friend class GrowableArrayView<E>;
+ 
+  private:
+   const GrowableArrayView<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 GrowableArray<E>* array, UnaryPredicate filter_predicate) :
+       _array(array), _position(0), _predicate(filter_predicate) {
+     // Advance to first element satisfying the predicate
+     while(!at_end() && !_predicate(_array->at(_position))) {
+       ++_position;
+     }
+   }
+ 
+   GrowableArrayFilterIterator<E, UnaryPredicate>& operator++() {
+     do {
+       // Advance to next element satisfying the predicate
+       ++_position;
+     } while(!at_end() && !_predicate(_array->at(_position)));
+     return *this;
+   }
+ 
+   E operator*() { return _array->at(_position); }
+ 
+   bool operator==(const GrowableArrayIterator<E>& rhs)  {
+     assert(_array == rhs._array, "iterator belongs to different array");
+     return _position == rhs._position;
+   }
+ 
+   bool operator!=(const GrowableArrayIterator<E>& rhs)  {
+     assert(_array == rhs._array, "iterator belongs to different array");
+     return _position != rhs._position;
+   }
+ 
+   bool operator==(const GrowableArrayFilterIterator<E, UnaryPredicate>& rhs)  {
+     assert(_array == rhs._array, "iterator belongs to different array");
+     return _position == rhs._position;
+   }
+ 
+   bool operator!=(const GrowableArrayFilterIterator<E, UnaryPredicate>& rhs)  {
+     assert(_array == rhs._array, "iterator belongs to different array");
+     return _position != rhs._position;
+   }
+ 
+   bool at_end() const {
+     return _array == nullptr || _position == _array->end()._position;
+   }
+ };
+ 
  // Arrays for basic types
  typedef GrowableArray<int> intArray;
  typedef GrowableArray<int> intStack;
  typedef GrowableArray<bool> boolArray;
  
< prev index next >