3 ms·
As a hobby experiment I did something similar: ported generic containers and algorithms to C89. Unsurprisingly, what I found was that it was an uphill battle t
by dleslie 6y ago
As a hobby experiment I did something similar: ported generic containers and algorithms to C89.
Unsurprisingly, what I found was that it was an uphill battle to replicate behaviour and performance that would be found under a better type system.
Things like "aggregate values matching this predicate with this folding lambda into this target container" were clumsy as hell.
But I like your approach of abusing preprocessor definitions prior to including the container header. Neat!
https://github.com/dleslie/dl/tree/master/include https://github.com/dleslie/dl/tree/master/include
https://github.com/dleslie/dl/blob/master/src/test_dl_algorithms.c https://github.com/dleslie/dl/blob/master/src/test_dl_algori...
- glouwbug 6y agoThe alternative would be to rely on void* for all data types, which was really nicely presented in Mastering Algorithms with C by Kyle Loudon, but after working through it the lack of type safety for even basic types was a huge pain. At least with a bit of pre-processor magic, type safety and contiguous data is promised. Also, getting crafty with precompiled headers, one could dump all the expansions into a single templates.h, compile a .gch, and reap the benefits of hundreds of containered types with instantaneous compile times.
- dleslie 6y agoI went with void* in order to avoid having separate algorithms for every container.
- adrift 6y agoI'd use something like Sean Barrett's stretchy buffer [1] instead. [1] https://github.com/nothings/stb/blob/master/stretchy_buffer.h https://github.com/nothings/stb/blob/master/stretchy_buffer....