view anagram/support/agdict.cpp @ 24:a4899cdfc2d6 default tip

Obfuscate the regexps to strip off the IBM compiler's copyright banners. I don't want bots scanning github to think they're real copyright notices because that could cause real problems.
author David A. Holland
date Mon, 13 Jun 2022 00:40:23 -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];
}