indexFstSparse.c 2.3 KB
Newer Older
dengyihao's avatar
add UT  
dengyihao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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/>.
 */

dengyihao's avatar
dengyihao 已提交
16
#include "indexFstSparse.h"
dengyihao's avatar
dengyihao 已提交
17

dengyihao's avatar
dengyihao 已提交
18 19 20 21 22
static void sparSetUtil(int32_t *buf, int32_t cap) {
  for (int32_t i = 0; i < cap; i++) {
    buf[i] = -1;
  }
}
dengyihao's avatar
dengyihao 已提交
23 24
FstSparseSet *sparSetCreate(int32_t sz) {
  FstSparseSet *ss = taosMemoryCalloc(1, sizeof(FstSparseSet));
wafwerar's avatar
wafwerar 已提交
25
  if (ss == NULL) {
dengyihao's avatar
dengyihao 已提交
26 27 28
    return NULL;
  }

dengyihao's avatar
dengyihao 已提交
29 30
  ss->dense = (int32_t *)taosMemoryMalloc(sz * sizeof(int32_t));
  ss->sparse = (int32_t *)taosMemoryMalloc(sz * sizeof(int32_t));
dengyihao's avatar
dengyihao 已提交
31 32 33
  sparSetUtil(ss->dense, sz);
  sparSetUtil(ss->sparse, sz);

dengyihao's avatar
dengyihao 已提交
34 35
  ss->cap = sz;

dengyihao's avatar
dengyihao 已提交
36
  ss->size = 0;
dengyihao's avatar
dengyihao 已提交
37 38 39 40 41 42
  return ss;
}
void sparSetDestroy(FstSparseSet *ss) {
  if (ss == NULL) {
    return;
  }
dengyihao's avatar
dengyihao 已提交
43 44
  taosMemoryFree(ss->dense);
  taosMemoryFree(ss->sparse);
dengyihao's avatar
dengyihao 已提交
45 46
  taosMemoryFree(ss);
}
dengyihao's avatar
dengyihao 已提交
47 48 49 50
uint32_t sparSetLen(FstSparseSet *ss) {
  // Get occupied size
  return ss == NULL ? 0 : ss->size;
}
dengyihao's avatar
dengyihao 已提交
51
bool sparSetAdd(FstSparseSet *ss, int32_t ip, int32_t *idx) {
dengyihao's avatar
dengyihao 已提交
52
  if (ss == NULL) {
dengyihao's avatar
dengyihao 已提交
53 54
    return false;
  }
dengyihao's avatar
dengyihao 已提交
55
  if (ip >= ss->cap || ip < 0) {
dengyihao's avatar
dengyihao 已提交
56
    return false;
dengyihao's avatar
dengyihao 已提交
57 58
  }
  uint32_t i = ss->size;
dengyihao's avatar
dengyihao 已提交
59 60
  ss->dense[i] = ip;
  ss->sparse[ip] = i;
dengyihao's avatar
dengyihao 已提交
61
  ss->size += 1;
dengyihao's avatar
dengyihao 已提交
62 63 64 65

  if (idx != NULL) *idx = i;

  return true;
dengyihao's avatar
dengyihao 已提交
66
}
dengyihao's avatar
dengyihao 已提交
67
bool sparSetGet(FstSparseSet *ss, int32_t idx, int32_t *ip) {
dengyihao's avatar
dengyihao 已提交
68
  if (idx >= ss->cap || idx >= ss->size || idx < 0) {
dengyihao's avatar
dengyihao 已提交
69 70 71 72 73 74 75
    return false;
  }
  int32_t val = ss->dense[idx];
  if (ip != NULL) {
    *ip = val;
  }
  return val == -1 ? false : true;
dengyihao's avatar
dengyihao 已提交
76
}
dengyihao's avatar
dengyihao 已提交
77
bool sparSetContains(FstSparseSet *ss, int32_t ip) {
dengyihao's avatar
dengyihao 已提交
78
  if (ip >= ss->cap || ip < 0) {
dengyihao's avatar
dengyihao 已提交
79 80 81
    return false;
  }

dengyihao's avatar
dengyihao 已提交
82
  int32_t i = ss->sparse[ip];
dengyihao's avatar
dengyihao 已提交
83
  if (i >= 0 && i < ss->cap && i < ss->size && ss->dense[i] == ip) {
dengyihao's avatar
dengyihao 已提交
84 85
    return true;
  } else {
dengyihao's avatar
dengyihao 已提交
86 87 88 89 90 91 92
    return false;
  }
}
void sparSetClear(FstSparseSet *ss) {
  if (ss == NULL) {
    return;
  }
dengyihao's avatar
dengyihao 已提交
93 94
  sparSetUtil(ss->dense, ss->cap);
  sparSetUtil(ss->sparse, ss->cap);
dengyihao's avatar
dengyihao 已提交
95 96
  ss->size = 0;
}