< 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 

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






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

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

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






















































906 // Arrays for basic types
907 typedef GrowableArray<int> intArray;
908 typedef GrowableArray<int> intStack;
909 typedef GrowableArray<bool> boolArray;
910 
911 #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 

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

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