C Collections

C Collections

The pieces that other languages put in their standard library and C doesn't have: Vectors, hash maps, ordered maps, strings, memory pools, thread-safe queues, a thread pool, a structured logger, JSON, YAML and an HTTP client and server. The compiler deduces your element types, with no void * casts.

A first program

A vector and a hash map, with the types deduced by the compiler and both released at the end of the scope.

#include <ccollections/chashmap.h>
#include <ccollections/cvector.h>
#include <stdio.h>

int main(void) {
  /* A vector of ints. */
  cvec_construct_scoped(scores, int); // Will be automatically destroyed
  cvec_push(scores, 91);
  cvec_push(scores, 74);
  cvec_push(scores, 88);

  // Using cvec_at for incremental writing
  cvec_at(scores, 0) += 2; // 91 -> 93

  // Sorting the cvector 'scores' in ascending order.
  cvec_sort(scores);

  for (size_t i = 0; i < cvec_size(scores); i++)
    printf("%d ", cvec_at(scores, i)); // Using cvec_at for reading
  printf("\n");                                   /* 74 88 93 */

  /* A hash-map from names to ints; the compiler tracks both the key type and
   * the value type. */
  chmap_construct_scoped(ages, char *, int); // Will be automatically destroyed
  chmap_insert(ages, "alice", 31);
  chmap_insert(ages, "bob", 27);

  printf("alice is %d\n", chmap_get(ages, "alice"));
  if (!chmap_get_ptr(ages, "carol")) {
    printf("no carol\n");
  }

  // In-place modification of an element
  *chmap_get_ptr(ages, "bob") += 2; // We know "bob" is present
  printf("bob is %d\n", chmap_get(ages, "bob"));

  // The cvector and the chashmap will automatically get
  // destroyed when the scope for the function main ends.
  return 0;
}

Build and install the library, then compile against it with pkg-config. The install puts the man pages in place too, so man cvec_push works right after it. It runs on Linux (glibc or musl), FreeBSD, OpenBSD and macOS, with GCC 10 or Clang 11 and later.

make
sudo make install
gcc -o hello hello.c $(pkg-config --cflags --libs ccollections)
./hello

You don't have to build what you won't use. JSON, YAML, the logger and the HTTP client and server are each one switch on the make command line, and turning one off also drops what it needs: OpenSSL goes with the two HTTP modules, zlib with the logger.

make WITH_CHTTPCLIENT=0 WITH_CHTTPSERVER=0     # no OpenSSL
make WITH_CJSON=0 WITH_CYAML=0 WITH_CLOGGER=0 \
     WITH_CHTTPCLIENT=0 WITH_CHTTPSERVER=0     # pthread and libm only

The HTTP modules log through the logger, so it stays in while either of them does. Leaving modules out covers the rest, such as installing a reduced build.

Modules

Each module has an overview page in the manual and a guide with complete examples.

cvector

A vector of any type, with sort and search

cstring

A growable string with find, replace, split and related operations

chashmap

A hash map with any key type and any value type

cbstmap

A map that keeps its keys in sorted order

citerators

One method to iterate over vectors and maps

csort

A stable sort for vectors and plain C arrays

cmempool

Fast fixed-size and ranged memory pools

cthreadcomm

Thread-safe queues, channels, select and an event loop

cthreadpool

A thread pool with futures and completion callbacks

clrucache

A thread-safe LRU cache, with an optional remote store

clogger

A structured logger: logfmt, JSON or syslog, with rotation and asynchronous writes

cjson

Parse, edit and write JSON

cyaml

Parse, edit and write YAML 1.2

chttpclient

An HTTP/1.1 client with TLS, keep-alive and three call styles

chttpserver

An HTTP/1.1 server with routes, middleware, streams and TLS

Documentation

  • ManualOne page for each public function and macro, with every parameter, return value and edge case.
  • Test coverageLine coverage of every source file, merged from all test suites on the main branch.
  • ChangelogWhat changed in each release.