talgo.c 8.3 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
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) {
H
Hongze Cheng 已提交
178
      if (flags == TD_GT) {
179
        lidx = midx + 1;
H
Hongze Cheng 已提交
180
      } else if (flags == TD_LT) {
181
        ridx = midx - 1;
H
Hongze Cheng 已提交
182
      } else {
183 184
        break;
      }
H
more  
Hongze Cheng 已提交
185 186 187 188 189 190
    } else if (c < 0) {
      ridx = midx - 1;
    } else {
      lidx = midx + 1;
    }
  }
S
talgo  
Shengliang Guan 已提交
191

192
  if (flags == TD_EQ) {
H
refact  
Hongze Cheng 已提交
193
    return c ? NULL : p;
H
more  
Hongze Cheng 已提交
194
  } else if (flags == TD_GE) {
H
refact  
Hongze Cheng 已提交
195
    return (c <= 0) ? p : (midx + 1 < nmemb ? p + size : NULL);
196
  } else if (flags == TD_LE) {
H
refact  
Hongze Cheng 已提交
197
    return (c >= 0) ? p : (midx > 0 ? p - size : NULL);
198 199 200 201
  } else if (flags == TD_GT) {
    return (c < 0) ? p : (midx + 1 < nmemb ? p + size : NULL);
  } else if (flags == TD_LT) {
    return (c > 0) ? p : (midx > 0 ? p - size : NULL);
202
  } else {
H
more  
Hongze Cheng 已提交
203
    ASSERT(0);
204
    return NULL;
205 206
  }
}
207

S
talgo  
Shengliang Guan 已提交
208
void taosheapadjust(void *base, int32_t size, int32_t start, int32_t end, const void *parcompar,
H
more  
Hongze Cheng 已提交
209
                    __ext_compar_fn_t compar, char *buf, bool maxroot) {
S
talgo  
Shengliang Guan 已提交
210 211
  int32_t parent;
  int32_t child;
212

H
more  
Hongze Cheng 已提交
213
  char *tmp = NULL;
214 215 216 217 218
  if (buf == NULL) {
    tmp = taosMemoryMalloc(size);
  } else {
    tmp = buf;
  }
219 220 221 222 223 224 225

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

    if (maxroot) {
      while (child <= end) {
S
talgo  
Shengliang Guan 已提交
226 227
        if (child + 1 <= end &&
            (*compar)(elePtrAt(base, size, child), elePtrAt(base, size, child + 1), parcompar) < 0) {
228 229 230 231 232 233 234
          child++;
        }

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

235
        doswap(elePtrAt(base, size, parent), elePtrAt(base, size, child), size, tmp);
236 237 238 239 240 241

        parent = child;
        child = 2 * parent + 1;
      }
    } else {
      while (child <= end) {
S
talgo  
Shengliang Guan 已提交
242 243
        if (child + 1 <= end &&
            (*compar)(elePtrAt(base, size, child), elePtrAt(base, size, child + 1), parcompar) > 0) {
244 245 246 247 248 249 250
          child++;
        }

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

251
        doswap(elePtrAt(base, size, parent), elePtrAt(base, size, child), size, tmp);
252 253 254 255 256

        parent = child;
        child = 2 * parent + 1;
      }
    }
257
  }
258

259 260
  if (buf == NULL) {
    taosMemoryFree(tmp);
261 262 263
  }
}

S
talgo  
Shengliang Guan 已提交
264
void taosheapsort(void *base, int32_t size, int32_t len, const void *parcompar, __ext_compar_fn_t compar,
265
                  bool maxroot) {
S
talgo  
Shengliang Guan 已提交
266
  int32_t i;
267

H
more  
Hongze Cheng 已提交
268
  char *buf = taosMemoryCalloc(1, size);
269 270 271 272
  if (buf == NULL) {
    return;
  }

273 274
  if (base && size > 0) {
    for (i = len / 2 - 1; i >= 0; i--) {
275
      taosheapadjust(base, size, i, len - 1, parcompar, compar, buf, maxroot);
276 277 278
    }
  }

279
  taosMemoryFree(buf);
S
talgo  
Shengliang Guan 已提交
280
  /*
wafwerar's avatar
wafwerar 已提交
281
    char *buf = taosMemoryCalloc(1, size);
282

S
talgo  
Shengliang Guan 已提交
283 284 285 286
    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);
    }
287

wafwerar's avatar
wafwerar 已提交
288
    taosMemoryFreeClear(buf);
S
talgo  
Shengliang Guan 已提交
289
  */
290
}