|
| 1 | +/* |
| 2 | + Copyright 1995-2013 Esri |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +
|
| 16 | + For additional information, contact: |
| 17 | + Environmental Systems Research Institute, Inc. |
| 18 | + Attn: Contracts Dept |
| 19 | + 380 New York Street |
| 20 | + Redlands, California, USA 92373 |
| 21 | +
|
| 22 | + email: contracts@esri.com |
| 23 | + */ |
| 24 | +package com.esri.core.geometry; |
| 25 | + |
| 26 | +final class BucketSort { |
| 27 | + AttributeStreamOfInt32 m_buckets; |
| 28 | + AttributeStreamOfInt32 m_bucketed_indices; |
| 29 | + double m_min_value; |
| 30 | + double m_max_value; |
| 31 | + double m_dy; |
| 32 | + |
| 33 | + static int MAXBUCKETS = 65536; |
| 34 | + |
| 35 | + public BucketSort() { |
| 36 | + m_buckets = new AttributeStreamOfInt32(0); |
| 37 | + m_bucketed_indices = new AttributeStreamOfInt32(0); |
| 38 | + m_min_value = 1; |
| 39 | + m_max_value = -1; |
| 40 | + m_dy = NumberUtils.TheNaN; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Executes sort on the Bucket_sort. The result is fed into the indices |
| 45 | + * array in the range between begin (inclusive) and end (exclusive). Uses |
| 46 | + * user supplied sorter to execute sort on each bucket. Users either supply |
| 47 | + * the sorter and use this method of Bucket_sort class, or use other methods |
| 48 | + * to form the buckets and take care of bucket sorting themselves. |
| 49 | + */ |
| 50 | + public void sort(AttributeStreamOfInt32 indices, int begin, int end, |
| 51 | + ClassicSort sorter) { |
| 52 | + if (end - begin < 32) { |
| 53 | + sorter.userSort(begin, end, indices); |
| 54 | + return; |
| 55 | + } |
| 56 | + boolean b_fallback = true; |
| 57 | + try { |
| 58 | + double miny = NumberUtils.positiveInf(); |
| 59 | + double maxy = NumberUtils.negativeInf(); |
| 60 | + for (int i = begin; i < end; i++) { |
| 61 | + double y = sorter.getValue(indices.get(i)); |
| 62 | + if (y < miny) |
| 63 | + miny = y; |
| 64 | + if (y > maxy) |
| 65 | + maxy = y; |
| 66 | + } |
| 67 | + |
| 68 | + if (reset(end - begin, miny, maxy, end - begin)) { |
| 69 | + for (int i = begin; i < end; i++) { |
| 70 | + int vertex = indices.get(i); |
| 71 | + double y = sorter.getValue(vertex); |
| 72 | + int bucket = getBucket(y); |
| 73 | + m_buckets.set(bucket, m_buckets.get(bucket) + 1);// counting |
| 74 | + // values |
| 75 | + // in a |
| 76 | + // bucket. |
| 77 | + m_bucketed_indices.write(i - begin, vertex); |
| 78 | + } |
| 79 | + |
| 80 | + // Recalculate buckets to contain start positions of buckets. |
| 81 | + int c = m_buckets.get(0); |
| 82 | + m_buckets.set(0, 0); |
| 83 | + for (int i = 1, n = m_buckets.size(); i < n; i++) { |
| 84 | + int b = m_buckets.get(i); |
| 85 | + m_buckets.set(i, c); |
| 86 | + c += b; |
| 87 | + } |
| 88 | + |
| 89 | + for (int i = begin; i < end; i++) { |
| 90 | + int vertex = m_bucketed_indices.read(i - begin); |
| 91 | + double y = sorter.getValue(vertex); |
| 92 | + int bucket = getBucket(y); |
| 93 | + int bucket_index = m_buckets.get(bucket); |
| 94 | + indices.set(bucket_index + begin, vertex); |
| 95 | + m_buckets.set(bucket, bucket_index + 1); |
| 96 | + } |
| 97 | + |
| 98 | + b_fallback = false; |
| 99 | + } |
| 100 | + } catch (Exception e) { |
| 101 | + m_buckets.resize(0); |
| 102 | + m_bucketed_indices.resize(0); |
| 103 | + } |
| 104 | + |
| 105 | + if (b_fallback) { |
| 106 | + sorter.userSort(begin, end, indices); |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + int j = 0; |
| 111 | + for (int i = 0, n = m_buckets.size(); i < n; i++) { |
| 112 | + int j0 = j; |
| 113 | + j = m_buckets.get(i); |
| 114 | + if (j > j0) |
| 115 | + sorter.userSort(begin + j0, begin + j, indices); |
| 116 | + } |
| 117 | + assert (j == end); |
| 118 | + |
| 119 | + if (getBucketCount() > 100) // some heuristics to preserve memory |
| 120 | + { |
| 121 | + m_buckets.resize(0); |
| 122 | + m_bucketed_indices.resize(0); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Clears and resets Bucket_sort to the new state, preparing for the |
| 128 | + * accumulation of new data. |
| 129 | + * |
| 130 | + * @param bucket_count |
| 131 | + * - the number of buckets. Usually equal to the number of |
| 132 | + * elements to sort. |
| 133 | + * @param min_value |
| 134 | + * - the minimum value of elements to sort. |
| 135 | + * @param max_value |
| 136 | + * - the maximum value of elements to sort. |
| 137 | + * @param capacity |
| 138 | + * - the number of elements to sort (-1 if not known). The |
| 139 | + * bucket_count are usually equal. |
| 140 | + * @return Returns False, if the bucket sort cannot be used with the given |
| 141 | + * parameters. The method also can throw out of memory exception. In |
| 142 | + * the later case, one should fall back to the regular sort. |
| 143 | + */ |
| 144 | + private boolean reset(int bucket_count, double min_value, double max_value, |
| 145 | + int capacity) { |
| 146 | + if (bucket_count < 2 || max_value == min_value) |
| 147 | + return false; |
| 148 | + |
| 149 | + int bc = Math.min(MAXBUCKETS, bucket_count); |
| 150 | + m_buckets.reserve(bc); |
| 151 | + m_buckets.resize(bc); |
| 152 | + m_buckets.setRange(0, 0, m_buckets.size()); |
| 153 | + m_min_value = min_value; |
| 154 | + m_max_value = max_value; |
| 155 | + m_bucketed_indices.resize(capacity); |
| 156 | + |
| 157 | + m_dy = (max_value - min_value) / (bc - 1); |
| 158 | + return true; |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Adds new element to the bucket builder. The value must be between |
| 163 | + * min_value and max_value. |
| 164 | + * |
| 165 | + * @param The |
| 166 | + * value used for bucketing. |
| 167 | + * @param The |
| 168 | + * index of the element to store in the buffer. Usually it is an |
| 169 | + * index into some array, where the real elements are stored. |
| 170 | + */ |
| 171 | + private int getBucket(double value) { |
| 172 | + assert (value >= m_min_value && value <= m_max_value); |
| 173 | + int bucket = (int) ((value - m_min_value) / m_dy); |
| 174 | + return bucket; |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * Returns the bucket count. |
| 179 | + */ |
| 180 | + private int getBucketCount() { |
| 181 | + return m_buckets.size(); |
| 182 | + } |
| 183 | + |
| 184 | +} |
0 commit comments