talgo.c 7.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
15

S
talgo  
Shengliang Guan 已提交
16
#define _DEFAULT_SOURCE
17 18
#include "talgo.h"

S
talgo  
Shengliang Guan 已提交
19 20 21 22 23 24
#define doswap(__left, __right, __size, __buf) \
  do {                                         \
    memcpy((__buf), (__left), (__size));       \
    memcpy((__left), (__right), (__size));     \
    memcpy((__right), (__buf), (__size));      \
  } while (0);
25

S
talgo  
Shengliang Guan 已提交
26 27
static void median(void *src, int64_t size, int64_t s, int64_t e, const void *param, __ext_compar_fn_t comparFn,
                   void *buf) {
S
Shengliang Guan 已提交
28
  int32_t mid = ((int32_t)(e - s) >> 1u) + (int32_t)s;
S
talgo  
Shengliang Guan 已提交
29

30 31 32
  if (comparFn(elePtrAt(src, size, mid), elePtrAt(src, size, s), param) == 1) {
    doswap(elePtrAt(src, size, mid), elePtrAt(src, size, s), size, buf);
  }
S
talgo  
Shengliang Guan 已提交
33

34 35 36 37 38 39
  if (comparFn(elePtrAt(src, size, mid), elePtrAt(src, size, e), param) == 1) {
    doswap(elePtrAt(src, size, mid), elePtrAt(src, size, s), size, buf);
    doswap(elePtrAt(src, size, mid), elePtrAt(src, size, e), size, buf);
  } else if (comparFn(elePtrAt(src, size, s), elePtrAt(src, size, e), param) == 1) {
    doswap(elePtrAt(src, size, s), elePtrAt(src, size, e), size, buf);
  }
S
talgo  
Shengliang Guan 已提交
40 41 42

  assert(comparFn(elePtrAt(src, size, mid), elePtrAt(src, size, s), param) <= 0 &&
         comparFn(elePtrAt(src, size, s), elePtrAt(src, size, e), param) <= 0);
43 44

#ifdef _DEBUG_VIEW
45 46 47
//  tTagsPrints(src[s], pOrderDesc->pColumnModel, &pOrderDesc->orderIdx);
//  tTagsPrints(src[mid], pOrderDesc->pColumnModel, &pOrderDesc->orderIdx);
//  tTagsPrints(src[e], pOrderDesc->pColumnModel, &pOrderDesc->orderIdx);
48 49 50
#endif
}

S
talgo  
Shengliang Guan 已提交
51 52
static void tInsertSort(void *src, int64_t size, int32_t s, int32_t e, const void *param, __ext_compar_fn_t comparFn,
                        void *buf) {
53 54 55 56 57 58 59 60 61 62 63
  for (int32_t i = s + 1; i <= e; ++i) {
    for (int32_t j = i; j > s; --j) {
      if (comparFn(elePtrAt(src, size, j), elePtrAt(src, size, j - 1), param) == -1) {
        doswap(elePtrAt(src, size, j), elePtrAt(src, size, j - 1), size, buf);
      } else {
        break;
      }
    }
  }
}

