-
-
Notifications
You must be signed in to change notification settings - Fork 411
structure: set #111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
structure: set #111
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be an interface the HashTable (which should perhaps be renamed to HashSet) implements rather than a class which delegates everything to the HashTable. Currently this is pretty much just useless boilerplate.
@appgurueu Is this what you mean? I found your comment a bit confusing |
Not quite. The interface is correct, but you should have simply renamed the |
But a hash table and a hash set are two different things, or am I wrong? A hash set stores a set of single values (e. g. "{1, 2, 3, ...}"). A hash table stores a set of key-pair values (e. g. "{a -> 1, b -> 2, c -> 3, ...}"). So therefore, renaming the hash table to a hash set would change the meaning of it. Feel free to correct me, if I misunderstood something. |
Ye, i guess. But implementing a Set based on a HashTable is pretty much the same except ignoring the value and just using the key. As @appgurueu said, it is pretty much just useless boilerplate. I could implement the Set based on a LinkedList, so that it really shows some extra logic. Currently, the HashTable is handling everything itself. |
Yes, you're right! Sorry, I glossed this over and mostly saw what looked like boilerplate... I'm used to implementing it the other way around (implementing a "map" as a special "set" which requires the "hack" of Your initial changes were fine. Perhaps somewhat cleaner would be a general abstraction to convert a map into a set. If you want to, you can implement the following:
|
Shouldn't the Set only have one generic? Because there is no key-value pair stored. |
Yes of course, sorry again. Fixed. |
btw something offtopic. Maybe it would make sense to group some of the data structures because there are multiple implementations and sorts of one data structure. |
Yes, multiple implementations of the same data structure should be grouped in a common package. Feel free to refactor this. |
Alrighty, I implemented your suggestion! I still got some questions:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Set
and Map
should still have their own files, since they aren't necessarily tied to their Hash* implementations (in general, it is considered good practice to have one interface / class per file). The Set
interface doesn't need a get
method, has
suffices.
The abstract MapSet<K>
class should not have a HashMap<K, null>
type, but rather any generic Map<K, null>
. It should also have an abstract method protected abstract Map<K, null> initMap();
to be overridden. The constructor should then simply be map = initMap()
. The HashMapSet<K>
class then boils down to Map<K, null> initMap() { return new HashMap<K, null>(); }
(this is the Template Method Pattern; TS doesn't allow abstract constructors).
Alternatively, you could make MapSet<K>
concrete and have it take the map
as a parameter. The HashMapSet<K>
would then boil down to constructor() { super(new HashMap<K, null>()); }
.
Alright, what about my questions in the previous comment? |
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your contribution!
No problem, it helps me improving too :) |
Since this data structure is just using the methods of the hash table, I thought that extra testing is not relevant. Correct me if I'm wrong