Show HN: Convenient Containers – ergonomic generics in C

github.com

2 points by JacksonAllan 5 hours ago

Hello Hacker News :)

I'd like to share my C generic data-structure library Convenient Containers (CC). The library's main advantages are summarized in the Rationale section of its README.[1] In short, using some novel techniques, the library is able to provide a range of fully typesafe data structures with a generic API agnostic to both container type and data types, without requiring the user to make any boilerplate pre-declarations for every container/data type combination. In other words, CC containers look and function much like containers in languages with native support for generics:

  #include <stdio.h>
  #include "cc.h"
  
  int main( void )
  {
    vec( int ) our_vec;
    init( &our_vec );
    push( &our_vec, 5 );
    printf( "%d\n", *get( &our_vec, 0 ) );
    cleanup( &our_vec );
  
    map( int, float ) our_map;
    init( &our_map );
    insert( &our_map, 5, 0.5f );
    printf( "%f\n", *get( &our_map, 5 ) );
    cleanup( &our_map );
  }
CC is the only C data-structure library that offers such ergonomics, to the best of my knowledge.

Of course, performance is also important. To this end, CC's hash tables (i.e. its maps and sets) have performed well in benchmarks both by me[2] and by others.[3] Its red-black trees (i.e. its ordered maps and sets) have also been benchmarked and proven to perform on par with their C++ Standard Library counterparts.[4]

Some of the techniques upon which CC relies are explained briefly in its FAQ[5] and more thoroughly in a series of Reddit comments that I made back when the library was first released.[6] I am working on a series of articles to describe these techniques more systematically, the first of which I published earlier.[7]

Thanks for reading!

[1] https://github.com/JacksonAllan/CC#rationale

[2] https://jacksonallan.github.io/c_cpp_hash_tables_benchmark/

[3] https://gist.github.com/attractivechaos/6815764c213f38802227...

[4] https://github.com/JacksonAllan/CC/releases/tag/v1.3.0

[5] https://github.com/JacksonAllan/CC?tab=readme-ov-file#how-do...

[6] https://www.reddit.com/r/C_Programming/comments/zvubfb/comme...

[7] https://github.com/JacksonAllan/CC/blob/main/articles/Better...