Merge pull request #17235 from taosdata/feat/TD-17777-V30
feat(shell): Supported the word completed by press Tab key for 3.0
This commit is contained in:
commit
1c106bfd1d
|
@ -33,38 +33,35 @@
|
|||
typedef struct STireNode {
|
||||
struct STireNode** d;
|
||||
bool end; // record end flag
|
||||
}STireNode;
|
||||
} STireNode;
|
||||
|
||||
typedef struct StrName {
|
||||
char * name;
|
||||
struct StrName * next;
|
||||
}StrName;
|
||||
|
||||
char* name;
|
||||
struct StrName* next;
|
||||
} StrName;
|
||||
|
||||
typedef struct STire {
|
||||
char type; // see define TIRE_
|
||||
STireNode root;
|
||||
|
||||
StrName * head;
|
||||
StrName * tail;
|
||||
StrName* head;
|
||||
StrName* tail;
|
||||
|
||||
int count; // all count
|
||||
int ref;
|
||||
}STire;
|
||||
} STire;
|
||||
|
||||
typedef struct SMatchNode {
|
||||
char* word;
|
||||
struct SMatchNode* next;
|
||||
}SMatchNode;
|
||||
|
||||
} SMatchNode;
|
||||
|
||||
typedef struct SMatch {
|
||||
SMatchNode* head;
|
||||
SMatchNode* tail; // append node to tail
|
||||
int count;
|
||||
char pre[MAX_WORD_LEN];
|
||||
}SMatch;
|
||||
|
||||
} SMatch;
|
||||
|
||||
// ----------- interface -------------
|
||||
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -26,17 +26,16 @@ STire* createTire(char type) {
|
|||
memset(tire, 0, sizeof(STire));
|
||||
tire->ref = 1; // init is 1
|
||||
tire->type = type;
|
||||
tire->root.d = (STireNode **)taosMemoryCalloc(CHAR_CNT, sizeof(STireNode *));
|
||||
tire->root.d = (STireNode**)taosMemoryCalloc(CHAR_CNT, sizeof(STireNode*));
|
||||
return tire;
|
||||
}
|
||||
|
||||
// free tire node
|
||||
void freeTireNode(STireNode* node) {
|
||||
if (node == NULL)
|
||||
return ;
|
||||
if (node == NULL) return;
|
||||
|
||||
// nest free sub node on array d
|
||||
if(node->d) {
|
||||
if (node->d) {
|
||||
for (int i = 0; i < CHAR_CNT; i++) {
|
||||
freeTireNode(node->d[i]);
|
||||
}
|
||||
|
@ -56,9 +55,9 @@ void freeTire(STire* tire) {
|
|||
taosMemoryFree(tire->root.d);
|
||||
|
||||
// free from list
|
||||
StrName * item = tire->head;
|
||||
StrName* item = tire->head;
|
||||
while (item) {
|
||||
StrName * next = item->next;
|
||||
StrName* next = item->next;
|
||||
// free string
|
||||
taosMemoryFree(item->name);
|
||||
// free node
|
||||
|
@ -75,14 +74,14 @@ void freeTire(STire* tire) {
|
|||
|
||||
// insert a new word to list
|
||||
bool insertToList(STire* tire, char* word) {
|
||||
StrName * p = (StrName *)taosMemoryMalloc(sizeof(StrName));
|
||||
StrName* p = (StrName*)taosMemoryMalloc(sizeof(StrName));
|
||||
p->name = strdup(word);
|
||||
p->next = NULL;
|
||||
|
||||
if(tire->head == NULL) {
|
||||
if (tire->head == NULL) {
|
||||
tire->head = p;
|
||||
tire->tail = p;
|
||||
}else {
|
||||
} else {
|
||||
tire->tail->next = p;
|
||||
tire->tail = p;
|
||||
}
|
||||
|
@ -93,7 +92,7 @@ bool insertToList(STire* tire, char* word) {
|
|||
// insert a new word to tree
|
||||
bool insertToTree(STire* tire, char* word, int len) {
|
||||
int m = 0;
|
||||
STireNode ** nodes = tire->root.d;
|
||||
STireNode** nodes = tire->root.d;
|
||||
for (int i = 0; i < len; i++) {
|
||||
m = word[i] - FIRST_ASCII;
|
||||
if (m < 0 || m > CHAR_CNT) {
|
||||
|
@ -102,7 +101,7 @@ bool insertToTree(STire* tire, char* word, int len) {
|
|||
|
||||
if (nodes[m] == NULL) {
|
||||
// no pointer
|
||||
STireNode* p = (STireNode* )taosMemoryMalloc(sizeof(STireNode));
|
||||
STireNode* p = (STireNode*)taosMemoryMalloc(sizeof(STireNode));
|
||||
memset(p, 0, sizeof(STireNode));
|
||||
nodes[m] = p;
|
||||
if (i == len - 1) {
|
||||
|
@ -114,7 +113,7 @@ bool insertToTree(STire* tire, char* word, int len) {
|
|||
|
||||
if (nodes[m]->d == NULL) {
|
||||
// malloc d
|
||||
nodes[m]->d = (STireNode **)taosMemoryCalloc(CHAR_CNT, sizeof(STireNode *));
|
||||
nodes[m]->d = (STireNode**)taosMemoryCalloc(CHAR_CNT, sizeof(STireNode*));
|
||||
}
|
||||
|
||||
// move to next node
|
||||
|
@ -146,7 +145,7 @@ bool insertWord(STire* tire, char* word) {
|
|||
|
||||
// delete one word from list
|
||||
bool deleteFromList(STire* tire, char* word) {
|
||||
StrName * item = tire->head;
|
||||
StrName* item = tire->head;
|
||||
while (item) {
|
||||
if (strcmp(item->name, word) == 0) {
|
||||
// found, reset empty to delete
|
||||
|
@ -176,7 +175,7 @@ bool deleteFromTree(STire* tire, char* word, int len) {
|
|||
return false;
|
||||
} else {
|
||||
// not null
|
||||
if(i == len - 1) {
|
||||
if (i == len - 1) {
|
||||
// this is last, only set end false , not free node
|
||||
nodes[m]->end = false;
|
||||
del = true;
|
||||
|
@ -184,8 +183,7 @@ bool deleteFromTree(STire* tire, char* word, int len) {
|
|||
}
|
||||
}
|
||||
|
||||
if(nodes[m]->d == NULL)
|
||||
break;
|
||||
if (nodes[m]->d == NULL) break;
|
||||
// move to next node
|
||||
nodes = nodes[m]->d;
|
||||
}
|
||||
|
@ -216,9 +214,9 @@ bool deleteWord(STire* tire, char* word) {
|
|||
return false;
|
||||
}
|
||||
|
||||
void addWordToMatch(SMatch* match, char* word){
|
||||
void addWordToMatch(SMatch* match, char* word) {
|
||||
// malloc new
|
||||
SMatchNode* node = (SMatchNode* )taosMemoryMalloc(sizeof(SMatchNode));
|
||||
SMatchNode* node = (SMatchNode*)taosMemoryMalloc(sizeof(SMatchNode));
|
||||
memset(node, 0, sizeof(SMatchNode));
|
||||
node->word = strdup(word);
|
||||
|
||||
|
@ -234,7 +232,7 @@ void addWordToMatch(SMatch* match, char* word){
|
|||
|
||||
// enum all words from node
|
||||
void enumAllWords(STireNode** nodes, char* prefix, SMatch* match) {
|
||||
STireNode * c;
|
||||
STireNode* c;
|
||||
char word[MAX_WORD_LEN];
|
||||
int len = strlen(prefix);
|
||||
for (int i = 0; i < CHAR_CNT; i++) {
|
||||
|
@ -255,18 +253,17 @@ void enumAllWords(STireNode** nodes, char* prefix, SMatch* match) {
|
|||
addWordToMatch(match, word);
|
||||
}
|
||||
// nested call next layer
|
||||
if (c->d)
|
||||
enumAllWords(c->d, word, match);
|
||||
if (c->d) enumAllWords(c->d, word, match);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// match prefix from list
|
||||
void matchPrefixFromList(STire* tire, char* prefix, SMatch* match) {
|
||||
StrName * item = tire->head;
|
||||
StrName* item = tire->head;
|
||||
int len = strlen(prefix);
|
||||
while (item) {
|
||||
if ( strncmp(item->name, prefix, len) == 0) {
|
||||
if (strncmp(item->name, prefix, len) == 0) {
|
||||
// prefix matched
|
||||
addWordToMatch(match, item->name);
|
||||
}
|
||||
|
@ -304,29 +301,27 @@ void matchPrefixFromTree(STire* tire, char* prefix, SMatch* match) {
|
|||
if (i == len - 1) {
|
||||
// malloc match if not pass by param match
|
||||
if (root == NULL) {
|
||||
root = (SMatch* )taosMemoryMalloc(sizeof(SMatch));
|
||||
root = (SMatch*)taosMemoryMalloc(sizeof(SMatch));
|
||||
memset(root, 0, sizeof(SMatch));
|
||||
strcpy(root->pre, prefix);
|
||||
}
|
||||
|
||||
// prefix is match to end char
|
||||
if (c->d)
|
||||
enumAllWords(c->d, prefix, root);
|
||||
if (c->d) enumAllWords(c->d, prefix, root);
|
||||
} else {
|
||||
// move to next node continue match
|
||||
if(c->d == NULL)
|
||||
break;
|
||||
if (c->d == NULL) break;
|
||||
nodes = c->d;
|
||||
}
|
||||
}
|
||||
|
||||
// return
|
||||
return ;
|
||||
return;
|
||||
}
|
||||
|
||||
SMatch* matchPrefix(STire* tire, char* prefix, SMatch* match) {
|
||||
if(match == NULL) {
|
||||
match = (SMatch* )taosMemoryMalloc(sizeof(SMatch));
|
||||
if (match == NULL) {
|
||||
match = (SMatch*)taosMemoryMalloc(sizeof(SMatch));
|
||||
memset(match, 0, sizeof(SMatch));
|
||||
}
|
||||
|
||||
|
@ -348,10 +343,9 @@ SMatch* matchPrefix(STire* tire, char* prefix, SMatch* match) {
|
|||
return match;
|
||||
}
|
||||
|
||||
|
||||
// get all items from tires tree
|
||||
void enumFromList(STire* tire, SMatch* match) {
|
||||
StrName * item = tire->head;
|
||||
StrName* item = tire->head;
|
||||
while (item) {
|
||||
if (item->name[0] != 0) {
|
||||
// not delete
|
||||
|
@ -365,7 +359,7 @@ void enumFromList(STire* tire, SMatch* match) {
|
|||
|
||||
// get all items from tires tree
|
||||
void enumFromTree(STire* tire, SMatch* match) {
|
||||
char pre[2] ={0, 0};
|
||||
char pre[2] = {0, 0};
|
||||
STireNode* c;
|
||||
|
||||
// enum first layer
|
||||
|
@ -380,7 +374,7 @@ void enumFromTree(STire* tire, SMatch* match) {
|
|||
}
|
||||
|
||||
// this branch have data
|
||||
if(c->end)
|
||||
if (c->end)
|
||||
addWordToMatch(match, pre);
|
||||
else
|
||||
matchPrefix(tire, pre, match);
|
||||
|
@ -389,7 +383,7 @@ void enumFromTree(STire* tire, SMatch* match) {
|
|||
|
||||
// get all items from tires tree
|
||||
SMatch* enumAll(STire* tire) {
|
||||
SMatch* match = (SMatch* )taosMemoryMalloc(sizeof(SMatch));
|
||||
SMatch* match = (SMatch*)taosMemoryMalloc(sizeof(SMatch));
|
||||
memset(match, 0, sizeof(SMatch));
|
||||
|
||||
switch (tire->type) {
|
||||
|
@ -410,16 +404,13 @@ SMatch* enumAll(STire* tire) {
|
|||
return match;
|
||||
}
|
||||
|
||||
|
||||
// free match result
|
||||
void freeMatchNode(SMatchNode* node) {
|
||||
// first free next
|
||||
if (node->next)
|
||||
freeMatchNode(node->next);
|
||||
if (node->next) freeMatchNode(node->next);
|
||||
|
||||
// second free self
|
||||
if (node->word)
|
||||
taosMemoryFree(node->word);
|
||||
if (node->word) taosMemoryFree(node->word);
|
||||
taosMemoryFree(node);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue