I’ve spent the last few weekends playing around with D and implementing a hash table by transcribing the code from jamesroutley/write-a-hash-table.
D has templates and several other facilities for meta-programming so I set out a few restrictions for myself while doing the transcription: no garbage collection, -betterC, and as much type safety as possible. Basically I wanted to use as many meta-programming facilities from D as possible to get a good intuition for them while at the same time avoiding any “high-level” facilities like the garbage collector and the D run time. On that front I think the exercise was a success. I came out learning some things about hash tables and some things about how D can be used to do low-level programming without having to deal with all the low-level drudgery of C.
Along the way I learned a few unexpected things about hash tables as well. I’m no longer convinced they’re such a great data structure. There were a lot of places I tripped up when implementing insert/delete/update functionality and the code has a bunch of TODOs about non-terminating loops and degenerate cases. I might revisit things at a later point and try to make things production ready but for the time being I’ve learned enough and will leave actual production-grade hash table implementations to the professionals.
Code lives here: davidk01/D-Finger-Exercises. Feel free to copy and adapt for your own learning needs.