view anagram/support/agmap.h @ 7:57b2cc9b87f7

Use memcpy instead of strncpy when we know the length anyway. Modern gcc seems to think it knows how to detect misuse of strncpy, but it's wrong (in fact: very, very wrong) and the path of least resistance is to not try to fight with it.
author David A. Holland
date Mon, 30 May 2022 23:47:52 -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.
 *
 * agmap.h
 */

#ifndef AGMAP_H
#define AGMAP_H

#include "agbaltree.h"


template <class Key, class Value>
class AgMap {
public:
  class Wrapper {
  public:
    Key key;
    Value value;
    int operator < (const Wrapper &w) const { return key < w.key; }
    Wrapper(const Key &k, const Value &v) : key(k), value(v) {}
  };

  AgBalancedTree<Wrapper> tree;
  friend class AgBalancedTree<Wrapper>;

public:
  Value &operator [] (const Key &k);
  int includes(const Key &k, Value &v);
};

template <class Key, class Value>
Value &AgMap<Key,Value>::operator [] (const Key &k) {
  Wrapper wrapper(k, Value());
  Wrapper *result = &wrapper;
  tree.identify(result);
  return result->value;
}

template <class Key, class Value>
int AgMap<Key,Value>::includes(const Key &k, Value &v) {
  Wrapper wrapper(k,v);
  Wrapper *result = &wrapper;
  int flag = tree.identify(result);
  v = result->value;
  return flag;
}


#endif /* AGMAP_H */