1 28 29 package com.caucho.db.sql; 30 31 import com.caucho.log.Log; 32 import com.caucho.util.IntArray; 33 34 import java.sql.SQLException ; 35 import java.util.logging.Logger ; 36 37 abstract class Order { 38 private static final Logger log = Log.open(Order.class); 39 40 private boolean _isAscending = true; 41 42 protected Order _next; 43 44 47 public boolean isAscending() 48 { 49 return _isAscending; 50 } 51 52 55 public void setAscending(boolean isAscending) 56 { 57 _isAscending = isAscending; 58 } 59 60 63 static Order append(Order order, Order next) 64 { 65 if (order == null) 66 return next; 67 else { 68 order._next = next; 69 return order; 70 } 71 } 72 73 abstract public int compare(SelectResult result, int indexA, int indexB) 74 throws SQLException ; 75 76 79 public void sort(SelectResult result, IntArray rowArray) 80 throws SQLException 81 { 82 int []rows = rowArray.getArray(); 83 int size = rowArray.size(); 84 85 sort(result, rows, 0, size); 86 } 87 88 91 private void sort(SelectResult result, int []rows, int offset, int length) 92 throws SQLException 93 { 94 if (length > 3) { 95 int head = offset; 96 int right = offset + length - 1; 97 int tail = right; 98 int pivot = offset + length / 2; 99 int pivotIndex = rows[pivot]; 100 101 int temp = rows[right]; 103 rows[right] = pivotIndex; 104 rows[pivot] = temp; 105 106 --tail; 107 108 for (;;) { 109 110 while (head <= tail && compare(result, rows[head], pivotIndex) < 0) { 111 ++head; 112 } 113 114 while (head <= tail && compare(result, rows[tail], pivotIndex) > 0) { 115 --tail; 116 } 117 118 if (head > tail) { 119 break; 120 } 121 122 temp = rows[head]; 124 rows[head++] = rows[tail]; 125 rows[tail--] = temp; 126 } 127 128 if (compare(result, rows[head], pivotIndex) > 0) { 129 temp = rows[head]; 131 rows[head] = rows[right]; 132 rows[right] = temp; 133 } 134 135 if (offset < head) { 136 sort(result, rows, offset, head - offset); 137 } 138 139 if (right > head) { 140 sort(result, rows, head+1, right - head); 141 } 142 } 143 else if (length == 3) { 144 int indexA = rows[offset + 0]; 145 int indexB = rows[offset + 1]; 146 int indexC = rows[offset + 2]; 147 148 if (compare(result, indexB, indexA) < 0) { 149 int temp = indexA; 150 indexA = indexB; 151 indexB = temp; 152 } 153 154 if (compare(result, indexB, indexC) <= 0) { 156 } 157 else if (compare(result, indexC, indexA) < 0) { 159 int temp = indexC; 160 indexC = indexB; 161 indexB = indexA; 162 indexA = temp; 163 } 164 else { 166 int temp = indexC; 167 indexC = indexB; 168 indexB = temp; 169 } 170 171 rows[offset + 0] = indexA; 172 rows[offset + 1] = indexB; 173 rows[offset + 2] = indexC; 174 } 175 else if (length == 2) { 176 int indexA = rows[offset]; 177 int indexB = rows[offset + 1]; 178 179 if (compare(result, indexB, indexA) < 0) { 180 int temp = indexB; 181 indexB = indexA; 182 indexA = temp; 183 } 184 185 rows[offset + 0] = indexA; 186 rows[offset + 1] = indexB; 187 } 188 else { 189 } 191 } 192 } 193 | Popular Tags |