S
talgo  
Shengliang Guan 已提交
64 65
static void tqsortImpl(void *src, int32_t start, int32_t end, int64_t size, const void *param,
                       __ext_compar_fn_t comparFn, void *buf) {
66 67 68 69 70 71
  // short array sort, incur another sort procedure instead of quick sort process
  const int32_t THRESHOLD_SIZE = 6;
  if (end - start + 1 <= THRESHOLD_SIZE) {
    tInsertSort(src, size, start, end, param, comparFn, buf);
    return;
  }
S
talgo  
Shengliang Guan 已提交
72

73
  median(src, size, start, end, param, comparFn, buf);
S
talgo  
Shengliang Guan 已提交
74

75 76
  int32_t s = start, e = end;
  int32_t endRightS = end, startLeftS = start;
S
talgo  
Shengliang Guan 已提交
77

78 79 80 81 82 83
  while (s < e) {
    while (e > s) {
      int32_t ret = comparFn(elePtrAt(src, size, e), elePtrAt(src, size, s), param);
      if (ret < 0) {
        break;
      }
S
talgo  
Shengliang Guan 已提交
84 85

      // move the data that equals to pivotal value to the right end of the list
86 87 88 89
      if (ret == 0 && e != endRightS) {
        doswap(elePtrAt(src, size, e), elePtrAt(src, size, endRightS), size, buf);
        endRightS--;
      }
S
talgo  
Shengliang Guan 已提交
90

91 92
      e--;
    }
S
talgo  
Shengliang Guan 已提交
93

94 95 96
    if (e != s) {
      doswap(elePtrAt(src, size, e), elePtrAt(src, size, s), size, buf);
    }
S
talgo  
Shengliang Guan 已提交
97

98 99 100 101 102
    while (s < e) {
      int32_t ret = comparFn(elePtrAt(src, size, s), elePtrAt(src, size, e), param);
      if (ret > 0) {
        break;
      }
S
talgo  
Shengliang Guan 已提交
103

104 105 106 107 108 109
      if (ret == 0 && s != startLeftS) {
        doswap(elePtrAt(src, size, s), elePtrAt(src, size, startLeftS), size, buf);
        startLeftS++;
      }
      s++;
    }
S
talgo  
Shengliang Guan 已提交
110

111 112 113 114
    if (e != s) {
      doswap(elePtrAt(src, size, s), elePtrAt(src, size, e), size, buf);
    }
  }
S
talgo  
Shengliang Guan 已提交
115

116 117 118 119
  int32_t rightPartStart = e + 1;
  if (endRightS != end && e < end) {
    int32_t left = rightPartStart;
    int32_t right = end;
S
talgo  
Shengliang Guan 已提交
120

121 122
    while (right > endRightS && left <= endRightS) {
      doswap(elePtrAt(src, size, left), elePtrAt(src, size, right), size, buf);
S
talgo  
Shengliang Guan 已提交
123

124 125 126
      left++;
      right--;
    }
S
talgo  
Shengliang Guan 已提交
127

128 129
    rightPartStart += (end - endRightS);
  }
S
talgo  
Shengliang Guan 已提交
130

131 132 133 134
  int32_t leftPartEnd = e - 1;
  if (startLeftS != end && s > start) {
    int32_t left = start;
    int32_t right = leftPartEnd;
S
talgo  
Shengliang Guan 已提交
135

136 137
    while (left < startLeftS && right >= startLeftS) {
      doswap(elePtrAt(src, size, left), elePtrAt(src, size, right), size, buf);
S
talgo  
Shengliang Guan 已提交
138

139 140 141
      left++;
      right--;
    }
S
talgo  
Shengliang Guan 已提交
142

143 144
    leftPartEnd -= (startLeftS - start);
  }
S
talgo  
Shengliang Guan 已提交
145

146
  if (leftPartEnd > start) {
H
hjxilinx 已提交
147
    tqsortImpl(src, start, leftPartEnd, size, param, comparFn, buf);
148
  }
S
talgo  
Shengliang Guan 已提交
149

150
  if (rightPartStart < end) {
H
hjxilinx 已提交
151
    tqsortImpl(src, rightPartStart, end, size, param, comparFn, buf);
152 153 154
  }
}

S
talgo  
Shengliang Guan 已提交
155
void taosqsort(void *src, int64_t numOfElem, int64_t size, const void *param, __ext_compar_fn_t comparFn) {
wafwerar's avatar
wafwerar 已提交
156
  char *buf = taosMemoryCalloc(1, size);  // prepare the swap buffer
S
Shengliang Guan 已提交
157
  tqsortImpl(src, 0, (int32_t)numOfElem - 1, (int32_t)size, param, comparFn, buf);
wafwerar's avatar
wafwerar 已提交
158
  taosMemoryFreeClear(buf);
159 160
}

