Skip to content

Commit

Permalink
adding and testing opIndex usage
Browse files Browse the repository at this point in the history
  • Loading branch information
Emmanuel Nyarko authored and Emmanuel Nyarko committed May 25, 2024
1 parent 254e5cc commit b14fee7
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
2 changes: 2 additions & 0 deletions extras/AssociativeContainers/map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ class Map
static Map<K,V>* make() { return new Map<K, V>(); }

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

V& operator[] (K const& key) { return this->map[key]; }
};
9 changes: 9 additions & 0 deletions source/stdcpp/map.d
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ module stdcpp.map;
}

static Map* make();

private void insertOrAssign(const ref Key, const ref Value);

size_t size();

ref Value opIndex( ref const Key key);

ref Value opIndex(const Key key)
{
return this.opIndex(key); // handle rvalue-ref
}
}
4 changes: 4 additions & 0 deletions source/stdcpp/test/map.d
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ unittest
auto mymap = Map!(int, char).make();
mymap.opIndexAssign(1, 'a');
assert(mymap.size == 1);
assert(mymap.opIndex(1) == 'a');
mymap.opIndex(2) = 'b';
mymap.opIndex(3) = 'c';
assert(mymap.size == 3);
}

0 comments on commit b14fee7

Please sign in to comment.