9 ms·
I backported a handful of C++ STL containers to ISO C99 to exemplify that a subset of C++, namely C with Templates, is a highly usable type-safe feature within
by glouwbug 6y ago
I backported a handful of C++ STL containers to ISO C99 to exemplify that a subset of C++, namely C with Templates, is a highly usable type-safe feature within the realm of ISO C99.
Included are a couple examples, like a JSON parser and a simple 6502 compiler. I have also written a small wiki, that showcases how to use the included containers: https://github.com/glouw/ctl/wiki https://github.com/glouw/ctl/wiki
- baybal2 6y agoWhat do you think about Glib?
- ku-man 6y agoGreat work! And thank you for making it available for everyone. Did you also run tests against other libraries such as glib?
- nitrogen 6y agoThis is cool! I've seen this C-with-templates used in the past with ".X" headers but haven't used the technique myself. Having something based on STL might help people to center on one library instead of constantly reinventing collections in C (which I've also done for some structures, and open sourced though I don't recommend using it: https://github.com/nitrogenlogic/nlutils https://github.com/nitrogenlogic/nlutils).
- ummonk 6y agoI recall a few years back doing something like this implementing a binary heap just as a hobby project (https://github.com/Equationist/ctl/blob/master/pqueue.h https://github.com/Equationist/ctl/blob/master/pqueue.h). I was curious to see how unoptimized it was vis a vis the STL, and ran compiled equivalent test code for both my priority queue and the STL's C++ implementation, with just integers. I was kind of shocked to find that my implementation was like 30% faster when both were compiled at -O3 levels. Seeing this backport I guess there isn't anything fancy in the STL implementations, so it's less surprising that my naive implementation could be just as fast (or happen to be faster).
- glouwbug 6y agoThere's this theory that humans are antennas, and that we pick up "wavelengths from above" to build and create the world we live in. Not only did we decide on the same project name and idea, but we also used the same pre-processor hacks. How surreal. As for your pqueue implementation, the efficacy of an optimizing compiler lies in the inherent complexity of the language. I can imagine the optimization backend for gcc is simpler (and more effective) than g++, but I am not an expert on compiler internals.
- wwweston 6y agoWhere have you heard that theory? I've wondered about it myself, but other than some songwriter-specific conversation, I'm not sure I'm familiar with other people talking about it, and I'd like to be.
- glouwbug 6y agoI'm not sure where I heard it, but if you are interested in an article: https://www.sciencedaily.com/releases/2014/01/140116085105.htm https://www.sciencedaily.com/releases/2014/01/140116085105.h...
- joshuaissac 6y agoIt sounds somewhat similar to Sheldrake's morphic resonance ideas.
- ncmncm 6y agoIn other words, it's 100% BS; in Sheldrake's case, wholly contradicted by his own examples.
- pwdisswordfish5 6y agogcc and g++ use the same backend.
- kzrdude 6y agoThese kinds of macro hacks are pretty widespread, aren't they? To me, klib and its khash hash table is the most well-known implementation, see: https://github.com/attractivechaos/klib https://github.com/attractivechaos/klib And I've written similar things myself (hash table and vectors).
- panta 6y agoThis is awesome! Very useful for writing highly portable software in C. I think I’ll use this library also with my students.
- woadwarrior01 6y agoThis along with Cosmopolitan libc which was recently posted here[1], would be awesome for writing tiny cross platform utilities in C. [1]: https://news.ycombinator.com/item?id=25556286 https://news.ycombinator.com/item?id=25556286
- pjmlp 6y agoWell, that is how BIDS 1.0 was designed, before C++ got templates.
- higherhalf 6y agoAre they fuzzed? Should be useful as some guarantee against memory-safety bugs. Didn't find this mentioned in the project.
- glouwbug 6y agoI have a series of functional tests in `tests/func` which randomly call container functions and check their output against the C++ STL. These tests run on every push, on all optimization settings, with and without gcc's address sanitizer.
- apankrat 6y agoNice, very nice. All of it. Long live C. Being stuck with C++ I did something in reverse - ported C-style ("intrusive") containers to ++, making them a bit safer to use, but keeping the syntax nearly the same. https://github.com/apankrat/notes/tree/master/intrusive-containers https://github.com/apankrat/notes/tree/master/intrusive-cont...
- einpoklum 6y agoThe Boost libraries have an intrusive containers library, described here: https://www.boost.org/doc/libs/1_75_0/doc/html/intrusive.html https://www.boost.org/doc/libs/1_75_0/doc/html/intrusive.htm... Have you considered it? Is it deficient for your use case?
- apankrat 6y agoThe post I linked to mentions it explicitly.
- gpderetta 6y agobut it doesn't mention why not to use it.
- apankrat 6y agoIt does, in the exact same sentence.
- gpderetta 6y agoYou mean this: "the goal here is to make a better version of C-style containers rather than to implement something C++-style and similar,"? But a goal is not a reason. I.e. why doing CONTAINER_OF( user_data, vip ); instead of container_of<&user_data::vip> vip_list; is preferable? How are they even meaningfully different?
- 6y ago
- tom_mellior 6y agoThis looks cool, but I dislike the naming. Calling a queue "queue" would be so much more readable than "que", and so on. I think the everything-is-three-letters gimmick is overall a drawback. Also, would it really hurt that much to write "#define PRIMITIVE" instead of "#define P"?
- jiggawatts 6y agoI've found that in forums like this, people generally react badly to comments about naming, but I've found that it actually matters more than people think it does. Many things have exactly one correctly spelled full name, but many alternative contractions or cute variations. Programmers can be expected to know the correct spelling of "vector" or "dequeue". They can't be expected to know your specific contraction or abbreviation of it. They can't guess and they're not mind readers. They have a muscle memory that has them type out the full word automatically, without thinking. Don't break these expectations.
- sgerenser 6y agoI mostly agree with this comment except “dequeue” is a bad example. C++ has called this data structure “deque” (pronounced deck) since the beginning of the STL. Is there another language/library that spells it dequeue?
- IncRnd 6y ago> Is there another language/library that spells it dequeue? Yes. https://www.npmjs.com/package/dequeue https://api.jquery.com/dequeue/ https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.queue-1.dequeue?view=net-5.0
- DaiPlusPlus 6y ago.NET uses Dequeue as a verb, not a noun though.
- 6y ago
- matvore 6y agoDid you consider using more traditional macros like VEC_ADD which works for all types rather than generating e.g. vec_<type>_add? The latter approach doesn't work with cscope and ctags, which is a pretty big drawback in my book (I like it when my super trivial IDE configuration works, which is only possible with C). See the comment and following 4 macros for how this would work with a (add-only, not growable) hashmap: https://github.com/matvore/nusort/blob/master/src/util.h#L166 https://github.com/matvore/nusort/blob/master/src/util.h#L16...