H
more  
Hongze Cheng 已提交
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
void *taosbsearch(const void *key, const void *base, int32_t nmemb, int32_t size, __compar_fn_t compar, int32_t flags) {
  uint8_t *p;
  int32_t  lidx;
  int32_t  ridx;
  int32_t  midx;
  int32_t  c;

  if (nmemb <= 0) return NULL;

  lidx = 0;
  ridx = nmemb - 1;
  while (lidx <= ridx) {
    midx = (lidx + ridx) / 2;
    p = (uint8_t *)base + size * midx;

    c = compar(key, p);
    if (c == 0) {
      break;
    } else if (c < 0) {
      ridx = midx - 1;
    } else {
      lidx = midx + 1;
    }
  }
S
talgo  
Shengliang Guan 已提交
185

186
  if (flags == TD_EQ) {
H
refact  
Hongze Cheng 已提交
187
    return c ? NULL : p;
H
more  
Hongze Cheng 已提交
188
  } else if (flags == TD_GE) {
H
refact  
Hongze Cheng 已提交
189
    return (c <= 0) ? p : (midx + 1 < nmemb ? p + size : NULL);
190
  } else if (flags == TD_LE) {
H
refact  
Hongze Cheng 已提交
191
    return (c >= 0) ? p : (midx > 0 ? p - size : NULL);
192
  } else {
H
more  
Hongze Cheng 已提交
193
    ASSERT(0);
194 195
  }
}
196

S
talgo  
Shengliang Guan 已提交
197
void taosheapadjust(void *base, int32_t size, int32_t start, int32_t end, const void *parcompar,
H
more  
Hongze Cheng 已提交
198
                    __ext_compar_fn_t compar, char *buf, bool maxroot) {
S
talgo  
Shengliang Guan 已提交
199 200
  int32_t parent;
  int32_t child;
201

H
more  
Hongze Cheng 已提交
202
  char *tmp = NULL;
203 204 205 206 207
  if (buf == NULL) {
    tmp = taosMemoryMalloc(size);
  } else {
    tmp = buf;
  }
208 209 210 211 212 213 214

  if (base && size > 0 && compar) {
    parent = start;
    child = 2 * parent + 1;

    if (maxroot) {
      while (child <= end) {
S
talgo  
Shengliang Guan 已提交
215 216
        if (child + 1 <= end &&
            (*compar)(elePtrAt(base, size, child), elePtrAt(base, size, child + 1), parcompar) < 0) {
217 218 219 220 221 222 223
          child++;
        }

        if ((*compar)(elePtrAt(base, size, parent), elePtrAt(base, size, child), parcompar) > 0) {
          break;
        }

224
        doswap(elePtrAt(base, size, parent), elePtrAt(base, size, child), size, tmp);
225 226 227 228 229 230

        parent = child;
        child = 2 * parent + 1;
      }
    } else {
      while (child <= end) {
S
talgo  
Shengliang Guan 已提交
231 232
        if (child + 1 <= end &&
            (*compar)(elePtrAt(base, size, child), elePtrAt(base, size, child + 1), parcompar) > 0) {
233 234 235 236 237 238 239
          child++;
        }

        if ((*compar)(elePtrAt(base, size, parent), elePtrAt(base, size, child), parcompar) < 0) {
          break;
        }

240
        doswap(elePtrAt(base, size, parent), elePtrAt(base, size, child), size, tmp);
241 242 243 244 245

        parent = child;
        child = 2 * parent + 1;
      }
    }
246
  }
247

248 249
  if (buf == NULL) {
    taosMemoryFree(tmp);
250 251 252
  }
}

S
talgo  
Shengliang Guan 已提交
253
void taosheapsort(void *base, int32_t size, int32_t len, const void *parcompar, __ext_compar_fn_t compar,
254
                  bool maxroot) {
S
talgo  
Shengliang Guan 已提交
255
  int32_t i;
256

H
more  
Hongze Cheng 已提交
257
  char *buf = taosMemoryCalloc(1, size);
258 259 260 261
  if (buf == NULL) {
    return;
  }

262 263
  if (base && size > 0) {
    for (i = len / 2 - 1; i >= 0; i--) {
264
      taosheapadjust(base, size, i, len - 1, parcompar, compar, buf, maxroot);
265 266 267
    }
  }

268
  taosMemoryFree(buf);
S
talgo  
Shengliang Guan 已提交
269
  /*
wafwerar's avatar
wafwerar 已提交
270
    char *buf = taosMemoryCalloc(1, size);
271

S
talgo  
Shengliang Guan 已提交
272 273 274 275
    for (i = len - 1; i > 0; i--) {
      doswap(elePtrAt(base, size, 0), elePtrAt(base, size, i));
      taosheapadjust(base, size, 0, i - 1, parcompar, compar, parswap, swap, maxroot);
    }
276

wafwerar's avatar
wafwerar 已提交
277
    taosMemoryFreeClear(buf);
S
talgo  
Shengliang Guan 已提交
278
  */
279
}