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
407 int new_len = this->_len + array_len;
408 if (new_len >= this->_capacity) grow(new_len);
409
410 for (int j = this->_len - 1; j >= idx; j--) {
411 this->_data[j + array_len] = this->_data[j];
412 }
413
414 for (int j = 0; j < array_len; j++) {
415 this->_data[idx + j] = array->at(j);
416 }
417
418 this->_len += array_len;
419 }
420
421 void appendAll(const GrowableArrayView<E>* l) {
422 for (int i = 0; i < l->length(); i++) {
423 this->at_put_grow(this->_len, l->at(i), E());
424 }
425 }
426
427 // Binary search and insertion utility. Search array for element
428 // matching key according to the static compare function. Insert
429 // that element if not already in the list. Assumes the list is
430 // already sorted according to compare function.
431 template <int compare(const E&, const E&)> E insert_sorted(const E& key) {
432 bool found;
433 int location = GrowableArrayView<E>::template find_sorted<E, compare>(key, found);
434 if (!found) {
435 insert_before(location, key);
436 }
437 return this->at(location);
438 }
439
440 E insert_sorted(CompareClosure<E>* cc, const E& key) {
441 bool found;
442 int location = find_sorted(cc, key, found);
443 if (!found) {
444 insert_before(location, key);
445 }
446 return this->at(location);
854 this->clear_and_deallocate();
855 }
856
857 void* operator new(size_t size) {
858 return AnyObj::operator new(size, MT);
859 }
860
861 void* operator new(size_t size, const std::nothrow_t& nothrow_constant) throw() {
862 return AnyObj::operator new(size, nothrow_constant, MT);
863 }
864 void operator delete(void *p) {
865 AnyObj::operator delete(p);
866 }
867 };
868
869 // Custom STL-style iterator to iterate over GrowableArrays
870 // It is constructed by invoking GrowableArray::begin() and GrowableArray::end()
871 template <typename E>
872 class GrowableArrayIterator : public StackObj {
873 friend class GrowableArrayView<E>;
874
875 private:
876 const GrowableArrayView<E>* _array; // GrowableArray we iterate over
877 int _position; // The current position in the GrowableArray
878
879 // Private constructor used in GrowableArray::begin() and GrowableArray::end()
880 GrowableArrayIterator(const GrowableArrayView<E>* array, int position) : _array(array), _position(position) {
881 assert(0 <= position && position <= _array->length(), "illegal position");
882 }
883
884 public:
885 GrowableArrayIterator() : _array(nullptr), _position(0) { }
886 GrowableArrayIterator& operator++() { ++_position; return *this; }
887 E operator*() { return _array->at(_position); }
888
889 bool operator==(const GrowableArrayIterator& rhs) {
890 assert(_array == rhs._array, "iterator belongs to different array");
891 return _position == rhs._position;
892 }
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
900 // Arrays for basic types
901 typedef GrowableArray<int> intArray;
902 typedef GrowableArray<int> intStack;
903 typedef GrowableArray<bool> boolArray;
904
905 #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
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 void appendAll(const Array<E>* l) {
431 for (int i = 0; i < l->length(); i++) {
432 this->at_put_grow(this->_len, l->at(i), E());
433 }
434 }
435
436 // Binary search and insertion utility. Search array for element
437 // matching key according to the static compare function. Insert
438 // that element if not already in the list. Assumes the list is
439 // already sorted according to compare function.
440 template <int compare(const E&, const E&)> E insert_sorted(const E& key) {
441 bool found;
442 int location = GrowableArrayView<E>::template find_sorted<E, compare>(key, found);
443 if (!found) {
444 insert_before(location, key);
445 }
446 return this->at(location);
447 }
448
449 E insert_sorted(CompareClosure<E>* cc, const E& key) {
450 bool found;
451 int location = find_sorted(cc, key, found);
452 if (!found) {
453 insert_before(location, key);
454 }
455 return this->at(location);
863 this->clear_and_deallocate();
864 }
865
866 void* operator new(size_t size) {
867 return AnyObj::operator new(size, MT);
868 }
869
870 void* operator new(size_t size, const std::nothrow_t& nothrow_constant) throw() {
871 return AnyObj::operator new(size, nothrow_constant, MT);
872 }
873 void operator delete(void *p) {
874 AnyObj::operator delete(p);
875 }
876 };
877
878 // Custom STL-style iterator to iterate over GrowableArrays
879 // It is constructed by invoking GrowableArray::begin() and GrowableArray::end()
880 template <typename E>
881 class GrowableArrayIterator : public StackObj {
882 friend class GrowableArrayView<E>;
883 template <typename F, typename UnaryPredicate> friend class GrowableArrayFilterIterator;
884
885 private:
886 const GrowableArrayView<E>* _array; // GrowableArray we iterate over
887 int _position; // The current position in the GrowableArray
888
889 // Private constructor used in GrowableArray::begin() and GrowableArray::end()
890 GrowableArrayIterator(const GrowableArrayView<E>* array, int position) : _array(array), _position(position) {
891 assert(0 <= position && position <= _array->length(), "illegal position");
892 }
893
894 public:
895 GrowableArrayIterator() : _array(nullptr), _position(0) { }
896 GrowableArrayIterator& operator++() { ++_position; return *this; }
897 E operator*() { return _array->at(_position); }
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 bool operator!=(const GrowableArrayIterator& rhs) {
905 assert(_array == rhs._array, "iterator belongs to different array");
906 return _position != rhs._position;
907 }
908 };
909
910 // Custom STL-style iterator to iterate over elements of a GrowableArray that satisfy a given predicate
911 template <typename E, class UnaryPredicate>
912 class GrowableArrayFilterIterator : public StackObj {
913 friend class GrowableArrayView<E>;
914
915 private:
916 const GrowableArrayView<E>* _array; // GrowableArray we iterate over
917 int _position; // Current position in the GrowableArray
918 UnaryPredicate _predicate; // Unary predicate the elements of the GrowableArray should satisfy
919
920 public:
921 GrowableArrayFilterIterator(const GrowableArray<E>* array, UnaryPredicate filter_predicate) :
922 _array(array), _position(0), _predicate(filter_predicate) {
923 // Advance to first element satisfying the predicate
924 while(!at_end() && !_predicate(_array->at(_position))) {
925 ++_position;
926 }
927 }
928
929 GrowableArrayFilterIterator<E, UnaryPredicate>& operator++() {
930 do {
931 // Advance to next element satisfying the predicate
932 ++_position;
933 } while(!at_end() && !_predicate(_array->at(_position)));
934 return *this;
935 }
936
937 E operator*() { return _array->at(_position); }
938
939 bool operator==(const GrowableArrayIterator<E>& rhs) {
940 assert(_array == rhs._array, "iterator belongs to different array");
941 return _position == rhs._position;
942 }
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 GrowableArrayFilterIterator<E, UnaryPredicate>& 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 at_end() const {
960 return _array == nullptr || _position == _array->end()._position;
961 }
962 };
963
964 // Arrays for basic types
965 typedef GrowableArray<int> intArray;
966 typedef GrowableArray<int> intStack;
967 typedef GrowableArray<bool> boolArray;
968
969 #endif // SHARE_UTILITIES_GROWABLEARRAY_HPP
|