< prev index next >

src/hotspot/share/utilities/growableArray.hpp

Print this page

 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 25 #ifndef SHARE_UTILITIES_GROWABLEARRAY_HPP
 26 #define SHARE_UTILITIES_GROWABLEARRAY_HPP
 27 
 28 #include "memory/allocation.hpp"
 29 #include "memory/iterator.hpp"


 30 #include "utilities/debug.hpp"
 31 #include "utilities/globalDefinitions.hpp"
 32 #include "utilities/ostream.hpp"
 33 #include "utilities/powerOfTwo.hpp"
 34 
 35 // A growable array.
 36 
 37 /*************************************************************************/
 38 /*                                                                       */
 39 /*     WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING   */
 40 /*                                                                       */
 41 /* Should you use GrowableArrays to contain handles you must be certain  */
 42 /* that the GrowableArray does not outlive the HandleMark that contains  */
 43 /* the handles. Since GrowableArrays are typically resource allocated    */
 44 /* the following is an example of INCORRECT CODE,                        */
 45 /*                                                                       */
 46 /* ResourceMark rm;                                                      */
 47 /* GrowableArray<Handle>* arr = new GrowableArray<Handle>(size);         */
 48 /* if (blah) {                                                           */
 49 /*    while (...) {                                                      */

 79   int _capacity;
 80 
 81   GrowableArrayBase(int capacity, int initial_len) :
 82       _len(initial_len),
 83       _capacity(capacity) {
 84     assert(_len >= 0 && _len <= _capacity, "initial_len too big");
 85   }
 86 
 87   ~GrowableArrayBase() {}
 88 
 89 public:
 90   int   length() const          { return _len; }
 91   int   capacity() const        { return _capacity; }
 92 
 93   bool  is_empty() const        { return _len == 0; }
 94   bool  is_nonempty() const     { return _len != 0; }
 95   bool  is_full() const         { return _len == _capacity; }
 96 };
 97 
 98 template <typename E> class GrowableArrayIterator;

 99 
100 // Extends GrowableArrayBase with a typed data array.
101 //
102 // E: Element type
103 //
104 // The "view" adds function that don't grow or deallocate
105 // the _data array, so there's no need for an allocator.
106 //
107 // The "view" can be used to type erase the allocator classes
108 // of GrowableArrayWithAllocator.
109 template <typename E>
110 class GrowableArrayView : public GrowableArrayBase {
111 protected:
112   E* _data; // data array
113 
114   GrowableArrayView(E* data, int capacity, int initial_len) :
115       GrowableArrayBase(capacity, initial_len), _data(data) {}
116 
117   ~GrowableArrayView() {}
118 

411     int new_len = this->_len + array_len;
412     if (new_len >= this->_capacity) grow(new_len);
413 
414     for (int j = this->_len - 1; j >= idx; j--) {
415       this->_data[j + array_len] = this->_data[j];
416     }
417 
418     for (int j = 0; j < array_len; j++) {
419       this->_data[idx + j] = array->at(j);
420     }
421 
422     this->_len += array_len;
423   }
424 
425   void appendAll(const GrowableArrayView<E>* l) {
426     for (int i = 0; i < l->length(); i++) {
427       this->at_put_grow(this->_len, l->at(i), E());
428     }
429   }
430 






431   // Binary search and insertion utility.  Search array for element
432   // matching key according to the static compare function.  Insert
433   // that element if not already in the list.  Assumes the list is
434   // already sorted according to compare function.
435   template <int compare(const E&, const E&)> E insert_sorted(const E& key) {
436     bool found;
437     int location = GrowableArrayView<E>::template find_sorted<E, compare>(key, found);
438     if (!found) {
439       insert_before(location, key);
440     }
441     return this->at(location);
442   }
443 
444   E insert_sorted(CompareClosure<E>* cc, const E& key) {
445     bool found;
446     int location = find_sorted(cc, key, found);
447     if (!found) {
448       insert_before(location, key);
449     }
450     return this->at(location);

859     this->clear_and_deallocate();
860   }
861 
862   void* operator new(size_t size) {
863     return AnyObj::operator new(size, MT);
864   }
865 
866   void* operator new(size_t size, const std::nothrow_t&  nothrow_constant) throw() {
867     return AnyObj::operator new(size, nothrow_constant, MT);
868   }
869   void operator delete(void *p) {
870     AnyObj::operator delete(p);
871   }
872 };
873 
874 // Custom STL-style iterator to iterate over GrowableArrays
875 // It is constructed by invoking GrowableArray::begin() and GrowableArray::end()
876 template <typename E>
877 class GrowableArrayIterator : public StackObj {
878   friend class GrowableArrayView<E>;

879 
880  private:
881   const GrowableArrayView<E>* _array; // GrowableArray we iterate over
882   int _position;                      // The current position in the GrowableArray
883 
884   // Private constructor used in GrowableArray::begin() and GrowableArray::end()
885   GrowableArrayIterator(const GrowableArrayView<E>* array, int position) : _array(array), _position(position) {
886     assert(0 <= position && position <= _array->length(), "illegal position");
887   }
888 
889  public:
890   GrowableArrayIterator() : _array(nullptr), _position(0) { }
891   GrowableArrayIterator& operator++() { ++_position; return *this; }
892   E operator*()                       { return _array->at(_position); }
893 
894   bool operator==(const GrowableArrayIterator& rhs)  {
895     assert(_array == rhs._array, "iterator belongs to different array");
896     return _position == rhs._position;
897   }
898 
899   bool operator!=(const GrowableArrayIterator& rhs)  {
900     assert(_array == rhs._array, "iterator belongs to different array");
901     return _position != rhs._position;
902   }
903 };
904 






















































