SearchTree: push/pull by string keys

This commit is contained in:
Timerix22 2022-07-21 00:07:50 +03:00
parent 2de4506e01
commit 84c3a10035
2 changed files with 23 additions and 10 deletions

View File

@ -45,11 +45,16 @@ indexes3 splitindex(uint8 i){
}; };
} }
void ST_push(STNode* node_first, const char* key, Unitype value){ void ST_push(STNode* node_first, char* key, Unitype value){
string keyString={key, cptr_length(key)};
ST_pushString(node_first, keyString, value);
}
void ST_pushString(STNode* node_first, string key, Unitype value){
if (!node_first) throw(ERR_NULLPTR); if (!node_first) throw(ERR_NULLPTR);
STNode* node_last=node_first; STNode* node_last=node_first;
while(*key){ while(key.length--){
indexes3 i3=splitindex((uint8)*key); indexes3 i3=splitindex((uint8)*key.ptr);
if(!node_last->branches){ if(!node_last->branches){
node_last->branches=(STNode****)malloc(8*sizeof(STNode***)); node_last->branches=(STNode****)malloc(8*sizeof(STNode***));
for(uint8 i=0;i<8;i++) for(uint8 i=0;i<8;i++)
@ -68,16 +73,21 @@ void ST_push(STNode* node_first, const char* key, Unitype value){
if(!node_last->branches[i3.n32][i3.n4][i3.rem]) if(!node_last->branches[i3.n32][i3.n4][i3.rem])
node_last->branches[i3.n32][i3.n4][i3.rem]=STNode_create(); node_last->branches[i3.n32][i3.n4][i3.rem]=STNode_create();
node_last=node_last->branches[i3.n32][i3.n4][i3.rem]; node_last=node_last->branches[i3.n32][i3.n4][i3.rem];
key++; key.ptr++;
} }
node_last->value=value; node_last->value=value;
} }
Unitype ST_pull(STNode* node_first, const char* key){ Unitype ST_pull(STNode* node_first, char* key){
string keyString={key, cptr_length(key)};
return ST_pullString(node_first, keyString);
}
Unitype ST_pullString(STNode* node_first, string key){
if (!node_first) throw(ERR_NULLPTR); if (!node_first) throw(ERR_NULLPTR);
STNode* node_last=node_first; STNode* node_last=node_first;
while (*key){ while (key.length--){
indexes3 i3=splitindex((uint8)*key); indexes3 i3=splitindex((uint8)*key.ptr);
if(!node_last->branches) return UniNull; if(!node_last->branches) return UniNull;
STNode*** ptrn32=(STNode***)node_last->branches[i3.n32]; STNode*** ptrn32=(STNode***)node_last->branches[i3.n32];
if(!ptrn32) return UniNull; if(!ptrn32) return UniNull;
@ -85,7 +95,7 @@ Unitype ST_pull(STNode* node_first, const char* key){
if(!ptrn4) return UniNull; if(!ptrn4) return UniNull;
node_last=ptrn4[i3.rem]; node_last=ptrn4[i3.rem];
if(!node_last) return UniNull; if(!node_last) return UniNull;
key++; key.ptr++;
} }
return node_last->value; return node_last->value;
} }

View File

@ -5,6 +5,7 @@ extern "C" {
#endif #endif
#include "../base/base.h" #include "../base/base.h"
#include "../String/string.h"
typedef struct SearchTreeNode{ typedef struct SearchTreeNode{
struct SearchTreeNode**** branches; // *STNode[8][8][4] struct SearchTreeNode**** branches; // *STNode[8][8][4]
@ -14,8 +15,10 @@ typedef struct SearchTreeNode{
STNode* STNode_create(); STNode* STNode_create();
void STNode_free(STNode* node); void STNode_free(STNode* node);
void ST_push(STNode* node, const char* key, Unitype value); void ST_push(STNode* node, char* key, Unitype value);
Unitype ST_pull(STNode* node, const char* key); void ST_pushString(STNode* node, string key, Unitype value);
Unitype ST_pull(STNode* node, char* key);
Unitype ST_pullString(STNode* node, string key);
#if __cplusplus #if __cplusplus
} }