Skip to content

Commit

Permalink
Update HashMap and HasherMap
Browse files Browse the repository at this point in the history
  • Loading branch information
cpurdy committed Sep 16, 2024
1 parent 90d5b19 commit 40c0209
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 70 deletions.
32 changes: 26 additions & 6 deletions lib_ecstasy/src/main/x/ecstasy/collections/HashSet.x
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import maps.CopyableMap;

/**
* An implementation of a Set that is backed by a HashMap.
*
* TODO variably mutable implementations to match HashMap
* An implementation of a [Set] that is backed by a [HashMap].
*/
class HashSet<Element extends Hashable>
extends MapSet<Element>
implements Replicable {
// ----- constructors --------------------------------------------------------------------------

/**
* Construct the HashSet with an (optional) initial capacity.
* Construct the `HashSet` with an (optional) initial capacity.
*
* @param initCapacity (optional) the number of expected element values
*/
Expand All @@ -17,9 +19,9 @@ class HashSet<Element extends Hashable>
}

/**
* Construct a HashSet that optionally contains an initial set of values.
* Construct a `HashSet` that contains the specified values.
*
* @param values initial values to store in the HashSet
* @param values initial values to store in the `HashSet`
*/
construct(Iterable<Element> values) {
if (values.is(HashSet<Element>)) {
Expand All @@ -37,4 +39,22 @@ class HashSet<Element extends Hashable>
construct(HashSet that) {
super(that);
}

/**
* Construct a `HashSet` that uses the specified [HashMap] for storage.
*
* @param map the [HashMap] to use for storage
*/
protected construct(HashMap<Element, Nullable> map) {
construct MapSet(map);
}

// ----- internal ------------------------------------------------------------------------------

@Override
protected HashSet ensureMapSet(CopyableMap<Element, Nullable> map) {
return &map == &contents
? this
: new HashSet(map.as(HashMap<Element, Nullable>));
}
}
27 changes: 24 additions & 3 deletions lib_ecstasy/src/main/x/ecstasy/collections/HasherSet.x
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import maps.CopyableMap;
import maps.HasherMap;

/**
* An implementation of a Set that is backed by a HasherMap.
*
* TODO variably mutable implementations to match HasherMap
*/
class HasherSet<Element>
extends MapSet<Element> {
// ----- constructors --------------------------------------------------------------------------

/**
* Construct the HasherSet with the specified hasher and (optional) initial capacity.
*
Expand Down Expand Up @@ -41,14 +42,34 @@ class HasherSet<Element>
* @param that another HasherSet to copy the contents from when constructing this HasherSet
*/
@Override
construct(HasherSet<Element> that) {
construct(HasherSet that) {
super(that);
}

/**
* Construct a `HasherSet` that uses the specified [HasherMap] for storage.
*
* @param map the [HasherMap] to use for storage
*/
protected construct(HasherMap<Element, Nullable> map) {
construct MapSet(map);
}

// ----- properties ----------------------------------------------------------------------------

/**
* The [Hasher] is used to hash and compare element values.
*/
Hasher<Element> hasher.get() {
return contents.as(HasherMap<Element, Nullable>).hasher;
}

// ----- internal ------------------------------------------------------------------------------

@Override
protected HasherSet ensureMapSet(CopyableMap<Element, Nullable> map) {
return &map == &contents
? this
: new HasherSet(map.as(HasherMap<Element, Nullable>));
}
}
2 changes: 1 addition & 1 deletion lib_ecstasy/src/main/x/ecstasy/collections/ListSet.x
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class ListSet<Element>
* @param that the `ListSet` object to duplicate from
*/
@Override
construct(ListSet<Element> that) {
construct(ListSet that) {
super(that);
}

Expand Down
6 changes: 3 additions & 3 deletions lib_ecstasy/src/main/x/ecstasy/maps/HashMap.x
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import collections.Hasher;

/**
* HashMap is a hashed implementation of the Map interface. It uses the natural [Hasher] of the
* `HashMap` is a hashed implementation of the [Map] interface. It uses the natural [Hasher] of the
* `Key` type for hashing and comparing keys.
*/
class HashMap<Key extends Hashable, Value>
Expand All @@ -11,7 +11,7 @@ class HashMap<Key extends Hashable, Value>
// ----- constructors --------------------------------------------------------------------------

/**
* Construct the HashMap with the specified hasher and (optional) initial capacity.
* Construct the `HashMap` with an (optional) initial capacity.
*
* @param initCapacity the number of expected entries
*/
Expand All @@ -37,7 +37,7 @@ class HashMap<Key extends Hashable, Value>
}

/**
* [HasherMap] virtual constructor: Construct the HashMap with the specified hasher and
* [HasherMap] virtual constructor: Construct the `HashMap` with the specified [Hasher] and
* (optional) initial capacity.
*
* @param hasher the [Hasher] to use
Expand Down
Loading

0 comments on commit 40c0209

Please sign in to comment.