Skip to content

Commit

Permalink
Now that put always succeeds, remove the return value.
Browse files Browse the repository at this point in the history
  • Loading branch information
robbywalker committed Oct 3, 2010
1 parent 4a5f791 commit 779bf6f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 20 deletions.
9 changes: 4 additions & 5 deletions CoalescedHashMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ CharacterMap::~CharacterMap() {
delete [] m_bucket;
}

bool CharacterMap::put(char key, long value) {
void CharacterMap::put(char key, long value) {
int index = hash(key);

// If the location is null, insert a new bucket to hashmap
if(m_bucket[index].used == false || m_bucket[index].key == key) {
m_bucket[index].key = key;
m_bucket[index].value = value;
m_bucket[index].used = true;
return true;
return;
}

// Find the right location in the cellar for this new value, starting at (tablesize - 1)
Expand All @@ -46,7 +46,8 @@ bool CharacterMap::put(char key, long value) {
if ( cursor == -1 ) {
// Table is full: re-size and try again.
resize(std::max(m_actualsize * 2, 256));
return put(key, value);
put(key, value);
return;
}

// Insert new bucket at the cursor location
Expand All @@ -60,8 +61,6 @@ bool CharacterMap::put(char key, long value) {
}

m_bucket[index].indexOfNext = cursor;

return true;
}

bool CharacterMap::get(char key, long &result) {
Expand Down
2 changes: 1 addition & 1 deletion CoalescedHashMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class CharacterMap {
~CharacterMap();

bool contains(char key);
bool put(char key, long value);
void put(char key, long value);
bool get(char key, long &result);
std::string toDebugString();
int getSize();
Expand Down
28 changes: 14 additions & 14 deletions CoalescedHashMapTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,26 +48,26 @@ class CoalescedHashMapTest : public CppUnit::TestFixture {

void testBasic() {
CPPUNIT_ASSERT(!map->contains('a'));
CPPUNIT_ASSERT(map->put('a', 100));
map->put('a', 100);
assertValue('a', 100);
}

void testOverwrite() {
CPPUNIT_ASSERT(map->put('a', 100));
map->put('a', 100);
assertValue('a', 100);

CPPUNIT_ASSERT(map->put('a', 200));
map->put('a', 200);
assertValue('a', 200);

CPPUNIT_ASSERT(map->put('a', 0));
map->put('a', 0);
assertValue('a', 0);
}

void testMany() {
CPPUNIT_ASSERT(map->put('a', 100));
CPPUNIT_ASSERT(map->put('b', 200));
CPPUNIT_ASSERT(map->put('c', 300));
CPPUNIT_ASSERT(map->put('d', 400));
map->put('a', 100);
map->put('b', 200);
map->put('c', 300);
map->put('d', 400);

assertValue('a', 100);
assertValue('b', 200);
Expand All @@ -79,7 +79,7 @@ class CoalescedHashMapTest : public CppUnit::TestFixture {
int increment = 10 * m_percentrestrict;

for (int i = 0; i < 5; i++) {
CPPUNIT_ASSERT(map->put('a' + increment * i, i * 100));
map->put('a' + increment * i, i * 100);
}

for (int i = 0; i < 5; i++) {
Expand All @@ -88,15 +88,15 @@ class CoalescedHashMapTest : public CppUnit::TestFixture {
}

void testNullChar() {
CPPUNIT_ASSERT(map->put(0, 1000));
map->put(0, 1000);
assertValue(0, 1000);
}

void testOverflow() {
for (int i = 0; i < 10; i++) {
CPPUNIT_ASSERT(map->put('a' + i, i));
map->put('a' + i, i);
}
CPPUNIT_ASSERT(map->put('z', 100));
map->put('z', 100);
CPPUNIT_ASSERT(map->getSize() >= 11);

for (int i = 0; i < 10; i++) {
Expand All @@ -107,7 +107,7 @@ class CoalescedHashMapTest : public CppUnit::TestFixture {

void testAllCharacters() {
for (int i = 0; i < 255; i++) {
CPPUNIT_ASSERT(map->put(i, i + 100));
map->put(i, i + 100);
}

for (int i = 0; i < 255; i++) {
Expand All @@ -117,7 +117,7 @@ class CoalescedHashMapTest : public CppUnit::TestFixture {
CPPUNIT_ASSERT_EQUAL(256, map->getSize());

for (int i = 0; i < 255; i++) {
CPPUNIT_ASSERT(map->put(i, i + 200));
map->put(i, i + 200);
}

for (int i = 0; i < 255; i++) {
Expand Down

0 comments on commit 779bf6f

Please sign in to comment.