2025-06-29 Sun 09:20 Data structures by heart
Fore some time now I found myself using couple of different abstract
data structures when programming in C, mainly:
1. Linked list
2. Doubly linked list
3. Ring buffer
4. Dynamic array
5. Rope
6. No hash table on this list [1]
But I always write them from scratch, tailored for given case. No
libraries, no copy and paste. I did that in recent reincarnation of
Yupa [2], during Advent Of Code 2024 [3] and in Dogo text editor [4].
This seems unreasonable. Using generic libraries for common data
structures is a no-brainer. Modern programming languages acknowledge
this by having some of them build-in not as standard library but as
language features. So what is the incentives for writing them from
scratch every single time?
Short answer: it's easier for me. It's natural to favor path of least
effort. And after few years of programming in C implementing data
structure on the spot become easier than finding or writing library
that solves my problem exactly as I need it.
For many choosing premade solution is easier. Those people might
justify they choice with "don't reinvent the wheel" and other similar
arguments but the ugly truth is that most of them lack knowledge and
skill; they are unable to make such data structures.
Solutions is a way of obfuscating knowledge
- Devine Lu Linvega
Besides it being easier, I choose to make abstract data structures
from scratch because:
1. I can customize them to specific case, maybe use dynamic memory
allocation, maybe in this case memory only grows, in other I might
know max number of items etc.
2. Implementation for specific case is less generic making it simpler.
3. API of such data structure is simpler.
4. Inner state of custom tailored version is smaller, use less memory.
5. Easier to debug, no black boxes in form of third party library.
6. Such approach makes need for templates/generics obsolete.
7. I avoid dependencies.
8. Faster build time and smaller binary.
Coming from dynamic scripting languages it might feel like dynamic
arrays and hash tables is all you need. Well, I think that they don't
rly have a choice to think otherwise. There is no possibility to go
low level and make different data structure in those languages. In
such languages programmers often adjust code to data structures they
have rather than making data structures that fit the problem. So even
with the skill and knowledge you sill have no choice but to use what
was given. Such environment don't help make good programmers.
I rest my case.
[1] I found hash tables very unnecessary and difficult to work with in
non scripting, non dynamic languages. I did entire Advent Of Code
2024 [3] in C without a single hash table.
[2]
https://github.com/ir33k/yupa
[3]
https://github.com/ir33k/aoc24
[4]
http://irek.gabr.pl/dogo-beta.html
EOF