You are required to implement a hashtable between words and number of occurrences, using the D programming language. Your hashtable must provide the following interface:
Method | Description |
---|---|
this(size_t initialLength); | a constructor that takes the initial size of the hashtable |
size_t length(); | returns the size of the hashtable |
void add(string word); | add a new word to the hashtable, or increase the number of occurrences |
void remove(string word); | remove the word from the hashtable; the word doesn't necessarily have to exist |
size_t get (string word, size_t defaultValue = 0); | return the number of occurances for the given word, or defaultValue if the word doesn't exist in the hashtable |
void clear(); | clear the table. |
void resizeDouble(); | double the size of the hashtable |
void resizeHalve(); | halve the size of the hashtable. Excess memory must be freed. |
string bucketToString(size_t index_bucket); | returns a string containing all the pairs <word, numOcc>, separated by one space (” ”) character |
string toString(); | returns a string containing all the pairs <word, numOcc> in the hashtable, starting with index 0 |
The implemented hashtable must be named Hashtable and it will contain SIZE buckets. Each bucket will contain a pair consisting of the word and number of occurrences.
1. You are not allowed to use the built-in Associative Array type, nor other library implementations of a hashtable.
2. You are allowed to use any other features/built-in types that the language provides.
3. You must use the globally available `hashOf` function, as your hash function:
string phrase = "Bran got the Throne"; size_t hashValue = hashOf(phrase);
4. Inserting in a bucket list always adds at the end of the list.
5. When halving, if the size is odd (2k + 1), the new size must be k.
6. We encourage you to use unittest blocks to test your implementation.
You are required to write a small Linux Bash script, named `dmd_setup.sh`, that will setup the test environment. The script must do the following sequence of actions:
1. Clone the DLang repositories: dmd compiler, druntime, standard library (Phobos).
2. Build each component.
3. Do the necessary environmental setup such that the command `custdmd hashtable.d` will compile the file `hashtable.d` using the binaries obtained at step 2.
For detailed information about how to achieve the above, read the Starting as a contributor and Building under Posix guides.
1. The hashtable implementation must reside in a single file, named `hashtable.d`.
2. The environment setup script must be named `dmd_setup.sh`.
The assignment must be submitted as a `.zip` archive on VMChecker for the `DLang` course. The archive must have the following naming convention: firstName-lastName.zip. The archive will contain the `hashtable.d` and `dmd_setup.sh` files.
The final grade will consist of:
The use of idiomatic D code will be highly appreciated.
Make sure you are subscribed to forum. For any questions please use the forum.