Skip to content
Browse files

Use explicit reference to vector in SymbolTable.

Renamed data structure typedefs to be more Ruby-like.
Added explicit check for equality of hashval in tests for symbol clashes.
  • Loading branch information...
1 parent 8dc150e commit 0053c4b1074cfe610a12c527590ac3ff0154bd48 Brian Ford committed Aug 14, 2008
Showing with 28 additions and 13 deletions.
  1. +4 −4 vm/symboltable.cpp
  2. +5 −6 vm/symboltable.hpp
  3. +19 −3 vm/test/test_symboltable.hpp
View
8 vm/symboltable.cpp
@@ -13,14 +13,14 @@ namespace rubinius {
hashval hash = String::hash_str((unsigned char*)str.c_str(), str.size());
- symbol_map::iterator entry = symbols.find(hash);
+ SymbolMap::iterator entry = symbols.find(hash);
if(entry == symbols.end()) {
sym = add(str);
- symbol_ids v(1, sym);
+ SymbolIds v(1, sym);
symbols[hash] = v;
} else {
- symbol_ids v = entry->second;
- for(symbol_ids::iterator i = v.begin(); i != v.end(); i++) {
+ SymbolIds& v = entry->second;
+ for(SymbolIds::iterator i = v.begin(); i != v.end(); i++) {
if(strings[*i] == str) return Symbol::from_index(state, *i);
}
sym = add(str);
View
11 vm/symboltable.hpp
@@ -4,7 +4,6 @@
#include "oop.hpp"
#include "prelude.hpp"
-#include <map>
#include <string>
#include <vector>
#include <tr1/unordered_map>
@@ -27,9 +26,9 @@
namespace rubinius {
class String;
- typedef std::vector<std::string> symbol_strings;
- typedef std::vector<std::size_t> symbol_ids;
- typedef std::tr1::unordered_map<hashval, symbol_ids> symbol_map;
+ typedef std::vector<std::string> SymbolStrings;
+ typedef std::vector<std::size_t> SymbolIds;
+ typedef std::tr1::unordered_map<hashval, SymbolIds> SymbolMap;
class SymbolTable {
public:
@@ -40,8 +39,8 @@ namespace rubinius {
size_t size();
private:
- symbol_map symbols;
- symbol_strings strings;
+ SymbolMap symbols;
+ SymbolStrings strings;
size_t add(std::string str);
};
View
22 vm/test/test_symboltable.hpp
@@ -22,19 +22,35 @@ class TestSymbolTable : public CxxTest::TestSuite {
delete state;
}
- void test_lookup() {
+ void test_lookup_with_c_str() {
OBJECT sym = symbols->lookup(state, "unique");
TS_ASSERT(sym->symbol_p());
OBJECT sym2 = symbols->lookup(state, "unique");
TS_ASSERT_EQUALS(sym, sym2);
}
+ void test_lookup_with_std_string() {
+ std::string str("unique");
+ std::string str2("uniquer");
+
+ OBJECT sym = symbols->lookup(state, str);
+ TS_ASSERT(sym->symbol_p());
+
+ OBJECT sym2 = symbols->lookup(state, str2);
+ TS_ASSERT_DIFFERS(sym, sym2);
+ }
+
void test_lookup_colliding_hash() {
OBJECT sym, sym2;
+ const char* str = "__uint_fast64_t";
+ const char* str2 = "TkIF_MOD";
+
+ TS_ASSERT_EQUALS(String::hash_str((unsigned char*)str, std::strlen(str)),
+ String::hash_str((unsigned char*)str2, std::strlen(str2)));
- sym = symbols->lookup(state, "__uint_fast64_t");
- sym2 = symbols->lookup(state, "TkIF_MOD");
+ sym = symbols->lookup(state, str);
+ sym2 = symbols->lookup(state, str2);
TS_ASSERT(sym != sym2);
}

0 comments on commit 0053c4b

Please sign in to comment.
Something went wrong with that request. Please try again.