/[LeafOK_CVS]/lbbs/src/trie_dict.c
ViewVC logotype

Diff of /lbbs/src/trie_dict.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.2 by sysadm, Fri May 16 04:54:35 2025 UTC Revision 1.3 by sysadm, Sat May 17 02:41:19 2025 UTC
# Line 16  Line 16 
16    
17  #include "trie_dict.h"  #include "trie_dict.h"
18  #include <stdlib.h>  #include <stdlib.h>
19    #include <stdio.h>
20    
21  int char_to_offset(char c)  inline int char_to_offset(char c)
22  {  {
23          if (c >= 'A' && c <= 'Z')          return (unsigned char)c;
         {  
                 return (c - 'A');  
         }  
         else if (c >= 'a' && c <= 'z')  
         {  
                 return (c - 'a' + 26);  
         }  
         else if (c >= '0' && c <= '9')  
         {  
                 return (c - '0' + 52); // 26 * 2  
         }  
         else if (c == '_')  
         {  
                 return 62; // (26 * 2 + 10)  
         }  
   
         return -1;  
24  }  }
25    
26  char offset_to_char(int i)  char offset_to_char(int i)
27  {  {
28          if (i < 0)          if (i > 255)
29          {          {
30                  return '\0';                  return '\0';
31          }          }
         else if (i < 26)  
         {  
                 return (char)('A' + i);  
         }  
         else if (i < 52)  
         {  
                 return (char)('a' + (i - 26));  
         }  
         else if (i < 62)  
         {  
                 return (char)('0' + (i - 52));  
         }  
         else if (i == 62)  
         {  
                 return '_';  
         }  
32    
33          return '\0';          return (char)(i % 256 - 256);
34  }  }
35    
36  TRIE_NODE *trie_dict_create(void)  TRIE_NODE *trie_dict_create(void)
# Line 88  void trie_dict_destroy(TRIE_NODE *p_dict Line 56  void trie_dict_destroy(TRIE_NODE *p_dict
56                          trie_dict_destroy(p_dict->p_nodes[i]);                          trie_dict_destroy(p_dict->p_nodes[i]);
57                          p_dict->p_nodes[i] = NULL;                          p_dict->p_nodes[i] = NULL;
58                  }                  }
59    
60                    p_dict->flags[i] = 0;
61          }          }
62    
63          free(p_dict);          free(p_dict);
# Line 100  int trie_dict_set(TRIE_NODE *p_dict, con Line 70  int trie_dict_set(TRIE_NODE *p_dict, con
70          while (key != NULL && *key != '\0')          while (key != NULL && *key != '\0')
71          {          {
72                  offset = char_to_offset(*key);                  offset = char_to_offset(*key);
73                    if (offset < 0 || offset >= TRIE_CHILDREN) // incorrect key character
74                    {
75                            return -1;
76                    }
77    
78                  if (*(key + 1) == '\0')                  if (*(key + 1) == '\0')
79                  {                  {
# Line 135  int trie_dict_get(TRIE_NODE *p_dict, con Line 109  int trie_dict_get(TRIE_NODE *p_dict, con
109          while (key != NULL && *key != '\0')          while (key != NULL && *key != '\0')
110          {          {
111                  offset = char_to_offset(*key);                  offset = char_to_offset(*key);
112                    if (offset < 0 || offset >= TRIE_CHILDREN) // incorrect key character
113                    {
114                            return -1;
115                    }
116    
117                  if (*(key + 1) == '\0')                  if (*(key + 1) == '\0')
118                  {                  {
# Line 169  int trie_dict_del(TRIE_NODE *p_dict, con Line 147  int trie_dict_del(TRIE_NODE *p_dict, con
147          while (key != NULL && *key != '\0')          while (key != NULL && *key != '\0')
148          {          {
149                  offset = char_to_offset(*key);                  offset = char_to_offset(*key);
150                    if (offset < 0 || offset >= TRIE_CHILDREN) // incorrect key character
151                    {
152                            return -1;
153                    }
154    
155                  if (*(key + 1) == '\0')                  if (*(key + 1) == '\0')
156                  {                  {
# Line 206  static void _trie_dict_traverse(TRIE_NOD Line 188  static void _trie_dict_traverse(TRIE_NOD
188    
189          for (int i = 0; i < TRIE_CHILDREN; i++)          for (int i = 0; i < TRIE_CHILDREN; i++)
190          {          {
                 key[depth] = offset_to_char(i);  
                 key[depth + 1] = '\0';  
   
191                  if (p_dict->flags[i] != 0)                  if (p_dict->flags[i] != 0)
192                  {                  {
193                            key[depth] = offset_to_char(i);
194                            key[depth + 1] = '\0';
195                          (*cb)(key, p_dict->values[i]);                          (*cb)(key, p_dict->values[i]);
196                  }                  }
197    
198                  if (p_dict->p_nodes[i] != NULL && depth + 1 < TRIE_MAX_KEY_LEN)                  if (p_dict->p_nodes[i] != NULL && depth + 1 < TRIE_MAX_KEY_LEN)
199                  {                  {
200                            key[depth] = offset_to_char(i);
201                          _trie_dict_traverse(p_dict->p_nodes[i], cb, key, depth + 1);                          _trie_dict_traverse(p_dict->p_nodes[i], cb, key, depth + 1);
202                  }                  }
203          }                }      
# Line 224  static void _trie_dict_traverse(TRIE_NOD Line 206  static void _trie_dict_traverse(TRIE_NOD
206  void trie_dict_traverse(TRIE_NODE *p_dict, trie_dict_traverse_cb cb)  void trie_dict_traverse(TRIE_NODE *p_dict, trie_dict_traverse_cb cb)
207  {  {
208          char key[TRIE_MAX_KEY_LEN + 1];          char key[TRIE_MAX_KEY_LEN + 1];
209            key[0] = '\0';
210    
211          _trie_dict_traverse(p_dict, cb, key, 0);          _trie_dict_traverse(p_dict, cb, key, 0);
212  }  }


Legend:
Removed lines/characters  
Changed lines/characters
  Added lines/characters

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1