indexFstSparse.c 1.7 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 18 19

FstSparseSet *sparSetCreate(int32_t sz) {
  FstSparseSet *ss = taosMemoryCalloc(1, sizeof(FstSparseSet));
wafwerar's avatar
wafwerar 已提交
20
  if (ss == NULL) {
dengyihao's avatar
dengyihao 已提交
21 22 23
    return NULL;
  }

dengyihao's avatar
dengyihao 已提交
24 25 26
  ss->dense = (uint32_t *)taosMemoryCalloc(sz, sizeof(uint32_t));
  ss->sparse = (uint32_t *)taosMemoryCalloc(sz, sizeof(uint32_t));
  ss->size = 0;
dengyihao's avatar
dengyihao 已提交
27 28 29 30 31 32
  return ss;
}
void sparSetDestroy(FstSparseSet *ss) {
  if (ss == NULL) {
    return;
  }
dengyihao's avatar
dengyihao 已提交
33 34
  taosMemoryFree(ss->dense);
  taosMemoryFree(ss->sparse);
dengyihao's avatar
dengyihao 已提交
35 36
  taosMemoryFree(ss);
}
dengyihao's avatar
dengyihao 已提交
37 38 39 40
uint32_t sparSetLen(FstSparseSet *ss) {
  // Get occupied size
  return ss == NULL ? 0 : ss->size;
}
dengyihao's avatar
dengyihao 已提交
41 42 43 44 45
uint32_t sparSetAdd(FstSparseSet *ss, uint32_t ip) {
  if (ss == NULL) {
    return 0;
  }
  uint32_t i = ss->size;
dengyihao's avatar
dengyihao 已提交
46 47
  ss->dense[i] = ip;
  ss->sparse[ip] = i;
dengyihao's avatar
dengyihao 已提交
48 49 50 51
  ss->size += 1;
  return i;
}
uint32_t sparSetGet(FstSparseSet *ss, uint32_t i) {
dengyihao's avatar
dengyihao 已提交
52 53
  // check later
  return ss->dense[i];
dengyihao's avatar
dengyihao 已提交
54 55
}
bool sparSetContains(FstSparseSet *ss, uint32_t ip) {
dengyihao's avatar
dengyihao 已提交
56 57 58 59
  uint32_t i = ss->sparse[ip];
  if (i < ss->size && ss->dense[i] == ip) {
    return true;
  } else {
dengyihao's avatar
dengyihao 已提交
60 61 62 63 64 65 66 67 68
    return false;
  }
}
void sparSetClear(FstSparseSet *ss) {
  if (ss == NULL) {
    return;
  }
  ss->size = 0;
}