File: trie.c

package info (click to toggle)
rsynth 2.0-6
  • links: PTS
  • area: non-free
  • in suites: woody
  • size: 720 kB
  • ctags: 544
  • sloc: ansic: 5,535; sh: 1,249; makefile: 117
file content (80 lines) | stat: -rw-r--r-- 1,191 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <config.h>
/* $Id: trie.c,v 1.13 1994/11/08 13:30:50 a904209 Exp a904209 $
 */
char *trie_id = "$Id: trie.c,v 1.13 1994/11/08 13:30:50 a904209 Exp a904209 $";
#include <useconfig.h>
#include <stdio.h>
#include "proto.h"
#include "trie.h"

struct trie_s
 {
  struct trie_s *otherwise;
  struct trie_s *more;
  void *value;
  char ch;
 };

void
trie_insert(r, s, value)
trie_ptr *r;
char *s;
void *value;
{
 trie_ptr p = NULL;
 char ch;
 while ((ch = *s++))
  {
   while ((p = *r))
    {
     if (p->ch == ch)
      break;
     else
      r = &p->otherwise;
    }
   if (!p)
    {
     p = (trie_ptr) malloc(sizeof(*p));
     memset(p, 0, sizeof(*p));
     p->ch = ch;
     *r = p;
    }
   r = &p->more;
  }
 p->value = value;
}

void *
trie_lookup(r, sp)
trie_ptr *r;
char **sp;
{
 char *s = *sp;
 char *value = NULL;
 char ch;
 while ((ch = *s))
  {
   trie_ptr *l = r;
   trie_ptr p;
   while ((p = *l))
    {
     if (p->ch == ch)
      break;
     else
      l = &p->otherwise;
    }
   if (p)
    {
     *l = p->otherwise;
     p->otherwise = *r;
     *r = p;
     r = &p->more;
     value = (char *) p->value;
     s++;
    }
   else
    break;
  }
 *sp = s;
 return value;
}