view anagram/support/agdict.cpp @ 4:bebb2ba69e1d

maybe help with getting tex to fail properly on error
author David A. Holland
date Sat, 18 Apr 2020 17:12:17 -0400
parents 13d2b8934445
children
line wrap: on
line source

/*
 * 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];
}