Skip to content

ViralTaco/h

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

h

Switch on string constants!

Getting Started

Prerequisites

Installing

As a library:

Coming if anybody asks.

Adding it to your code:

Download h.h. Add it to you project. For example in a folder named include.

Include the header to your .cpp file containing you main function.

#include "include/h.h" // h::hash, h::operator ""_h
// ...

int main(int argc, char** argv) {
  switch (h::hash(argv[1])) {
    using namespace h::literals;
  case "foo"_h:
    // ...
  case "bar"_h:
    // ...
  }
}

Example

For examples check out examples.

Argument Parser Example

#include "lib/h.h" // h::hash, h::operator ""_h
#include <print>   // std::println

int main(int argc, char** argv) {
  using namespace h::literals;
  
  if (argc < 2) return EXIT_FAILURE;
  
  switch (h::hash(argv[1])) {
  case "--version"_h:
    std::println("com.viraltaco.h v{}", com_viraltaco_h_h);
    break;
  case "--help"_h:
  case "-h"_h:
    std::println("--help");
    break;
  default:
    std::println("Invalid input: '{}'", argv[1]);
    return EXIT_FAILURE;
  }
}

See examples/options.cxx
Run the CMake project run-options

Minimal Hash Map

#include "lib/h.h" // h::{ hash_t, hash }
#include <vector>  // std::vector

template <class T> struct HashMap {
  std::vector<h::hash_t> keys;
  std::vector<T> values;

  constexpr T& operator [](const std::string_view key_str) {
    const auto key = h::hash(key_str);
    auto idx = 0zu;
    for (const auto k: keys) {
      if (k == key) { return values[idx]; }
      ++idx;
    }
    keys.emplace_back(key);
    values.push_back(T{});
    return values[idx];
  }
};

int main() {
  HashMap<int> h{};
  h["LMAO"] = 0XDadBeef;
  return h["LMAO"] xor 0XDadBeef; // 0
}

See examples/hashmap.cxx
Run the CMake project run-hashmap

Running the tests

Run the CMake project run-tests.
Or compile and run ./tests/test.cxx.

Deployment

This library is provided as-is. Use at your own discretion.

Contributing

  1. Fork
  2. Code (if you add a feature don't forget to write tests for it)
  3. Test
  4. Add and commit (if possible: each file individually)
  5. Push to your remote
  6. Make a pull request on this repo.

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

Authors

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License - see MIT for more information.

Acknowledgments

About

Minimal (25 SLOC) Argument Parser Concept. Using DJB2 hash on std::string_view for blazing fast switching over string literals.

Topics

Resources

Stars

Watchers

Forks

Contributors