refactor builder struct
This commit is contained in:
parent
06fe44cabd
commit
e769d0a002
|
@ -36,6 +36,8 @@ typedef struct FstRange {
|
|||
typedef enum { OneTransNext, OneTrans, AnyTrans, EmptyFinal} State;
|
||||
typedef enum { Included, Excluded, Unbounded} FstBound;
|
||||
|
||||
typedef enum {Ordered, OutOfOrdered, DuplicateKey} OrderType;
|
||||
|
||||
|
||||
|
||||
/*
|
||||
|
@ -66,11 +68,16 @@ typedef struct FstBuilder {
|
|||
FstCountingWriter *wrt; // The FST raw data is written directly to `wtr`.
|
||||
FstUnFinishedNodes *unfinished; // The stack of unfinished nodes
|
||||
FstRegistry* registry; // A map of finished nodes.
|
||||
SArray* last; // The last word added
|
||||
FstSlice last; // The last word added
|
||||
CompiledAddr lastAddr; // The address of the last compiled node
|
||||
uint64_t len; // num of keys added
|
||||
} FstBuilder;
|
||||
|
||||
FstBuilder *fstBuilderCreate(void *w, FstType ty);
|
||||
OrderType fstBuilderCheckLastKey(FstBuilder *b, FstSlice bs, bool ckDup);
|
||||
CompiledAddr fstBuilderCompile(FstBuilder *b, FstBuilderNode *bn);
|
||||
|
||||
|
||||
|
||||
typedef struct FstTransitions {
|
||||
FstNode *node;
|
||||
|
|
|
@ -70,11 +70,11 @@ CompiledAddr unpackDelta(char *data, uint64_t len, uint64_t nodeAddr);
|
|||
typedef struct FstSlice {
|
||||
uint8_t *data;
|
||||
uint64_t dLen;
|
||||
uint32_t start;
|
||||
uint32_t end;
|
||||
int32_t start;
|
||||
int32_t end;
|
||||
} FstSlice;
|
||||
|
||||
FstSlice fstSliceCopy(FstSlice *slice, uint32_t start, uint32_t end);
|
||||
FstSlice fstSliceCopy(FstSlice *slice, int32_t start, int32_t end);
|
||||
FstSlice fstSliceCreate(uint8_t *data, uint64_t dLen);
|
||||
bool fstSliceEmpty(FstSlice *slice);
|
||||
|
||||
|
|
|
@ -300,16 +300,30 @@ FstBuilder *fstBuilderCreate(void *w, FstType ty) {
|
|||
b->wrt = fstCountingWriterCreate(w);
|
||||
b->unfinished = fstUnFinishedNodesCreate();
|
||||
b->registry = fstRegistryCreate(10000, 2) ;
|
||||
b->last = NULL;
|
||||
b->last = fstSliceCreate(NULL, 0);
|
||||
b->lastAddr = NONE_ADDRESS;
|
||||
b->len = 0;
|
||||
return b;
|
||||
}
|
||||
|
||||
|
||||
bool fstBuilderCheckLastKey(FstBuilder *b, FstSlice bs, bool ckDupe) {
|
||||
|
||||
return true;
|
||||
|
||||
OrderType fstBuilderCheckLastKey(FstBuilder *b, FstSlice bs, bool ckDup) {
|
||||
FstSlice *input = &bs;
|
||||
if (fstSliceEmpty(&b->last)) {
|
||||
// deep copy or not
|
||||
b->last = fstSliceCopy(&bs, input->start, input->end);
|
||||
} else {
|
||||
int comp = fstSliceCompare(&b->last, &bs);
|
||||
if (comp == 0 && ckDup) {
|
||||
return DuplicateKey;
|
||||
} else if (comp == 1) {
|
||||
return OutOfOrdered;
|
||||
}
|
||||
// deep copy or not
|
||||
b->last = fstSliceCopy(&bs, input->start, input->end);
|
||||
}
|
||||
return Ordered;
|
||||
}
|
||||
CompiledAddr fstBuilderCompile(FstBuilder *b, FstBuilderNode *bn) {
|
||||
if (FST_BUILDER_NODE_IS_FINAL(bn)
|
||||
|
|
|
@ -94,7 +94,7 @@ FstSlice fstSliceCreate(uint8_t *data, uint64_t dLen) {
|
|||
FstSlice slice = {.data = data, .dLen = dLen, .start = 0, .end = dLen - 1};
|
||||
return slice;
|
||||
}
|
||||
FstSlice fstSliceCopy(FstSlice *slice, uint32_t start, uint32_t end) {
|
||||
FstSlice fstSliceCopy(FstSlice *slice, int32_t start, int32_t end) {
|
||||
FstSlice t;
|
||||
if (start >= slice->dLen || end >= slice->dLen || start > end) {
|
||||
t.data = NULL;
|
||||
|
@ -111,12 +111,10 @@ bool fstSliceEmpty(FstSlice *slice) {
|
|||
return slice->data == NULL || slice->dLen <= 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int fstSliceCompare(FstSlice *a, FstSlice *b) {
|
||||
uint32_t aLen = (a->end - a->start + 1);
|
||||
uint32_t bLen = (b->end - b->start + 1);
|
||||
uint32_t mLen = (aLen < bLen ? aLen : bLen);
|
||||
int32_t aLen = (a->end - a->start + 1);
|
||||
int32_t bLen = (b->end - b->start + 1);
|
||||
int32_t mLen = (aLen < bLen ? aLen : bLen);
|
||||
for (int i = 0; i < mLen; i++) {
|
||||
uint8_t x = a->data[i + a->start];
|
||||
uint8_t y = b->data[i + b->start];
|
||||
|
|
Loading…
Reference in New Issue