index_fst_util.h 3.7 KB
Newer Older
dengyihao's avatar
dengyihao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * 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/>.
 */

#ifndef __INDEX_FST_UTIL_H__
#define __INDEX_FST_UTIL_H__

dengyihao's avatar
dengyihao 已提交
19 20 21 22
#ifdef __cplusplus
extern "C" {
#endif

dengyihao's avatar
dengyihao 已提交
23
#include "index_fst_common.h"
24
#include "tarray.h"
dengyihao's avatar
dengyihao 已提交
25 26

typedef uint64_t FstType;
27 28 29
typedef uint64_t CompiledAddr;
typedef uint64_t Output;
typedef uint8_t  PackSizes;
dengyihao's avatar
dengyihao 已提交
30

31
// A sentinel value used to indicate an empty final state
dengyihao's avatar
dengyihao 已提交
32 33 34 35 36
extern const CompiledAddr EMPTY_ADDRESS;
/// A sentinel value used to indicate an invalid state.
extern const CompiledAddr NONE_ADDRESS;

// This version number is written to every finite state transducer created by
dengyihao's avatar
dengyihao 已提交
37
// this version When a finite state transducer is read, its version number is
dengyihao's avatar
dengyihao 已提交
38
// checked against this value.
39 40 41
extern const uint64_t VERSION;
// The threshold (in number of transitions) at which an index is created for
// a node's transitions. This speeds up lookup time at the expense of FST size
dengyihao's avatar
dengyihao 已提交
42 43 44 45 46 47 48

extern const uint64_t TRANS_INDEX_THRESHOLD;
// high 4 bits is transition address packed size.
// low 4 bits is output value packed size.
//
// `0` is a legal value which means there are no transitions/outputs

49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
#define FST_SET_TRANSITION_PACK_SIZE(v, sz) \
  do {                                      \
    v = (v & 0b00001111) | (sz << 4);       \
  } while (0)
#define FST_GET_TRANSITION_PACK_SIZE(v) (((v)&0b11110000) >> 4)
#define FST_SET_OUTPUT_PACK_SIZE(v, sz) \
  do {                                  \
    v = (v & 0b11110000) | sz;          \
  } while (0)
#define FST_GET_OUTPUT_PACK_SIZE(v) ((v)&0b00001111)

#define COMMON_INPUT(idx) COMMON_INPUTS_INV[(idx)-1]

#define COMMON_INDEX(v, max, val)                 \
  do {                                            \
    val = ((uint16_t)COMMON_INPUTS[v] + 1) % 256; \
    val = val > max ? 0 : val;                    \
  } while (0)

// uint8_t commonInput(uint8_t idx);
// uint8_t commonIdx(uint8_t v, uint8_t max);

uint8_t      packSize(uint64_t n);
dengyihao's avatar
dengyihao 已提交
72 73 74 75
uint64_t     unpackUint64(uint8_t *ch, uint8_t sz);
uint8_t      packDeltaSize(CompiledAddr nodeAddr, CompiledAddr transAddr);
CompiledAddr unpackDelta(char *data, uint64_t len, uint64_t nodeAddr);

dengyihao's avatar
dengyihao 已提交
76
typedef struct FstString {
77
  uint8_t *data;
dengyihao's avatar
dengyihao 已提交
78
  uint32_t len;
79
  int32_t  ref;
dengyihao's avatar
dengyihao 已提交
80
} FstString;
dengyihao's avatar
dengyihao 已提交
81 82

typedef struct FstSlice {
83 84 85
  FstString *str;
  int32_t    start;
  int32_t    end;
dengyihao's avatar
dengyihao 已提交
86 87
} FstSlice;

dengyihao's avatar
dengyihao 已提交
88 89 90
FstSlice fstSliceCreate(uint8_t *data, uint64_t len);
FstSlice fstSliceCopy(FstSlice *s, int32_t start, int32_t end);
FstSlice fstSliceDeepCopy(FstSlice *s, int32_t start, int32_t end);
91 92 93 94
bool     fstSliceIsEmpty(FstSlice *s);
int      fstSliceCompare(FstSlice *s1, FstSlice *s2);
void     fstSliceDestroy(FstSlice *s);
uint8_t *fstSliceData(FstSlice *s, int32_t *sz);
dengyihao's avatar
dengyihao 已提交
95

dengyihao's avatar
dengyihao 已提交
96
#define FST_SLICE_LEN(s) (s->end - s->start + 1)
dengyihao's avatar
dengyihao 已提交
97

98
//// stack
dengyihao's avatar
dengyihao 已提交
99
//
100
// typedef (*StackFreeElemFn)(void *elem);
dengyihao's avatar
dengyihao 已提交
101
//
102 103 104 105
// typedef struct FstStack {
//  void   *first;
//  void   *end;
//  size_t elemSize;
dengyihao's avatar
dengyihao 已提交
106 107 108 109 110
//  size_t nElem;
//  StackFreeElemFn fn;
//} FstStack;
//
//
111 112 113 114 115
// FstStack* fstStackCreate(size_t elemSize, stackFreeElem);
// void  *fstStackPush(FstStack *s, void *elem);
// void  *fstStackTop(FstStack *s);
// size_t fstStackLen(FstStack *s);
// void  fstStackDestory(FstStack *);
dengyihao's avatar
dengyihao 已提交
116 117
//

dengyihao's avatar
dengyihao 已提交
118 119 120
#ifdef __cplusplus
}
#endif
dengyihao's avatar
dengyihao 已提交
121 122

#endif