GithubHelp home page GithubHelp logo

karel-burda / constexpr-hash-map Goto Github PK

View Code? Open in Web Editor NEW
9.0 1.0 2.0 68 KB

Compile-time single-header hash-map written in C++ 17

License: MIT License

C++ 100.00%
constexpr constexpr-constructors constexpr-context compile-time compile-time-meta-programming templates cpp cpp17 array constant hash-map hash-table header-only no-dependencies unordered-map

constexpr-hash-map's Introduction

Version Language License CI

Introduction

Simple single-header compile-time hash-map written in C++ 17.

Hash-map operates only in compile time and represents associative key-value container that contains only unique keys.

It works with custom data-types, if these provide constexpr and noexcept construction and operator=.

Container supports:

  • construction in one command
  • look-up
  • value retrieval
  • supports iterators (cend(), std::size(), ...)
  • algorithms (for-each, ...)

Implemented and documented in the constexpr_hash_map.hpp.

Behaviour is undefined, if there are multiple same keys.

Compatible and tested on:

  • x86-64 g++ 8.1 and higher
  • x86-64 clang++ 6.0 and higher
  • x64 MSVC v19.14 and higher

In case of bigger number of elements, compiler's settings regarding constexpr (such as -fconstexpr-depth on the GNU) might needed be tuned-up, as container uses compile-time recursion.

Example

#include <constexpr_hash_map/constexpr_hash_map.hpp>

// use arbitrary constexpr constructible types like std::string_view, etc. 
static constexpr burda::ct::hash_map<2, const char *, int> map
{
    std::make_pair("key1", 1),
    std::make_pair("key2", 2)
};

// capacity and iterators
static_assert(map.size() == 2);
static_assert(std::size(map) == 2);
static constexpr auto it = map.find("key1");
static_assert(it != std::cend(map));
static_assert(it->second == 1); // it->second holds the value
static_assert(!std::empty(map));

// accessors
static_assert(map.contains("key1"));
static_assert(!map.contains("key99"));
static_assert(map.at("key1").first); // first denotes, if element was found
static_assert(map.at("key1").second == 1); // second is the actual value
// operator[] doesn't check whether key exists
static_assert(map["key2"] == 2);
// this wouldn't compile -- the key doesn't exist and therefore cannot be evaluated to a constant
// expression, but outside static assertion it would compile and resulted in an undefined behaviour
//static_assert(map["key3"] == 3);

// algorithm support
for (const auto& [key, value] : map)
{
    // do something...
}

See also main.cpp.

Example might compiled (with no additional flags), for example, by this minimal command:

g++ main.cpp -I include -std=c++17

Live Demo

constexpr-hash-map's People

Contributors

karel-burda avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

constexpr-hash-map's Issues

What about a constructor from an existing std::array of key value pairs?

What do you think about adding an additional constructor for the case where one already has the data array created?

diff --git a/include/constexpr_hash_map/constexpr_hash_map.hpp b/include/constexpr_hash_map/constexpr_hash_map.hpp
index c1b9d50..68d740b 100644
--- a/include/constexpr_hash_map/constexpr_hash_map.hpp
+++ b/include/constexpr_hash_map/constexpr_hash_map.hpp
@@ -42,6 +42,15 @@ public:
         static_assert(N == sizeof...(elements), "Elements size doesn't match expected size of a hash-map");
     }
 
+    /// @brief The only other construction that might be used, array must be provided in the constructor.
+    /// @param std::array<std::pair<K,V>, N>, cannot be empty
+    explicit constexpr hash_map(data_type arr) noexcept
+    : data{std::move(arr)}
+    {
+        static_assert(N > 0, "N should be positive");
+        static_assert(N == data.size(), "Array size doesn't match expected size of a hash-map");
+    }
+
     /// @brief Searches map for a given key and returns iterator.
     /// @param key key to be searched for
     /// @return constant iterator to an element (cend, if not found)

Someone with more experience template programming may find a way to deduce the N, K and V parameters from the std::array itself.

Add documentation

Mention also constexpr depth of compilers.
Clang-tidy should have warnings as errors enabled.
Mention also that in C++20, implementation would be trivial due to constexpr algorithms.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.