Skip to content

Commit

Permalink
Adding and testing for swap in map
Browse files Browse the repository at this point in the history
  • Loading branch information
Emmanuel Nyarko authored and Emmankoko committed Jun 7, 2024
1 parent 10959d8 commit 4eb9cac
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
20 changes: 11 additions & 9 deletions extras/AssociativeContainers/map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,24 @@ class Map
std::map<K, V> map;

public: // msvc mangles public and private static fields differently so need for public
static Map<K,V>* make() { return new Map<K, V>(); }
static Map<K,V>* make() { return new Map<K, V>(); }

size_t size() { return this->map.size(); }
size_t size() { return this->map.size(); }

V& operator[] (K const& key) { return this->map[key]; }
V& operator[] (K const& key) { return this->map[key]; }

V& at(K const& key) { return this->map.at(key); }
V& at(K const& key) { return this->map.at(key); }

bool empty() const noexcept { return this->map.empty(); }
bool empty() const noexcept { return this->map.empty(); }

size_t max_size() const { return this->map.max_size(); }
size_t max_size() const { return this->map.max_size(); }

void clear() noexcept { this->map.clear(); }
void clear() noexcept { this->map.clear(); }

size_t count(K const& key ) const { return this->map.count(key); }
size_t count(K const& key ) const { return this->map.count(key); }

bool contains(K const& key ) const { return this->map.contains(key);}
bool contains(K const& key ) const { return this->map.contains(key);}

void swap( Map* other) noexcept { this->map.swap(other->map); } // swap with the function parameter's map

};
2 changes: 1 addition & 1 deletion extras/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ template std::size_t stdcpp::test::cppSizeOf<std::vector<int> >();
template class std::set<int>;
template std::size_t stdcpp::test::cppSizeOf<std::set<int> >();

template class Map<int, char>;
template class Map<int, char>;
2 changes: 2 additions & 0 deletions source/stdcpp/map.d
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,6 @@ module stdcpp.map;
{
return this.contains(key);
}
///
void swap( Map* other) nothrow;
}
10 changes: 10 additions & 0 deletions source/stdcpp/test/map.d
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,14 @@ unittest
assert(mymap.size == 0);
assert(mymap.empty == 1);
assert(mymap.count(1) == 0); // container cleared
/* utilities to use to test after a swap */
mymap.opIndex(2) = 'b';
mymap.opIndex(3) = 'c';

/* testing for swap*/
auto mymap2 = Map!(int, char).make();
mymap2.swap(mymap);
assert(mymap2.size == 2);
assert(mymap2.at(2) == 'b');
assert(mymap2.at(3) == 'c'); // swap works!
}

0 comments on commit 4eb9cac

Please sign in to comment.