163 points · by clibraries · 3y ago · 85 comments
That being said, I think the ergonomics of the approach taken by Sean Barrett in stb_ds is much nicer than the #define ALG_TYPE, #define ALG_PREFIX approach, although I'm sure it wouldn't be a big stretch to make something similar that was compatible with it.
My feeling is that you could get pretty far with something like Sean Barrett's stb_ds + Salvatore Sanfilippo's sds and then a generic algorithm library like this one
It would be interesting to see if something similar could be achieved for an algorithm library.
Not a bad approach, but the source (header file) is not very readable (so I guess "just like the STL" applies :-))
1. You don't implement the most important algorithms. For example `stable_sort` for arrays. So right there, it's not even a substitute.
One of my motivations from the start was simply to have `stable_sort` in C (to implement database queries).
2. Yours is a clever implementation of C++ features using macros. But it's not idiomatic C. If someone (or their team) wants to use your system, they must learn your macro language.
My file can be included by someone who only knows C array, and will not intrude on any aspect of their C programming style.
3. Mine is intentionally not a data structure library, and avoids making allocations.
There are countless data structure libraries for C. Very few catch on. That's because C programmers tend to manage the life cycle of data themselves. Furthermore, data structures are inter-woven with their data, as opposed to being separate containers.
Besides that point, I see no advantage to yours over other data structure libraries. For example, the best C hash map implementations are written for uint64 keys. Fixing to a concrete type allows for tremendous optimization.
I applaud the efforts in your project and wish it success. Our projects happen to draw on similar source material, but that's about it.
Your variant being static is indeed nice, the STL, CTL cannot do that, just MTL exists for that. Which supports all containers. E.g. Non-vector types allow for much faster inserts.
You can only replace insitu, which is the opposite of functional. No backtracking. Manual copying needed. The spirit of the STL algorithms is to allocate, not overwrite.
Re hash-map: this is not even finished. For now it's just a pointer-stable unordered_map with optimizations for all types, primitives or strings. optimized Swisstable (string) and Stanford hashes (u64) are in work (no time for that), and are indeed multiple times faster.
Yes.
> Your name "array" clashes with the C++ array
In C they are called arrays.
> And your array is unsafe sized, as last is the only size provision, even unsafer than C++ with its unsafe iterators.
Can you explain more? C++ <algorithm> is intended to be used for C pointers as well. The iterator concept is an abstraction of pointer.
> You can only replace insitu
I'm not sure what you mean. The replace/remove/unique work just like the C++ versions. The STL recognizes there are cases when you want to work in-place, and others when you want to use a separate buffer. That's why it provides multiple versions.
> the spirit of the STL algorithms is to allocate, not overwrite.
I don't believe this is true. It's designed to separate memory allocation concerns from the algorithm. That's why a lot of them require you to provide a buffer satisfying some requirements.
On a more constructive side, I think adding some value by giving the poster some feedback or engaging in a conversation about the topic would be nice, otherwise it just sounds as an attempt to show off.
I'm not a fan of these "header only" projects nor am I a fan of overly clever code because even though it's fun and rewarding to write it's not a great fit for larger or collaborative projects where consistently adhering to a coding style (hopefully one of the secure ones) and favoring readability over "coolness" are the way to go.
People love to blame C for all of its footguns but not a lot of people like to write "boring" code which kinda sucks...
Please don't take this personally, even though I have to admit that I felt somewhat irritated upon reading your comment it is my no means my intention to attack you with this rant.
Why not? rurban's work is solid and should be shown off.
On a side note, I looked at both codebases and even though I've already said that I wouldn't use either of them I have to say that I find rurbans to be somewhat messier and uglier so I have to disagree with you.
it's only common for normies (ok, I guess that is a lot) to expend too much energy looking for emotional content. Just pay attention to the facts, here's a library/framework, here's another library/framework. See? now you know about two. Want emotion? ok, one guy seems proud of his, another guy maybe too proud of his? So what, you be the judge, look at the code.
that is very emotional of your part using borderline insults
You don't need to spend any energy looking for emotional content when it's very face front ego inflated "mine is better" without any constructive feedback.
I don’t think this is true. p != end is a very common check in C?
Regarding composition, I suppose it’s kind of true. The string functions in the C standard library for example unfortunately often return null instead of end. I use functions that return end instead, which makes C code much more elegant.
It's a lot like some of the other decisions by the standards committee; they see demand for a particular feature $FOO, then add in support for something not quite like $FOO, which maybe addresses part of what was wanted but turns out useless in practice so that few use it.
They added `const` (which, admittedly, does get a lot of use in practice) which is subtly different from what already existed in C++ at the time.
They added VLAs, then had to walk that back because, like `gets()`, there was no practical way to use it safely.
The added _Generic, which requires the typenames to be known in advance.
They added threads, but without wide enough functionality to use it for anything but the most trivial of programs.
All in all, they've been doing a poor job of steering the language in the last 20-odd years since C99. What users have been clamouring for was "less UB, please? Make it IB where you can", but instead they have been adding UB.
In the words of Linus Torvalds: "Yeah, let's just say that the original C designers were better at their job than a gaggle of standards people who were making bad crap up to make some Fortran-style programs go faster."
Original K&R C had no reference to undefined behaviour. The worst you could say about C at that point in time was that some things were defined by the implementation.
[1] Some might say more than a little bit :-/ You can't use it to write type-safe generic libraries.
You don't have to explicitly refer to undefined behavior to have it. If you just merely omit defining the behavior of certain programs then that's enough.
I respectfully disagree. It can be implementation defined, as in all other languages.
In fact, when I started with C, I did not use a C89 one, if we weren't certain what a certain construct did, we;d try it on a compiler and use the result as a definition for what that construct did on that compiler!
On another compiler it might do something else, but that didn't bother us until it was time to port the code to another compiler.
With undefined behaviour being added, that no longer holds: you try something out, get a particular result, but the next time you compile that construct you could get a completely different result.
also, glib has lots of goodies and is cross platform too, it's somehow under used in my opinion.
A single header style allows you to customize the library with the preprocessor before `#include`. I use this ability to implement generics. Otherwise, you're stuck with void* or code generation (want my python script instead?).
However, you don't have to forgo the benefits of separate compilation units. You can include the declarations in a header, and the implementation in a separate C file. No other parts of the code base will be impacted.
Apparently few can. How many image libraries came before `stb_image`? KISS is a feature.
Also, tooling changes between platforms and build systems.
Can you suggest what tooling actually can help to implement algorithms and data structures libraries with generic types in a clean way?
Nothing about this screams "MS-DOS".[1]
Why the disparagement?
[1] I know, I was programming at that time.
pjmlp is a security fan who consistently criticizes C.
Honestly, never came across those in the C compilers I used back then.
I guess we can wait another 30 years and there will be a C implementation of the C++ reflection or networking?
And I'm not saying there isn't room for improvement. There are lots of genuinely useful features C is missing, especially regarding static analysis. I would love to have a proof assistant that can integrate with a C/C++ compiler to prove equivalence of functions, so you could write an obvious version and then transform it step by step to an optimized version, which is guaranteed to have the same observable behaviour.
std::filesystem = boost::filesystem - 1.30.0, 2003
std::optional = boost::optional - 1.30.0, 2003
You don't really need to wait if you're willing to use Objective C with the GnuStep framework.
You'll get reflection, and a lot of other nice addons[1].
[1] Dunno about network support; ISTR it being part of the GnuStep framework, but could be wrong.
...is what all non-Apple programmers have been begging ever since the iPhone came out.