All Projects → hogliux → Semimap

hogliux / Semimap

Licence: mit
A semi compile-/run-time associative map container with compile-time lookup and run-time storage

Projects that are alternatives of or similar to Semimap

Containers
Containers backed by std.experimental.allocator
Stars: ✭ 111 (-66.87%)
Mutual labels:  hashmap, containers
C Macro Collections
Easy to use, header only, macro generated, generic and type-safe Data Structures in C
Stars: ✭ 192 (-42.69%)
Mutual labels:  hashmap, containers
Eternal
A C++14 compile-time/constexpr map and hash map with minimal binary footprint
Stars: ✭ 93 (-72.24%)
Mutual labels:  compile-time, hashmap
Kedge
Kedge : Simple, Concise & Declarative Kubernetes Applications
Stars: ✭ 300 (-10.45%)
Mutual labels:  containers
Kubernetes Kitap
Stars: ✭ 299 (-10.75%)
Mutual labels:  containers
Img
Standalone, daemon-less, unprivileged Dockerfile and OCI compatible container image builder.
Stars: ✭ 3,512 (+948.36%)
Mutual labels:  containers
Linuxdeploy
Install and run GNU/Linux on Android
Stars: ✭ 3,775 (+1026.87%)
Mutual labels:  containers
Rancher
Complete container management platform
Stars: ✭ 18,191 (+5330.15%)
Mutual labels:  containers
Kubectl Dig
Deep kubernetes visibility from the kubectl
Stars: ✭ 325 (-2.99%)
Mutual labels:  containers
Adapt
ReactJS for your infrastructure. Create and deploy full-stack apps to any infrastructure using the power of React.
Stars: ✭ 317 (-5.37%)
Mutual labels:  containers
Rustbreak
A simple, fast and easy to use self-contained single file storage for Rust
Stars: ✭ 315 (-5.97%)
Mutual labels:  hashmap
Pharos Cluster
Pharos - The Kubernetes Distribution
Stars: ✭ 302 (-9.85%)
Mutual labels:  containers
Bitnami Docker Redis
Bitnami Redis Docker Image
Stars: ✭ 317 (-5.37%)
Mutual labels:  containers
Crossplane
Your Universal Control Plane
Stars: ✭ 4,414 (+1217.61%)
Mutual labels:  containers
Cni
Container Network Interface - networking for Linux containers
Stars: ✭ 3,907 (+1066.27%)
Mutual labels:  containers
Terraform Ecs Fargate
A Terraform template used for provisioning web application stacks on AWS ECS Fargate
Stars: ✭ 293 (-12.54%)
Mutual labels:  containers
Trait Eval
We all know Rust's trait system is Turing complete, so tell me, why aren't we exploiting this???
Stars: ✭ 325 (-2.99%)
Mutual labels:  compile-time
My Links
Knowledge seeks no man
Stars: ✭ 311 (-7.16%)
Mutual labels:  containers
Shifter
Shifter - Linux Containers for HPC
Stars: ✭ 308 (-8.06%)
Mutual labels:  containers
Containerops
DevOps Orchestration Platform
Stars: ✭ 317 (-5.37%)
Mutual labels:  containers

semi::static_map and semi::map

This container was the topic of a cppcon talk:

https://www.youtube.com/watch?v=qNAbGpV1ZkU

(slides: https://goo.gl/igwVxD )

associative map containers with compile-time lookup!

Normally, associative containers require some runtime overhead when looking up their values from a key. However, when the key is known at compile-time (for example, when the key is a literal) then this run-time lookup could technically be avoided. This is exactly what the goal of semi::static_map and semi::map is.

In fact, when using semi::static_map and looking up a value with C++ literal as a key, then the value lookup is nearly as efficient as looking up a global variable (on x86/arm it will reduce to only three machine instructions: a cmp, jne and a direct load). As long as you use C++ literals as your keys, the computational time of the lookup will stay constant and, for example, will not increase with the number of keys in your container!

#include <iostream>
#include <string>

#include "semimap.h"

#define ID(x) []() constexpr { return x; }

int main()
{
  semi::map<std::string, std::string> map;

  // Using string literals to access the container is super fast:
  // computational complexity remains constant regardless of the number of key, value pairs!
  map.get(ID("food"))  = "pizza";
  map.get(ID("drink")) = "soda";
  std::cout << map.get(ID("drink")) << std::endl;


  // Values can also be looked-up with run-time keys
  // which will then use std::unordered_map as a fallback.
  std::string key;

  std::cin >> key;
  std::cout << map.get(key) << std::endl; // for example: outputs "soda" if key is "drink"

  // there is also a static version of the map where lookeup is even faster
  struct Tag {};
  using Map = semi::static_map<std::string, std::string, Tag>;

  // in fact, it is (nearly) as fast as looking up any plain old global variable
  Map::get(ID("food")) = "pizza";
  Map::get(ID("drink")) = "beer";
  
  return 0;
}

The containers are very simple and only have the methods get, erase, contains and clear. get can also take any number of extra optoinal parameters which will be passed to your value's constructor if the value is not already in the container. As such, get is very similar to std::map's try_emplace. For example:

#define ID(x) []() constexpr { return x; }

semi::map<std::string, std::string> m;

m.get(ID("food"), "pizza");      // a value with key food is not already in the map so construct it with the parameter "pizza"
m.get(ID("food"), "spaghetti");  // a value with key food is in the map. The second parameter will not be used

There are two variants of the map:

  1. semi::map behaves very similar to a normal associative container. It's methods are non-static, so that the key, value pairs are not shared between several instances of semi::maps (as one would expect).
  2. semi::static_map is completely static. It's even faster than semi::map. However, to achieve this speed, it requires that all the methods are static. This means that two semi::static_maps, with same key and value types, will share their contents. To avoid this, there is a third optional "tag" template parameter. Only semi::static_maps that also have the same tag template type will share their contents. It's useful to use a local struct as the tag type, like follows:
void foo()
{
  struct Tag {};
  using map = semi::static_map<std::string, int, Tag>;

  map::get(ID("age")) = 18;
}

As this ensures that even if the same struct name "Tag" is used in another block, the semi::static_maps will not share their contents.

Also note, that as a static type, the contents of the semi::static_map will only be deleted when the program is exited. If you need to delete your key/values sooner then use the clear method.

-Fabian

@hogliux

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].