Mercurial > ~dholland > hg > ag > index.cgi
diff 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 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/anagram/support/agdict.cpp Sat Dec 22 17:52:45 2007 -0500 @@ -0,0 +1,71 @@ +/* + * AnaGram, A System for Syntax Directed Programming + * Copyright 1997-2002 Parsifal Software. All Rights Reserved. + * See the file COPYING for license and usage terms. + * + * agdict.cpp - new string dictionary + */ + +#include "agstring.h" +#include "agdict.h" +#include "assert.h" + +//#define INCLUDE_LOGGING +#include "log.h" + + +int AgStringDictionary::Tree::compare(const int &x, const int &y) const { + LOGSECTION("AgStringDictionary::Tree::compare"); + LOGV(x) LCV(stringStore[x]); + LOGV(y) LCV(stringStore[y]); + LOGV(stringStore[x] < stringStore[y]); + LOGV(stringStore[y] < stringStore[x]); + + if (stringStore[x] < stringStore[y]) { + return -1; + } + return (stringStore[y] < stringStore[x]); +} + +void AgStringDictionary::Tree::reset() { + LOGSECTION("AgStringDictionary::Tree::reset"); + AgBalancedTree<int>::reset(); + stringStore.reset(); + stringStore.push(AgString()).push(AgString()); + insert(0); + LOGV(size()) LCV(stringStore.size()); +}; + +int AgStringDictionary::operator << (const AgString s) { + LOGSECTION("AgStringDictionary::operator <<"); + int newIndex = tree.size(); + tree.stringStore.top() = s; + + LOGV(newIndex) + LCV(tree.stringStore.size()) + LCV(tree.stringStore.top()) + LCV(s); + + if (!tree.identify(newIndex)) { + tree.stringStore.push(AgString()); + } + LOGV(newIndex); + return newIndex; +} + +int AgStringDictionary::operator [] (const AgString s) { + int n = tree.stringStore.size() - 1; + int actualIndex = n; + tree.stringStore.top() = s; + if (tree.lookup(actualIndex)) { + return actualIndex; + } + return 0; +} + +AgString AgStringDictionary::operator [] (const unsigned x) { + LOGSECTION("AgStringDictionary::operator []"); + assert(x < tree.size()); + LOGV(x) LCV(tree.stringStore[x]); + return tree.stringStore[x]; +}