All pastes #730294 Raw Edit

Miscellany

public text v1 · immutable
·published 2007-10-09 02:45 UTC
rendered paste body
// $Id: SymbolTable.cpp,v 1.1 2007/10/09 02:32:44 dmk9870 Exp dmk9870 $
// Author: David King
// Contributers:
// Description: A symbol table whose symbols are integer variables

// Revision:
// $Log: SymbolTable.cpp,v $
// Revision 1.1  2007/10/09 02:32:44  dmk9870
// Initial revision
//
//
//

#include "SymbolTable.h"
#include <iostream>
#include <string>
#include <map>
#include <assert.h>
using std::string;
using std::map;
namespace RITCS4{
  
  //ctor
  SymbolTable::SymbolTable(){
    map<string,int> myMap();
  }
  
  //adds new variable to table
  void SymbolTable::declare(string name, int init){
    assert(!contains(name));
    typedef map<string, int>::value_type Pair;
    myMap.insert(Pair(name, init));
  }

  //change the value of a variable already int the table
  void SymbolTable::set(string name, int val){
    assert(contains(name));
    myMap[name] = val;
  }

  //gets int value
  int SymbolTable::get(string name) const{
    assert(contains(name));
    map<string,int>::const_iterator iter = myMap.find(name);
    int abc = iter->second;
    return abc;
  }

  //gets size
  unsigned int SymbolTable::size() const{
    int toReturn = myMap.size();
    return toReturn;
  }

  //true iff a variable with given name is in table
  bool SymbolTable::contains(string name) const{
    map<string,int>::const_iterator iter = myMap.find(name);
    if(iter != myMap.end()){
      return true;
    }
    return false;
  }

  //outputs table
  void SymbolTable::dump( std::ostream &out) const{
    map<string, int>::const_iterator iter;
    for(iter = myMap.begin(); iter != myMap.end(); iter++){
      out << iter->first << ": " << iter->second << "\n";
    }
  }
}