indexFstNode.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
#include "indexFstNode.h"
dengyihao's avatar
dengyihao 已提交
16

dengyihao's avatar
dengyihao 已提交
17
FstBuilderNode* fstBuilderNodeDefault() {
wafwerar's avatar
wafwerar 已提交
18
  FstBuilderNode* bn = taosMemoryMalloc(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) {
dengyihao's avatar
dengyihao 已提交
25 26 27
  if (node == NULL) {
    return;
  }
dengyihao's avatar
dengyihao 已提交
28

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

dengyihao's avatar
dengyihao 已提交
33
bool fstBuilderNodeEqual(FstBuilderNode* n1, FstBuilderNode* n2) {
dengyihao's avatar
dengyihao 已提交
34 35 36 37 38 39
  if (n1 == n2) {
    return true;
  }
  if (n1 == NULL || n2 == NULL) {
    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
  size_t s1 = n1->trans ? taosArrayGetSize(n1->trans) : 0;
  size_t s2 = n2->trans ? taosArrayGetSize(n2->trans) : 0;
dengyihao's avatar
dengyihao 已提交
46 47 48
  if (s1 != s2) {
    return false;
  }
dengyihao's avatar
dengyihao 已提交
49
  for (size_t i = 0; i < s1; i++) {
dengyihao's avatar
dengyihao 已提交
50 51
    FstTransition* t1 = taosArrayGet(n1->trans, i);
    FstTransition* t2 = taosArrayGet(n2->trans, i);
dengyihao's avatar
dengyihao 已提交
52 53 54
    if (t1->inp != t2->inp || t1->out != t2->out || t1->addr != t2->addr) {
      return false;
    }
dengyihao's avatar
dengyihao 已提交
55
  }
56

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

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

  for (size_t i = 0; i < sz; i++) {
dengyihao's avatar
dengyihao 已提交
70
    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) {
dengyihao's avatar
dengyihao 已提交
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++) {
dengyihao's avatar
dengyihao 已提交
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;
//}