< prev index next >

src/share/vm/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_VM_UTILITIES_GROWABLEARRAY_HPP
  26 #define SHARE_VM_UTILITIES_GROWABLEARRAY_HPP
  27 
  28 #include "memory/allocation.hpp"
  29 #include "memory/allocation.inline.hpp"

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


 370     assert(0 <= idx && idx <= _len, "illegal index");
 371     check_nesting();
 372     int array_len = array->length();
 373     int new_len = _len + array_len;
 374     if (new_len >= _max) grow(new_len);
 375 
 376     for (int j = _len - 1; j >= idx; j--) {
 377       _data[j + array_len] = _data[j];
 378     }
 379 
 380     for (int j = 0; j < array_len; j++) {
 381       _data[idx + j] = array->_data[j];
 382     }
 383 
 384     _len += array_len;
 385   }
 386 
 387   void appendAll(const GrowableArray<E>* l) {
 388     for (int i = 0; i < l->_len; i++) {
 389       raw_at_put_grow(_len, l->_data[i], E());






 390     }
 391   }
 392 
 393   void sort(int f(E*,E*)) {
 394     qsort(_data, length(), sizeof(E), (_sort_Fn)f);
 395   }
 396   // sort by fixed-stride sub arrays:
 397   void sort(int f(E*,E*), int stride) {
 398     qsort(_data, length() / stride, sizeof(E) * stride, (_sort_Fn)f);
 399   }
 400 
 401   // Binary search and insertion utility.  Search array for element
 402   // matching key according to the static compare function.  Insert
 403   // that element is not already in the list.  Assumes the list is
 404   // already sorted according to compare function.
 405   template <int compare(const E&, const E&)> E insert_sorted(E& key) {
 406     bool found;
 407     int location = find_sorted<E, compare>(key, found);
 408     if (!found) {
 409       insert_before(location, key);




  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_VM_UTILITIES_GROWABLEARRAY_HPP
  26 #define SHARE_VM_UTILITIES_GROWABLEARRAY_HPP
  27 
  28 #include "memory/allocation.hpp"
  29 #include "memory/allocation.inline.hpp"
  30 #include "oops/array.hpp"
  31 #include "utilities/debug.hpp"
  32 #include "utilities/globalDefinitions.hpp"
  33 
  34 // A growable array.
  35 
  36 /*************************************************************************/
  37 /*                                                                       */
  38 /*     WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING   */
  39 /*                                                                       */
  40 /* Should you use GrowableArrays to contain handles you must be certain  */
  41 /* the the GrowableArray does not outlive the HandleMark that contains   */
  42 /* the handles. Since GrowableArrays are typically resource allocated    */
  43 /* the following is an example of INCORRECT CODE,                        */
  44 /*                                                                       */
  45 /* ResourceMark rm;                                                      */
  46 /* GrowableArray<Handle>* arr = new GrowableArray<Handle>(size);         */
  47 /* if (blah) {                                                           */
  48 /*    while (...) {                                                      */
  49 /*      HandleMark hm;                                                   */
  50 /*      ...                                                              */


 371     assert(0 <= idx && idx <= _len, "illegal index");
 372     check_nesting();
 373     int array_len = array->length();
 374     int new_len = _len + array_len;
 375     if (new_len >= _max) grow(new_len);
 376 
 377     for (int j = _len - 1; j >= idx; j--) {
 378       _data[j + array_len] = _data[j];
 379     }
 380 
 381     for (int j = 0; j < array_len; j++) {
 382       _data[idx + j] = array->_data[j];
 383     }
 384 
 385     _len += array_len;
 386   }
 387 
 388   void appendAll(const GrowableArray<E>* l) {
 389     for (int i = 0; i < l->_len; i++) {
 390       raw_at_put_grow(_len, l->_data[i], E());
 391     }
 392   }
 393 
 394   void appendAll(const Array<E>* l) {
 395     for (int i = 0; i < l->length(); i++) {
 396       raw_at_put_grow(_len, l->at(i), E());
 397     }
 398   }
 399 
 400   void sort(int f(E*,E*)) {
 401     qsort(_data, length(), sizeof(E), (_sort_Fn)f);
 402   }
 403   // sort by fixed-stride sub arrays:
 404   void sort(int f(E*,E*), int stride) {
 405     qsort(_data, length() / stride, sizeof(E) * stride, (_sort_Fn)f);
 406   }
 407 
 408   // Binary search and insertion utility.  Search array for element
 409   // matching key according to the static compare function.  Insert
 410   // that element is not already in the list.  Assumes the list is
 411   // already sorted according to compare function.
 412   template <int compare(const E&, const E&)> E insert_sorted(E& key) {
 413     bool found;
 414     int location = find_sorted<E, compare>(key, found);
 415     if (!found) {
 416       insert_before(location, key);


< prev index next >