905 // Arrays for basic types
906 typedef GrowableArray<int> intArray;
907 typedef GrowableArray<int> intStack;
908 typedef GrowableArray<bool> boolArray;
909 
910 #endif // SHARE_UTILITIES_GROWABLEARRAY_HPP

 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 25 #ifndef SHARE_UTILITIES_GROWABLEARRAY_HPP
 26 #define SHARE_UTILITIES_GROWABLEARRAY_HPP
 27 
 28 #include "memory/allocation.hpp"
 29 #include "memory/iterator.hpp"
 30 #include "oops/array.hpp"
 31 #include "oops/oop.hpp"
 32 #include "utilities/debug.hpp"
 33 #include "utilities/globalDefinitions.hpp"
 34 #include "utilities/ostream.hpp"
 35 #include "utilities/powerOfTwo.hpp"
 36 
 37 // A growable array.
 38 
 39 /*************************************************************************/
 40 /*                                                                       */
 41 /*     WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING   */
 42 /*                                                                       */
 43 /* Should you use GrowableArrays to contain handles you must be certain  */
 44 /* that the GrowableArray does not outlive the HandleMark that contains  */
 45 /* the handles. Since GrowableArrays are typically resource allocated    */
 46 /* the following is an example of INCORRECT CODE,                        */
 47 /*                                                                       */
 48 /* ResourceMark rm;                                                      */
 49 /* GrowableArray<Handle>* arr = new GrowableArray<Handle>(size);         */
 50 /* if (blah) {                                                           */
 51 /*    while (...) {                                                      */

 81   int _capacity;
 82 
 83   GrowableArrayBase(int capacity, int initial_len) :
 84       _len(initial_len),
 85       _capacity(capacity) {
 86     assert(_len >= 0 && _len <= _capacity, "initial_len too big");
 87   }
 88 
 89   ~GrowableArrayBase() {}
 90 
 91 public:
 92   int   length() const          { return _len; }
 93   int   capacity() const        { return _capacity; }
 94 
 95   bool  is_empty() const        { return _len == 0; }
 96   bool  is_nonempty() const     { return _len != 0; }
 97   bool  is_full() const         { return _len == _capacity; }
 98 };
 99 
