tape.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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<bool, std::string, double, double, double>>;
  20. private:
  21. enum class Type : char;
  22. template <typename T> struct EntryT {
  23. EntryT(T min, T max) : min(min), max(max), result() {}
  24. EntryT(T min, T max, T result) : min(min), max(max), result(result) {}
  25. T min;
  26. T max;
  27. T result;
  28. };
  29. struct Entry {
  30. template <typename T>
  31. Entry(Type type, EntryT<T> params) : type(type), params(params) {}
  32. Type type;
  33. std::variant<EntryT<double>, EntryT<unsigned int>> params;
  34. };
  35. private:
  36. size_t index_{0}; // transient
  37. std::vector<Entry> entries_;
  38. public:
  39. Tape() = default;
  40. Tape(serial_type serial);
  41. explicit operator serial_type() const;
  42. unsigned int exclusive(unsigned int max) override;
  43. double exclusive(double min, double max) override;
  44. double inclusive(double min, double max) override;
  45. void exclusive(unsigned int max, unsigned int result);
  46. void exclusive(double min, double max, double result);
  47. void inclusive(double min, double max, double result);
  48. private:
  49. template <typename T> T fetch(Type type, T min, T max);
  50. };
  51. }