|
|
1.1 root 1: /***********************************************************
2: Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts,
3: and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
4:
5: All Rights Reserved
6:
7: Permission to use, copy, modify, and distribute this software and its
8: documentation for any purpose and without fee is hereby granted,
9: provided that the above copyright notice appear in all copies and that
10: both that copyright notice and this permission notice appear in
11: supporting documentation, and that the names of Digital or MIT not be
12: used in advertising or publicity pertaining to distribution of the
13: software without specific, written prior permission.
14:
15: DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
16: ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
17: DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
18: ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
19: WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
20: ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
21: SOFTWARE.
22:
23: ******************************************************************/
24:
25: /* $Header: atom.c,v 1.21 87/09/11 07:18:24 toddb Exp $ */
26:
27: #include "X.h"
28: #include "Xatom.h"
29: #include "misc.h"
30:
31: #define InitialTableSize 100
32:
33: char *Xalloc();
34:
35: typedef struct _Node {
36: struct _Node *left, *right;
37: Atom a;
38: unsigned int fingerPrint;
39: char *string;
40: } NodeRec, *NodePtr;
41:
42: static Atom lastAtom = None;
43: static NodePtr atomRoot = (NodePtr)NULL;
44: static int tableLength;
45: static NodePtr *nodeTable;
46:
47: Atom
48: MakeAtom(string, len, makeit)
49: char *string;
50: int len;
51: Bool makeit;
52: {
53: register NodePtr * np;
54: int comp, i;
55: register unsigned int fp = 0;
56:
57: np = &atomRoot;
58: for (i = 0; i < (len+1)/2; i++)
59: {
60: fp = fp * 27 + string[i];
61: fp = fp * 27 + string[len - 1 - i];
62: }
63: while (*np != (NodePtr) NULL)
64: {
65: if (fp < (*np)->fingerPrint)
66: np = &((*np)->left);
67: else if (fp > (*np)->fingerPrint)
68: np = &((*np)->right);
69: else
70: { /* now start testing the strings */
71: comp = strncmp(string, (*np)->string, len);
72: if ((comp < 0) || ((comp == 0) && (len < strlen((*np)->string))))
73: np = &((*np)->left);
74: else if (comp > 0)
75: np = &((*np)->right);
76: else
77: return(*np)->a;
78: }
79: }
80: if (makeit)
81: {
82: *np = (NodePtr) Xalloc(sizeof(NodeRec));
83: (*np)->left = (*np)->right = (NodePtr) NULL;
84: (*np)->fingerPrint = fp;
85: (*np)->a = (++lastAtom);
86: strncpy((*np)->string = Xalloc(len + 1), string, len);
87: (*np)->string[len] = 0;
88: if (lastAtom >= tableLength)
89: {
90: tableLength *= 2;
91: nodeTable = (NodePtr *) Xrealloc(
92: nodeTable, tableLength * sizeof(NodePtr));
93: }
94: nodeTable[lastAtom] = *np;
95: return(*np)->a;
96: }
97: else
98: return None;
99: }
100:
101: ValidAtom(atom)
102: Atom atom;
103: {
104: return (atom != None) && (atom <= lastAtom);
105: }
106:
107: char *
108: NameForAtom(atom)
109: Atom atom;
110: {
111: NodePtr node;
112: if (atom > lastAtom) return 0;
113: if ((node = nodeTable[atom]) == (NodePtr)NULL) return 0;
114: return node->string;
115: }
116:
117: AtomError()
118: {
119: FatalError("initializing atoms");
120: }
121:
122: InitAtoms()
123: {
124: FreeAllAtoms();
125: tableLength = InitialTableSize;
126: nodeTable = (NodePtr *)Xalloc(InitialTableSize*sizeof(NodePtr));
127: nodeTable[None] = (NodePtr)NULL;
128: MakePredeclaredAtoms();
129: if (lastAtom != XA_LAST_PREDEFINED)
130: AtomError ();
131: }
132:
133: FreeAllAtoms()
134: {
135: if(atomRoot == (NodePtr)NULL)
136: return;
137: FreeAtom(atomRoot);
138: atomRoot = (NodePtr)NULL;
139: Xfree(nodeTable);
140: nodeTable = (NodePtr *)NULL;
141: lastAtom = None;
142: }
143:
144: FreeAtom(patom)
145: NodePtr patom;
146: {
147: if(patom->left)
148: FreeAtom(patom->left);
149: if(patom->right)
150: FreeAtom(patom->right);
151: Xfree(patom->string);
152: Xfree(patom);
153: }
154:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.