Bgen is a powerful B-tree generator for C designed for high performance and versatility. With compile-time generation, type safety, and no dependencies, it allows developers to create customized in-memory collections with ease. From spatial B-trees to counted variations, Bgen is your go-to solution for efficient data management.
Bgen is an efficient B-tree generator for C, designed to create custom in-memory B-tree based collections with outstanding performance. With its lightweight, single-file header and no dependencies, Bgen allows for seamless integration into your C projects. Here are some of the standout features that make Bgen an excellent choice:
Key Features
- Compile-time generation using preprocessor templates ensures optimized performance.
- Type-safe generic data structure for robust code.
- Support for custom allocators, giving you full control over memory management.
- Iterators with both callback and loop-based approaches for flexible data handling.
- Utilizes copy-on-write functionality, enabling O(1) cloning of tree structures.
- Extensive range of customization options to cater to various data types, including specialized B-tree variations like Counted B-tree, Vector B-tree, and Spatial B-tree.
- Compatible with most C compilers (C99+) including Clang, GCC, and TCC, along with WebAssembly support via Emscripten.
- Thoroughly tested with 100% coverage, ensuring that your data is handled correctly and efficiently.
- Outstanding performance, making it suitable for high-load applications.
Customization Options
Bgen allows developers to set multiple options via the C preprocessor:
- BGEN_NAME: Specify a namespace for your B-tree.
- BGEN_TYPE: Define the data type for the items stored in the tree.
- BGEN_FANOUT: Set the maximum number of children per node.
- BGEN_COW: Enable copy-on-write support for efficient cloning.
- BGEN_ITEMCOPY and BGEN_ITEMFREE: Customize how items are copied or freed to handle complex data structures correctly.
Usage Examples
Simple B-tree
Here's a quick example of how to create a simple B-tree that stores integers:
#define BGEN_NAME bt // The namespace for the btree structure.
#define BGEN_TYPE int // The data type for all items in the btree
#define BGEN_LESS return a < b; // A code fragment for comparing items
#include "bgen.h" // Include "bgen.h" to generate the btree
int main() {
struct bt *tree = 0; // Create an empty btree instance.
bt_insert(&tree, 3, 0, 0);
bt_insert(&tree, 8, 0, 0);
bt_insert(&tree, 2, 0, 0);
bt_insert(&tree, 5, 0, 0);
// Iteration and manipulation code here...
}
Key-Value Map
Create a key-value map with string keys and integer values:
struct pair {
const char *key;
int value;
};
#define BGEN_NAME map
#define BGEN_TYPE struct pair
#define BGEN_COMPARE return strcmp(a.key, b.key);
#include "../bgen.h"
int main() {
struct map *map = 0; // Create a new map instance.
map_insert(&map, (struct pair){"GPU", 15}, 0, 0);
// More insertions and operations...
}
With various options available, you can further customize your B-trees to meet the specific needs of your application. For more detailed examples, please refer to the examples directory and check out the complete API reference.
Conclusion
Bgen provides exceptional performance, flexibility, and usability for developers looking to implement B-tree data structures in C. Whether you need efficient storage solutions for maps, sets, or even spatial indexing, Bgen is the go-to choice for building high-quality, performant software.