100 template <typename E> class GrowableArrayIterator;
101 template <typename E, typename UnaryPredicate> class GrowableArrayFilterIterator;
102 
103 // Extends GrowableArrayBase with a typed data array.
104 //
105 // E: Element type
106 //
107 // The "view" adds function that don't grow or deallocate
108 // the _data array, so there's no need for an allocator.
109 //
110 // The "view" can be used to type erase the allocator classes
111 // of GrowableArrayWithAllocator.
112 template <typename E>
113 class GrowableArrayView : public GrowableArrayBase {
114 protected:
115   E* _data; // data array
116 
117   GrowableArrayView(E* data, int capacity, int initial_len) :
118       GrowableArrayBase(capacity, initial_len), _data(data) {}
119 
120   ~GrowableArrayView() {}
121 

414     int new_len = this->_len + array_len;
415     if (new_len >= this->_capacity) grow(new_len);
416 
417     for (int j = this->_len - 1; j >= idx; j--) {
418       this->_data[j + array_len] = this->_data[j];
419     }
420 
421     for (int j = 0; j < array_len; j++) {
422       this->_data[idx + j] = array->at(j);
423     }
424 
425     this->_len += array_len;
426   }
427 
428   void appendAll(const GrowableArrayView<E>* l) {
429     for (int i = 0; i < l->length(); i++) {
430       this->at_put_grow(this->_len, l->at(i), E());
431     }
432   }
433 
434   void appendAll(const Array<E>* l) {
435     for (int i = 0; i < l->length(); i++) {
436       this->at_put_grow(this->_len, l->at(i), E());
437     }
438   }
439 
440   // Binary search and insertion utility.  Search array for element
441   // matching key according to the static compare function.  Insert
442   // that element if not already in the list.  Assumes the list is
443   // already sorted according to compare function.
444   template <int compare(const E&, const E&)> E insert_sorted(const E& key) {
445     bool found;
446     int location = GrowableArrayView<E>::template find_sorted<E, compare>(key, found);
447     if (!found) {
448       insert_before(location, key);
449     }
450     return this->at(location);
451   }
452 
453   E insert_sorted(CompareClosure<E>* cc, const E& key) {
454     bool found;
455     int location = find_sorted(cc, key, found);
456     if (!found) {
457       insert_before(location, key);
458     }
459     return this->at(location);

868     this->clear_and_deallocate();
869   }
870 
871   void* operator new(size_t size) {
872     return AnyObj::operator new(size, MT);
873   }
874 
875   void* operator new(size_t size, const std::nothrow_t&  nothrow_constant) throw() {
876     return AnyObj::operator new(size, nothrow_constant, MT);
877   }
878   void operator delete(void *p) {
879     AnyObj::operator delete(p);
880   }
881 };
882 
883 // Custom STL-style iterator to iterate over GrowableArrays
884 // It is constructed by invoking GrowableArray::begin() and GrowableArray::end()
885 template <typename E>
886 class GrowableArrayIterator : public StackObj {
887   friend class GrowableArrayView<E>;
888   template <typename F, typename UnaryPredicate> friend class GrowableArrayFilterIterator;
889 
890  private:
891   const GrowableArrayView<E>* _array; // GrowableArray we iterate over
892   int _position;                      // The current position in the GrowableArray
893 
894   // Private constructor used in GrowableArray::begin() and GrowableArray::end()
895   GrowableArrayIterator(const GrowableArrayView<E>* array, int position) : _array(array), _position(position) {
896     assert(0 <= position && position <= _array->length(), "illegal position");
897   }
898 
899  public:
900   GrowableArrayIterator() : _array(nullptr), _position(0) { }
901   GrowableArrayIterator& operator++() { ++_position; return *this; }
902   E operator*()                       { return _array->at(_position); }
903 
904   bool operator==(const GrowableArrayIterator& rhs)  {
905     assert(_array == rhs._array, "iterator belongs to different array");
906     return _position == rhs._position;
907   }
908 
909   bool operator!=(const GrowableArrayIterator& rhs)  {
910     assert(_array == rhs._array, "iterator belongs to different array");
911     return _position != rhs._position;
912   }
913 };
914 
915 // Custom STL-style iterator to iterate over elements of a GrowableArray that satisfy a given predicate
916 template <typename E, class UnaryPredicate>
917 class GrowableArrayFilterIterator : public StackObj {
918   friend class GrowableArrayView<E>;
919 
920  private:
921   const GrowableArrayView<E>* _array; // GrowableArray we iterate over
922   int _position;                      // Current position in the GrowableArray
923   UnaryPredicate _predicate;          // Unary predicate the elements of the GrowableArray should satisfy
924 
925  public:
926   GrowableArrayFilterIterator(const GrowableArray<E>* array, UnaryPredicate filter_predicate) :
927       _array(array), _position(0), _predicate(filter_predicate) {
928     // Advance to first element satisfying the predicate
929     while(!at_end() && !_predicate(_array->at(_position))) {
930       ++_position;
931     }
932   }
933 
934   GrowableArrayFilterIterator<E, UnaryPredicate>& operator++() {
935     do {
936       // Advance to next element satisfying the predicate
937       ++_position;
938     } while(!at_end() && !_predicate(_array->at(_position)));
939     return *this;
940   }
941 
942   E operator*() { return _array->at(_position); }
943 
944   bool operator==(const GrowableArrayIterator<E>& rhs)  {
945     assert(_array == rhs._array, "iterator belongs to different array");
946     return _position == rhs._position;
947   }
948 
949   bool operator!=(const GrowableArrayIterator<E>& rhs)  {
950     assert(_array == rhs._array, "iterator belongs to different array");
951     return _position != rhs._position;
952   }
953 
954   bool operator==(const GrowableArrayFilterIterator<E, UnaryPredicate>& rhs)  {
955     assert(_array == rhs._array, "iterator belongs to different array");
956     return _position == rhs._position;
957   }
958 
959   bool operator!=(const GrowableArrayFilterIterator<E, UnaryPredicate>& rhs)  {
960     assert(_array == rhs._array, "iterator belongs to different array");
961     return _position != rhs._position;
962   }
963 
964   bool at_end() const {
965     return _array == nullptr || _position == _array->end()._position;
966   }
967 };
968 
969 // Arrays for basic types
970 typedef GrowableArray<int> intArray;
971 typedef GrowableArray<int> intStack;
972 typedef GrowableArray<bool> boolArray;
973 
974 #endif // SHARE_UTILITIES_GROWABLEARRAY_HPP
< prev index next >