tape.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. //
  2. // tape.hpp
  3. // shared_random_generator
  4. //
  5. // Created by Sam Jaffe on 3/19/23.
  6. // Copyright © 2023 Sam Jaffe. All rights reserved.
  7. //
  8. #pragma once
  9. #include <string>
  10. #include <tuple>
  11. #include <variant>
  12. #include <vector>
  13. #include <random/device.h>
  14. namespace engine::random {
  15. class Tape : public Device {
  16. public:
  17. // Allow for the serialization and de-serialization of this Tape
  18. using serial_type =
  19. std::vector<std::tuple<std::string, uint64_t, uint64_t, uint64_t>>;
  20. private:
  21. template <typename T> struct EntryT {
  22. EntryT(T min, T max) : min(min), max(max), result() {}
  23. EntryT(T min, T max, T result) : min(min), max(max), result(result) {}
  24. EntryT(uint64_t min, uint64_t max, uint64_t result);
  25. T min;
  26. T max;
  27. T result;
  28. };
  29. using Entry = std::variant<EntryT<double>, EntryT<uint32_t>, EntryT<int32_t>>;
  30. private:
  31. size_t index_{0}; // transient
  32. std::vector<Entry> entries_;
  33. public:
  34. Tape() = default;
  35. Tape(serial_type serial);
  36. explicit operator serial_type() const;
  37. int32_t inclusive(int32_t min, int32_t max) override {
  38. return fetch(min, max);
  39. }
  40. uint32_t inclusive(uint32_t min, uint32_t max) override {
  41. return fetch(min, max);
  42. }
  43. double exclusive(double min, double max) override { return fetch(min, max); }
  44. void inclusive(int32_t min, int32_t max, int32_t result);
  45. void inclusive(uint32_t min, uint32_t max, uint32_t result);
  46. void exclusive(double min, double max, double result);
  47. private:
  48. template <typename T> T fetch(T min, T max);
  49. };
  50. }