index_fst_node.c 3.4 KB
Newer Older
dengyihao's avatar
dengyihao 已提交
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/>.
 */
dengyihao's avatar
dengyihao 已提交
15 16 17 18
#include "index_fst_node.h"

FstBuilderNode *fstBuilderNodeDefault() {
  FstBuilderNode *bn = malloc(sizeof(FstBuilderNode));
19 20 21
  bn->isFinal = false;
  bn->finalOutput = 0;
  bn->trans = taosArrayInit(16, sizeof(FstTransition));
dengyihao's avatar
dengyihao 已提交
22 23
  return bn;
}
dengyihao's avatar
dengyihao 已提交
24
void fstBuilderNodeDestroy(FstBuilderNode *node) {
25 26 27
  if (node == NULL) {
    return;
  }
dengyihao's avatar
dengyihao 已提交
28

dengyihao's avatar
dengyihao 已提交
29 30
  taosArrayDestroy(node->trans);
  free(node);
31
}
dengyihao's avatar
dengyihao 已提交
32 33

bool fstBuilderNodeEqual(FstBuilderNode *n1, FstBuilderNode *n2) {
34 35 36 37
  if (n1 == n2) {
    return true;
  }
  if (n1 == NULL || n2 == NULL) {
dengyihao's avatar
dengyihao 已提交
38 39
    return false;
  }
dengyihao's avatar
dengyihao 已提交
40

dengyihao's avatar
dengyihao 已提交
41 42 43
  if (n1->isFinal != n2->isFinal || n1->finalOutput != n2->finalOutput) {
    return false;
  }
44 45 46
  size_t s1 = n1->trans ? taosArrayGetSize(n1->trans) : 0;
  size_t s2 = n2->trans ? taosArrayGetSize(n2->trans) : 0;
  if (s1 != s2) {
dengyihao's avatar
dengyihao 已提交
47 48 49
    return false;
  }
  for (size_t i = 0; i < s1; i++) {
dengyihao's avatar
dengyihao 已提交
50 51 52 53
    FstTransition *t1 = taosArrayGet(n1->trans, i);
    FstTransition *t2 = taosArrayGet(n2->trans, i);
    if (t1->inp != t2->inp || t1->out != t2->out || t1->addr != t2->addr) {
      return false;
54
    }
dengyihao's avatar
dengyihao 已提交
55
  }
56

dengyihao's avatar
dengyihao 已提交
57 58
  return true;
}
dengyihao's avatar
dengyihao 已提交
59
FstBuilderNode *fstBuilderNodeClone(FstBuilderNode *src) {
60 61 62 63
  FstBuilderNode *node = malloc(sizeof(FstBuilderNode));
  if (node == NULL) {
    return NULL;
  }
dengyihao's avatar
dengyihao 已提交
64

65 66
  //
  size_t  sz = taosArrayGetSize(src->trans);
dengyihao's avatar
dengyihao 已提交
67 68 69 70
  SArray *trans = taosArrayInit(sz, sizeof(FstTransition));

  for (size_t i = 0; i < sz; i++) {
    FstTransition *tran = taosArrayGet(src->trans, i);
71
    taosArrayPush(trans, tran);
dengyihao's avatar
dengyihao 已提交
72 73
  }

74
  node->trans = trans;
dengyihao's avatar
dengyihao 已提交
75 76 77 78
  node->isFinal = src->isFinal;
  node->finalOutput = src->finalOutput;
  return node;
}
79
// not destroy src, User's bussiness
dengyihao's avatar
dengyihao 已提交
80
void fstBuilderNodeCloneFrom(FstBuilderNode *dst, FstBuilderNode *src) {
81 82 83
  if (dst == NULL || src == NULL) {
    return;
  }
dengyihao's avatar
dengyihao 已提交
84

85
  dst->isFinal = src->isFinal;
dengyihao's avatar
dengyihao 已提交
86
  dst->finalOutput = src->finalOutput;
dengyihao's avatar
dengyihao 已提交
87

88 89
  // release free  avoid mem leak
  taosArrayDestroy(dst->trans);
dengyihao's avatar
dengyihao 已提交
90
  size_t sz = taosArrayGetSize(src->trans);
91
  dst->trans = taosArrayInit(sz, sizeof(FstTransition));
dengyihao's avatar
dengyihao 已提交
92
  for (size_t i = 0; i < sz; i++) {
93
    FstTransition *trn = taosArrayGet(src->trans, i);
dengyihao's avatar
dengyihao 已提交
94
    taosArrayPush(dst->trans, trn);
95
  }
dengyihao's avatar
dengyihao 已提交
96 97
}

98 99
// bool fstBuilderNodeCompileTo(FstBuilderNode *b, FstCountingWriter *wrt, CompiledAddr lastAddr, CompiledAddr
// startAddr) {
dengyihao's avatar
dengyihao 已提交
100

101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
// size_t sz = taosArrayGetSize(b->trans);
// assert(sz < 256);
// if (FST_BUILDER_NODE_IS_FINAL(b)
//    && FST_BUILDER_NODE_TRANS_ISEMPTY(b)
//    && FST_BUILDER_NODE_FINALOUTPUT_ISZERO(b)) {
//  return true;
//} else if (sz != 1 || b->isFinal) {
//  // AnyTrans->Compile(w, addr, node);
//} else {
//  FstTransition *tran = taosArrayGet(b->trans, 0);
//  if (tran->addr == lastAddr && tran->out == 0) {
//     //OneTransNext::compile(w, lastAddr, tran->inp);
//     return true;
//  } else {
//    //OneTrans::Compile(w, lastAddr, *tran);
//     return true;
//  }
//}
// return true;
//}