--- old/src/share/vm/utilities/growableArray.hpp 2019-02-15 19:02:49.878398497 +0300 +++ new/src/share/vm/utilities/growableArray.hpp 2019-02-15 19:02:49.782401848 +0300 @@ -168,6 +168,10 @@ GrowableArray(int initial_size, bool C_heap = false, MEMFLAGS F = mtInternal) : GenericGrowableArray(initial_size, 0, C_heap, F) { _data = (E*)raw_allocate(sizeof(E)); +// Needed for Visual Studio 2012 and older +#ifdef _MSC_VER +#pragma warning(suppress: 4345) +#endif for (int i = 0; i < _max; i++) ::new ((void*)&_data[i]) E(); } @@ -372,6 +376,40 @@ void sort(int f(E*,E*), int stride) { qsort(_data, length() / stride, sizeof(E) * stride, (_sort_Fn)f); } + + // Binary search and insertion utility. Search array for element + // matching key according to the static compare function. Insert + // that element is not already in the list. Assumes the list is + // already sorted according to compare function. + template E insert_sorted(const E& key) { + bool found; + int location = find_sorted(key, found); + if (!found) { + insert_before(location, key); + } + return at(location); + } + + template int find_sorted(const K& key, bool& found) { + found = false; + int min = 0; + int max = length() - 1; + + while (max >= min) { + int mid = (int)(((uint)max + min) / 2); + E value = at(mid); + int diff = compare(key, value); + if (diff > 0) { + min = mid + 1; + } else if (diff < 0) { + max = mid - 1; + } else { + found = true; + return mid; + } + } + return min; + } }; // Global GrowableArray methods (one instance in the library per each 'E' type). @@ -385,6 +423,10 @@ E* newData = (E*)raw_allocate(sizeof(E)); int i = 0; for ( ; i < _len; i++) ::new ((void*)&newData[i]) E(_data[i]); +// Needed for Visual Studio 2012 and older +#ifdef _MSC_VER +#pragma warning(suppress: 4345) +#endif for ( ; i < _max; i++) ::new ((void*)&newData[i]) E(); for (i = 0; i < old_max; i++) _data[i].~E(); if (on_C_heap() && _data != NULL) {