comparison anagram/support/agdict.cpp @ 0:13d2b8934445

Import AnaGram (near-)release tree into Mercurial.
author David A. Holland
date Sat, 22 Dec 2007 17:52:45 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:13d2b8934445
1 /*
2 * AnaGram, A System for Syntax Directed Programming
3 * Copyright 1997-2002 Parsifal Software. All Rights Reserved.
4 * See the file COPYING for license and usage terms.
5 *
6 * agdict.cpp - new string dictionary
7 */
8
9 #include "agstring.h"
10 #include "agdict.h"
11 #include "assert.h"
12
13 //#define INCLUDE_LOGGING
14 #include "log.h"
15
16
17 int AgStringDictionary::Tree::compare(const int &x, const int &y) const {
18 LOGSECTION("AgStringDictionary::Tree::compare");
19 LOGV(x) LCV(stringStore[x]);
20 LOGV(y) LCV(stringStore[y]);
21 LOGV(stringStore[x] < stringStore[y]);
22 LOGV(stringStore[y] < stringStore[x]);
23
24 if (stringStore[x] < stringStore[y]) {
25 return -1;
26 }
27 return (stringStore[y] < stringStore[x]);
28 }
29
30 void AgStringDictionary::Tree::reset() {
31 LOGSECTION("AgStringDictionary::Tree::reset");
32 AgBalancedTree<int>::reset();
33 stringStore.reset();
34 stringStore.push(AgString()).push(AgString());
35 insert(0);
36 LOGV(size()) LCV(stringStore.size());
37 };
38
39 int AgStringDictionary::operator << (const AgString s) {
40 LOGSECTION("AgStringDictionary::operator <<");
41 int newIndex = tree.size();
42 tree.stringStore.top() = s;
43
44 LOGV(newIndex)
45 LCV(tree.stringStore.size())
46 LCV(tree.stringStore.top())
47 LCV(s);
48
49 if (!tree.identify(newIndex)) {
50 tree.stringStore.push(AgString());
51 }
52 LOGV(newIndex);
53 return newIndex;
54 }
55
56 int AgStringDictionary::operator [] (const AgString s) {
57 int n = tree.stringStore.size() - 1;
58 int actualIndex = n;
59 tree.stringStore.top() = s;
60 if (tree.lookup(actualIndex)) {
61 return actualIndex;
62 }
63 return 0;
64 }
65
66 AgString AgStringDictionary::operator [] (const unsigned x) {
67 LOGSECTION("AgStringDictionary::operator []");
68 assert(x < tree.size());
69 LOGV(x) LCV(tree.stringStore[x]);
70 return tree.stringStore[x];
71 }