Back to index

texmacs  1.0.7.15
assoc_environment.hpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : assoc_environment.hpp
00004 * DESCRIPTION: association arrays as environments
00005 * COPYRIGHT  : (C) 2006  Joris van der Hoeven
00006 *******************************************************************************
00007 * This software falls under the GNU general public license version 3 or later.
00008 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
00009 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
00010 ******************************************************************************/
00011 
00012 #ifndef ASSOC_ENVIRONMENT_H
00013 #define ASSOC_ENVIRONMENT_H
00014 #include "environment.hpp"
00015 
00016 /******************************************************************************
00017 * An assoc environment is represented by an array of assoc_nodes
00018 ******************************************************************************/
00019 
00020 class assoc_node {
00021   static tree uninit;
00022 public:
00023   int  key;    // key of the pair
00024   tree val;    // value of the pair
00025 public:
00026   inline assoc_node (): val (uninit) {}
00027 };
00028 
00029 /******************************************************************************
00030 * Assoc environments
00031 ******************************************************************************/
00032 
00033 class assoc_environment;
00034 class assoc_environment_rep: public environment_rep {
00035   static tree uninit;
00036 public:
00037   int n;
00038   assoc_node* a;
00039 public:
00040   inline assoc_environment_rep (int n2):
00041     n (n2), a (tm_new_array<assoc_node> (n)) {}
00042   inline ~assoc_environment_rep () {
00043     tm_delete_array (a); }
00044   inline void raw_write (int i, int key, const tree& val) {
00045     a[i].key= key; a[i].val= val; }
00046   inline void raw_write (int i, const string& key, const tree& val) {
00047     a[i].key= (int) make_tree_label (key); a[i].val= val; }
00048 
00049   bool contains (int key);
00050   tree read (int key);
00051   void write (int key, const tree& val);
00052   void remove (int key);
00053   void print (const string& prefix);
00054 };
00055 
00056 class assoc_environment {
00057   ABSTRACT(assoc_environment);
00058   inline assoc_environment (int n):
00059     rep (tm_new<assoc_environment_rep> (n)) {}
00060   inline tree operator [] (int key) {
00061     return rep->read (key); }
00062   inline friend environment as_environment (const assoc_environment& env) {
00063     return environment ((environment_rep*) env.rep); }
00064   inline friend assoc_environment as_assoc_environment (const environment& e) {
00065     return assoc_environment ((assoc_environment_rep*) as_pointer (e)); }
00066 };
00067 ABSTRACT_CODE(assoc_environment);
00068 
00069 assoc_environment copy (assoc_environment env);
00070 int weak_hash (assoc_environment env);
00071 bool weak_equal (assoc_environment env1, assoc_environment env2);
00072 
00073 #endif // defined ASSOC_ENVIRONMENT_H