9 ms·
A header-only C implementation of C++ <algorithm>
- KingLancelot 3y ago[dead]
- lelanthran 3y agoThis looks like a good implementation for common array operations in a type-safe manner. Not a bad approach, but the source (header file) is not very readable (so I guess "just like the STL" applies :-))
- aifusenno1 3y agoI found it fairly straightforward and readable compared to many libraries out there.
- tw1984 3y agoI feel sick everything time when I see C++ related stuff, simple & fun programming got so complicated just because some big name "committee members" who insist to spend 30 years to standardize networking/filesystem/reflection refuse to simplify their craps. yes, they haven't got that completed yet. I guess we can wait another 30 years and there will be a C implementation of the C++ reflection or networking?
- lelanthran 3y ago> I guess we can wait another 30 years and there will be a C implementation of the C++ reflection or networking? 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.
- deleted 3y ago[deleted]
- deleted 3y ago[deleted]
- cornel_io 3y ago"Stop trying to make 'Objective C' happen" ...is what all non-Apple programmers have been begging ever since the iPhone came out.
- lelanthran 3y agoFWIW, I'm a non-Apple developer: never wrote a single line of code for any Apple device for production. Ported some of my libraries over to Mac OS about a decade back, but that was it.
- wsc981 3y agoI thought (and still think) Objective-C is quite nice. Kinda hoping my employer wants to port (wrap) it’s C++ based library to Objective-C (and Swift) in the coming year. I’d be the primary guy doing the work and I’d enjoy it :)
- physicsguy 3y agoYou can always just use Boost. I don't think I've ever worked on a C++ project that didn't use it! std::filesystem = boost::filesystem - 1.30.0, 2003 std::optional = boost::optional - 1.30.0, 2003
- jb1991 3y agoKeep in mind that when boost features are eventually adopted into the C++ standard library, it is often only after their implementation has been strictly defined in ways that diverge from the boost implementations, which are often inefficient. Boost contains a lot of full-featured but inefficient code; for example, some Boost tools allow for arbitrary memory allocation which is often undesirable and their standard library versions do not allow for it. There are other design issues as well, such as how to consistently handle exceptions in edge cases, etc. Boost versions of some of these don't as comprehensively consider those situations.
- physicsguy 3y agoOf course, but aside from standardisation defined details, it also depends on your compiler's standard library implementation, and those are not all made equal, especially if you've got to build something both for Windows and Linux.
- jb1991 3y agoWell, your compiler's standard library implementation should follow the C++ standard, and if your code expects what the standard requires, in most cases you should be fine. The problem is when code is written with certain behavior in mind that is not in the standard because the developer has gotten used to a particular compiler's liberties that extend beyond the standard, and then if they port to another platform, complications can ensue.
- suby 3y agoThere is no reasonable solution to reflection. That we don't have an official solution here is an indictment of the C++ update process. Look to projects like Unreal Engine to see the horror and lengths that real projects have to go through to get around this.
- PhilipRoman 3y agoGod I wish everything was done in this way. Even the C standards committee moves too fast and adds features that have no place in the standard.
- tw1984 3y agoWell, there are people who choose to stay away from social and technical progress. I totally respect that desire.
- badpun 3y ago"Social progress" got us the holocaust and tens of millions killed by communism, though. It's perfectly reasonable to be wary of it.
- Maken 3y agoIt also ended serfdom. Being wary of change is usually the excuse to perpetuate awful systems in both technology and society.
- badpun 3y ago> Being wary of change is usually the excuse to perpetuate awful systems in both technology and society. Do you have any evidence to back that "usually" claim? I'm from a country that went through the horrors of malicious forced social change (under Communism), and most people who warm against it just don't want it ever to return.
- tw1984 3y ago> I'm from a country that went through the horrors of malicious forced social change (under Communism) I call communism cancer, let's be crystal clear about that first. Because of such permanent hate towards communism, I am deeply concerned as more and more countries are developing into de facto communism. Huge amount of $ were handed out to the public during COVID-19, in normal days you also see nonsenses like public housing and so called free health care which are actually covered by other hard working average tax payers - they are forced to cover other people's problem. In the end, many people got brainwashed to believe that there is a state that looks after everyone. Such typical communist way of thinking is horrible at best. I'd willing to bet that your country is just one of the above described one. You should be very concerned. Communism isn't dead, it just got itself a slightly different skin.
- pjmlp 3y agoAh, one of those "generic" libraries back from MS-DOS days.
- lelanthran 3y ago> Ah, one of those "generic" libraries back from MS-DOS days. Nothing about this screams "MS-DOS".[1] Why the disparagement? [1] I know, I was programming at that time.
- pjmlp 3y agoCommon C libraries in MS-DOS, before Borland C++ and Watcom C++ took over.
- lelanthran 3y ago> Common C libraries in MS-DOS, Honestly, never came across those in the C compilers I used back then.
- loeg 3y agopjmlp is a C++ fan who consistently criticizes C.
- downvotetruth 3y agoGit repo of user filters (https://news.ycombinator.com/item?id=33365189 https://news.ycombinator.com/item?id=33365189) seems inevitable
- pjmlp 3y agoAlready updated?
- deleted 3y ago[deleted]
- 3y ago
- lionkor 3y agoIsn't this a use-case for _Generic[1]? [1]: https://en.cppreference.com/w/c/language/generic https://en.cppreference.com/w/c/language/generic
- kzrdude 3y agoWith _Generic, the original library implementor has to accommodate your type. If it's a “template”, the user can insert their type. Also a type that's created long after the original library author has finished their work.
- iamyemeth 3y agoIt's also the case that _Generic is only available in C11 while this library targets C99
- lelanthran 3y ago_Generic is a little bit broken[1]. 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.
- 3y ago
- iamyemeth 3y agoI'm always a fan of 80% solutions implemented in C. I think the library is pretty nice, and as someone who's experimented with trying to add quality of life features to C99 with macros I think the implementation is actually pretty clean and readable as far as these kind of implementations go. 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
- kzrdude 3y agoI think the difference is that stb_ds is a datastructures library and this is for algorithms. This algorithm library will also look like a completely regular C library if you just use separate .h and .c files to instantiate declarations and the implementation, I think it's very clean.
- iamyemeth 3y agoI meant more from the perspective that stb_ds achieves type safe generics in C using macros but without using defines. It would be interesting to see if something similar could be achieved for an algorithm library.
- kzrdude 3y agoIt would be interesting but I prefer the approach in this post - because the templating macros are isolated out into a "module" and it does not great spread across the user code.
- rurban 3y agoWell, I do like mine better, which is closer to the STL, and for all containers. https://github.com/rurban/ctl/ https://github.com/rurban/ctl/
- l-albertovich 3y agoHonestly, who cares? 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.
- garganzol 3y ago> attempt to show off Why not? rurban's work is solid and should be shown off.
- l-albertovich 3y agoI think it's not polite, if you just want to show something cool you did you could create your own thread. You don't interrupt a street musicians and start playing in front of the crew just because you think you are a "better" artist or that your version of the song is cooler right? It's common courtesy. 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.
- garganzol 3y agoYour attitude may apply to some situations, but in general preliminary judgements kill innovation. Next time after reading your comment, someone somewhere may remain silent without showing off their art, and the world will be missing the next Copernicus, Gauss, Wozniak, Stroustrup. If I have to choose between "polite" and "innovation", I choose "innovation" any time of day. This is the same kind of difference as to "diss" someone for no real reason as opposed to letting someone just be and "blossom" so they could share their enlightenment with everyone, and thus benefit us all.
- smcameron 3y agoWhy header only? Jesus christ, we have a linker.
- clibraries 3y agoI assume you are not familiar with modern C, but I think it's a good opportunity to explain. 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.
- kevin_thibedeau 3y agoThat isn't modern C. It's the cool kids backporting a misfeature from C++ because that's what all the game devs do. Header only serves no purpose in C other than to demonstrate that you can't drive your tooling. Plus you've created a new burden to keep track of the magic define that activates the definitions in the code and the one file where it's invoked.
- deleted 3y ago[deleted]
- distcs 3y ago> you can't drive your tooling Can you suggest what tooling actually can help to implement algorithms and data structures libraries with generic types in a clean way?
- kevin_thibedeau 3y agoThe issue here is people too lazy to tell their compiler where to find a header file so instead they cook up a bespoke system that is specific to every library using this approach.
- typedef_struct 3y agoYou're hired
- tjalfi 3y agoNice work! Have you considering combining ARRAY_ALG_TYPE and ARRAY_ALG_PREFIX into a single function-like macro? ARRAY_ALG_TYPE_PREFIX(int, intv_), for example, can set up all the other macros. I would also recommend adding a ARRAY_ALG_ prefix to the NAME1, NAME2, NS, and T macros so that you don't redefine any previous macros with these names.
- eska 3y ago* STL algorithms typically operate on half-open ranges bounded by iterators [first, last). This convention is not used as often in C* 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.
- synergy20 3y agoI wonder what's the difference among clibraries, ctl verse the well known stb_ds.h at https://nothings.org/stb_ds/ https://nothings.org/stb_ds/ ? also, glib has lots of goodies and is cross platform too, it's somehow under used in my opinion.