00001 /* 00002 * Copyright (c) 2000, 2020, Oracle and/or its affiliates. 00003 * 00004 * Licensed under the Universal Permissive License v 1.0 as shown at 00005 * http://oss.oracle.com/licenses/upl. 00006 */ 00007 #ifndef COH_ARRAYS_HPP 00008 #define COH_ARRAYS_HPP 00009 00010 #include "coherence/lang.ns" 00011 00012 #include "coherence/util/Comparator.hpp" 00013 #include "coherence/util/List.hpp" 00014 00015 COH_OPEN_NAMESPACE2(coherence,util) 00016 00017 00018 /** 00019 * This class contains various methods for manipulating arrays (such as 00020 * sorting and searching). 00021 * 00022 * @author tb 2008.04.04 00023 */ 00024 class COH_EXPORT Arrays 00025 { 00026 // ----- static methods ------------------------------------------------- 00027 00028 public: 00029 /** 00030 * Copies an array from the specified source array, beginning at the 00031 * specified position, to the specified position of the destination 00032 * array. 00033 * 00034 * @param haoSrc the source array 00035 * @param iSrcStart starting position in the source array 00036 * @param haoDest the destination array 00037 * @param iDestStart starting position in the destination data 00038 * @param cElements the number of array elements to be copied 00039 * @throws IndexOutOfBoundsException if copying would cause 00040 * access of data outside array bounds 00041 */ 00042 static void copy(ObjectArray::Handle haoSrc, 00043 size32_t iSrcStart, ObjectArray::Handle haoDest, 00044 size32_t iDestStart, size32_t cElements); 00045 00046 /** 00047 * Reverses the order of the elements in the specified array. 00048 * 00049 * This method runs in linear time. 00050 * 00051 * @param hao the array whose elements are to be reversed. 00052 * 00053 * since 12.2.1.3 00054 */ 00055 static void reverse(ObjectArray::Handle hao); 00056 00057 /** 00058 * Sorts the specified array of objects according to the order induced 00059 * by the specified comparator. All elements in the array must be 00060 * mutually comparable by the specified comparator. 00061 * 00062 * This sort is guaranteed to be stable: equal elements will not be 00063 * reordered as a result of the sort.<p> 00064 * 00065 * @param hao the array to be sorted 00066 * @param hComparator the comparator to determine the order of the 00067 * array. A null value indicates that the 00068 * elements' natural ordering should be used 00069 * @throws ClassCastException if the array contains elements that are 00070 * not mutually comparable 00071 */ 00072 static void sort(ObjectArray::Handle hao, 00073 Comparator::Handle hComparator = NULL); 00074 00075 /** 00076 * Swaps the elements at the specified positions in the specified array. 00077 * (If the specified positions are equal, invoking this method leaves 00078 * the array unchanged.) 00079 * 00080 * @param hao the array in which to swap elements. 00081 * @param i the index of one element to be swapped. 00082 * @param j the index of the other element to be swapped. 00083 * 00084 * @throws IndexOutOfBoundsException if either <tt>i</tt> or <tt>j</tt> 00085 * is out of range (i < 0 || i >= hList->length 00086 * || j < 0 || j >= hList->length). 00087 * 00088 * @since 12.2.1.3 00089 */ 00090 static void swap(ObjectArray::Handle hao, size32_t i, size32_t j); 00091 00092 /** 00093 * Performs a binary search for the specified element in the 00094 * specified sorted array using the Comparator to compare elements. 00095 * 00096 * @param vaoSorted the sorted Object array to search 00097 * @param vo the value element to find 00098 * @param vComparator the Comparator (optional) 00099 * 00100 * @return the non-negative index of the element, or a negative index 00101 * which is the -index - 1 where the element would be inserted 00102 */ 00103 static int32_t binarySearch(ObjectArray::View vaoSorted, 00104 Object::View vo, Comparator::View vComparator = NULL); 00105 00106 /** 00107 * Performs a binary search for the specified element in a part of the 00108 * specified sorted array using the Comparator to compare elements. 00109 * 00110 * @param vaoSorted the sorted Object array to search 00111 * @param iStart the inclusive start index 00112 * @param iEnd the exclusive end index 00113 * @param vo the value element to find 00114 * @param vComparator the Comparator (optional) 00115 * 00116 * @return the non-negative index of the element, or a negative index 00117 * which is the -index - 1 where the element would be inserted 00118 */ 00119 static int32_t binarySearch(ObjectArray::View vaoSorted, 00120 size32_t iStart, size32_t iEnd, Object::View vo, 00121 Comparator::View vComparator = NULL); 00122 00123 /** 00124 * Returns a fixed-size list backed by the specified array. (Changes 00125 * to the returned list "write through" to the array.) This method 00126 * acts as bridge between array-based and collection-based APIs. 00127 * This method also provides a convenient way to create a fixed-size 00128 * list initialized to contain several elements: 00129 * 00130 * @param vao the array by which the list will be backed 00131 * 00132 * @return a list view of the specified array 00133 * 00134 * @since Coherence 12.1.2 00135 */ 00136 static List::View asList(ObjectArray::View vao); 00137 }; 00138 00139 COH_CLOSE_NAMESPACE2 00140 00141 #endif // COH_ARRAYS_HPP