comparator.cpp 1.5 KB
Newer Older
W
wangyunlai.wyl 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved.
miniob is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
         http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details. */

//
// Created by wangyunlai on 2021/6/11.
//

#include <string.h>
羽飞's avatar
羽飞 已提交
16
#include <algorithm>
羽飞's avatar
羽飞 已提交
17
#include "common/defs.h"
W
wangyunlai.wyl 已提交
18

19 20 21
namespace common {


W
wangyunlai.wyl 已提交
22 23 24 25 26 27 28 29 30
int compare_int(void *arg1, void *arg2)
{
  int v1 = *(int *)arg1;
  int v2 = *(int *)arg2;
  return v1 - v2;
}

int compare_float(void *arg1, void *arg2)
{
L
Longda Feng 已提交
31 32
  float v1 = *(float *)arg1;
  float v2 = *(float *)arg2;
W
wangyunlai.wyl 已提交
33
  float cmp = v1 - v2;
羽飞's avatar
羽飞 已提交
34
  if (cmp > EPSILON) {
W
wangyunlai.wyl 已提交
35 36
    return 1;
  }
羽飞's avatar
羽飞 已提交
37
  if (cmp < -EPSILON) {
W
wangyunlai.wyl 已提交
38 39 40 41 42
    return -1;
  }
  return 0;
}

羽飞's avatar
羽飞 已提交
43
int compare_string(void *arg1, int arg1_max_length, void *arg2, int arg2_max_length)
W
wangyunlai.wyl 已提交
44 45 46
{
  const char *s1 = (const char *)arg1;
  const char *s2 = (const char *)arg2;
羽飞's avatar
羽飞 已提交
47
  int maxlen = std::min(arg1_max_length, arg2_max_length);
L
Longda Feng 已提交
48
  int result = strncmp(s1, s2, maxlen);
羽飞's avatar
羽飞 已提交
49 50 51 52 53 54 55 56 57 58 59 60
  if (0 != result) {
    return result;
  }

  if (arg1_max_length > maxlen) {
    return s1[maxlen] - 0;
  }

  if (arg2_max_length > maxlen) {
    return 0 - s2[maxlen];
  }
  return 0;
W
wangyunlai.wyl 已提交
61
}
62 63

}