Browse Source

Adding in files

Sam Jaffe 7 years ago
parent
commit
9b846db479

+ 25 - 0
include/shared_random_generator/random.h

@@ -0,0 +1,25 @@
+#pragma once
+
+#include <memory>
+
+namespace engine {
+  namespace detail {
+    struct random_impl;
+  }
+
+  class random_number_generator {
+  public:
+    random_number_generator();
+    template <typename Provider>
+    random_number_generator(Provider const & p)
+        : p_impl(std::make_shared<Provider>(p)) {}
+    template <typename Provider>
+    random_number_generator(std::shared_ptr<Provider> const & p) : p_impl(p) {}
+    unsigned int exclusive(unsigned int max) const;
+    double exclusive(double min, double max) const;
+    double inclusive(double min, double max) const;
+
+  private:
+    std::shared_ptr<detail::random_impl> p_impl;
+  };
+}

+ 17 - 0
include/shared_random_generator/random_impl.h

@@ -0,0 +1,17 @@
+//
+//  random_impl.h
+//  pokemon
+//
+//  Created by Sam Jaffe on 12/9/17.
+//
+
+#pragma once
+
+namespace engine { namespace detail {
+  struct random_impl {
+    virtual ~random_impl() = default;
+    virtual unsigned int exclusive(unsigned int max) = 0;
+    virtual double exclusive(double min, double max) = 0;
+    virtual double inclusive(double min, double max) = 0;
+  };
+}}

+ 299 - 0
shared_random_generator.xcodeproj/project.pbxproj

@@ -0,0 +1,299 @@
+// !$*UTF8*$!
+{
+	archiveVersion = 1;
+	classes = {
+	};
+	objectVersion = 50;
+	objects = {
+
+/* Begin PBXBuildFile section */
+		CDED6A4F21B2F62C00AB91D0 /* random.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CDED6A4E21B2F62C00AB91D0 /* random.cxx */; };
+		CDED6A5E21B2F76B00AB91D0 /* shared_random_generator in Headers */ = {isa = PBXBuildFile; fileRef = CDED6A5D21B2F76700AB91D0 /* shared_random_generator */; settings = {ATTRIBUTES = (Public, ); }; };
+/* End PBXBuildFile section */
+
+/* Begin PBXFileReference section */
+		CDED6A4221B2F5A700AB91D0 /* libshared_random_generator.dylib */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.dylib"; includeInIndex = 0; path = libshared_random_generator.dylib; sourceTree = BUILT_PRODUCTS_DIR; };
+		CDED6A4E21B2F62C00AB91D0 /* random.cxx */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = random.cxx; sourceTree = "<group>"; };
+		CDED6A5D21B2F76700AB91D0 /* shared_random_generator */ = {isa = PBXFileReference; lastKnownFileType = folder; name = shared_random_generator; path = include/shared_random_generator; sourceTree = "<group>"; };
+/* End PBXFileReference section */
+
+/* Begin PBXFrameworksBuildPhase section */
+		CDED6A4021B2F5A700AB91D0 /* Frameworks */ = {
+			isa = PBXFrameworksBuildPhase;
+			buildActionMask = 2147483647;
+			files = (
+			);
+			runOnlyForDeploymentPostprocessing = 0;
+		};
+/* End PBXFrameworksBuildPhase section */
+
+/* Begin PBXGroup section */
+		CDED6A3921B2F5A700AB91D0 = {
+			isa = PBXGroup;
+			children = (
+				CDED6A5D21B2F76700AB91D0 /* shared_random_generator */,
+				CDED6A4921B2F5DF00AB91D0 /* src */,
+				CDED6A4A21B2F5DF00AB91D0 /* test */,
+				CDED6A4321B2F5A700AB91D0 /* Products */,
+			);
+			sourceTree = "<group>";
+		};
+		CDED6A4321B2F5A700AB91D0 /* Products */ = {
+			isa = PBXGroup;
+			children = (
+				CDED6A4221B2F5A700AB91D0 /* libshared_random_generator.dylib */,
+			);
+			name = Products;
+			sourceTree = "<group>";
+		};
+		CDED6A4921B2F5DF00AB91D0 /* src */ = {
+			isa = PBXGroup;
+			children = (
+				CDED6A4E21B2F62C00AB91D0 /* random.cxx */,
+			);
+			path = src;
+			sourceTree = "<group>";
+		};
+		CDED6A4A21B2F5DF00AB91D0 /* test */ = {
+			isa = PBXGroup;
+			children = (
+			);
+			path = test;
+			sourceTree = "<group>";
+		};
+/* End PBXGroup section */
+
+/* Begin PBXHeadersBuildPhase section */
+		CDED6A3E21B2F5A700AB91D0 /* Headers */ = {
+			isa = PBXHeadersBuildPhase;
+			buildActionMask = 2147483647;
+			files = (
+				CDED6A5E21B2F76B00AB91D0 /* shared_random_generator in Headers */,
+			);
+			runOnlyForDeploymentPostprocessing = 0;
+		};
+/* End PBXHeadersBuildPhase section */
+
+/* Begin PBXNativeTarget section */
+		CDED6A4121B2F5A700AB91D0 /* shared_random_generator */ = {
+			isa = PBXNativeTarget;
+			buildConfigurationList = CDED6A4621B2F5A700AB91D0 /* Build configuration list for PBXNativeTarget "shared_random_generator" */;
+			buildPhases = (
+				CDED6A3E21B2F5A700AB91D0 /* Headers */,
+				CDED6A3F21B2F5A700AB91D0 /* Sources */,
+				CDED6A4021B2F5A700AB91D0 /* Frameworks */,
+			);
+			buildRules = (
+			);
+			dependencies = (
+			);
+			name = shared_random_generator;
+			productName = shared_random_generator;
+			productReference = CDED6A4221B2F5A700AB91D0 /* libshared_random_generator.dylib */;
+			productType = "com.apple.product-type.library.dynamic";
+		};
+/* End PBXNativeTarget section */
+
+/* Begin PBXProject section */
+		CDED6A3A21B2F5A700AB91D0 /* Project object */ = {
+			isa = PBXProject;
+			attributes = {
+				LastUpgradeCheck = 1010;
+				ORGANIZATIONNAME = "Sam Jaffe";
+				TargetAttributes = {
+					CDED6A4121B2F5A700AB91D0 = {
+						CreatedOnToolsVersion = 10.1;
+					};
+				};
+			};
+			buildConfigurationList = CDED6A3D21B2F5A700AB91D0 /* Build configuration list for PBXProject "shared_random_generator" */;
+			compatibilityVersion = "Xcode 9.3";
+			developmentRegion = en;
+			hasScannedForEncodings = 0;
+			knownRegions = (
+				en,
+			);
+			mainGroup = CDED6A3921B2F5A700AB91D0;
+			productRefGroup = CDED6A4321B2F5A700AB91D0 /* Products */;
+			projectDirPath = "";
+			projectRoot = "";
+			targets = (
+				CDED6A4121B2F5A700AB91D0 /* shared_random_generator */,
+			);
+		};
+/* End PBXProject section */
+
+/* Begin PBXSourcesBuildPhase section */
+		CDED6A3F21B2F5A700AB91D0 /* Sources */ = {
+			isa = PBXSourcesBuildPhase;
+			buildActionMask = 2147483647;
+			files = (
+				CDED6A4F21B2F62C00AB91D0 /* random.cxx in Sources */,
+			);
+			runOnlyForDeploymentPostprocessing = 0;
+		};
+/* End PBXSourcesBuildPhase section */
+
+/* Begin XCBuildConfiguration section */
+		CDED6A4421B2F5A700AB91D0 /* Debug */ = {
+			isa = XCBuildConfiguration;
+			buildSettings = {
+				ALWAYS_SEARCH_USER_PATHS = NO;
+				CLANG_ANALYZER_NONNULL = YES;
+				CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
+				CLANG_CXX_LANGUAGE_STANDARD = "gnu++14";
+				CLANG_CXX_LIBRARY = "libc++";
+				CLANG_ENABLE_MODULES = YES;
+				CLANG_ENABLE_OBJC_ARC = YES;
+				CLANG_ENABLE_OBJC_WEAK = YES;
+				CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
+				CLANG_WARN_BOOL_CONVERSION = YES;
+				CLANG_WARN_COMMA = YES;
+				CLANG_WARN_CONSTANT_CONVERSION = YES;
+				CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
+				CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
+				CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
+				CLANG_WARN_EMPTY_BODY = YES;
+				CLANG_WARN_ENUM_CONVERSION = YES;
+				CLANG_WARN_INFINITE_RECURSION = YES;
+				CLANG_WARN_INT_CONVERSION = YES;
+				CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
+				CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
+				CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
+				CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
+				CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
+				CLANG_WARN_STRICT_PROTOTYPES = YES;
+				CLANG_WARN_SUSPICIOUS_MOVE = YES;
+				CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
+				CLANG_WARN_UNREACHABLE_CODE = YES;
+				CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
+				CODE_SIGN_IDENTITY = "-";
+				COPY_PHASE_STRIP = NO;
+				DEBUG_INFORMATION_FORMAT = dwarf;
+				ENABLE_STRICT_OBJC_MSGSEND = YES;
+				ENABLE_TESTABILITY = YES;
+				GCC_C_LANGUAGE_STANDARD = gnu11;
+				GCC_DYNAMIC_NO_PIC = NO;
+				GCC_NO_COMMON_BLOCKS = YES;
+				GCC_OPTIMIZATION_LEVEL = 0;
+				GCC_PREPROCESSOR_DEFINITIONS = (
+					"DEBUG=1",
+					"$(inherited)",
+				);
+				GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
+				GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
+				GCC_WARN_UNDECLARED_SELECTOR = YES;
+				GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
+				GCC_WARN_UNUSED_FUNCTION = YES;
+				GCC_WARN_UNUSED_VARIABLE = YES;
+				MACOSX_DEPLOYMENT_TARGET = 10.13;
+				MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
+				MTL_FAST_MATH = YES;
+				ONLY_ACTIVE_ARCH = YES;
+				SDKROOT = macosx;
+				USER_HEADER_SEARCH_PATHS = "$(PROJECT_DIR)/include";
+			};
+			name = Debug;
+		};
+		CDED6A4521B2F5A700AB91D0 /* Release */ = {
+			isa = XCBuildConfiguration;
+			buildSettings = {
+				ALWAYS_SEARCH_USER_PATHS = NO;
+				CLANG_ANALYZER_NONNULL = YES;
+				CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
+				CLANG_CXX_LANGUAGE_STANDARD = "gnu++14";
+				CLANG_CXX_LIBRARY = "libc++";
+				CLANG_ENABLE_MODULES = YES;
+				CLANG_ENABLE_OBJC_ARC = YES;
+				CLANG_ENABLE_OBJC_WEAK = YES;
+				CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
+				CLANG_WARN_BOOL_CONVERSION = YES;
+				CLANG_WARN_COMMA = YES;
+				CLANG_WARN_CONSTANT_CONVERSION = YES;
+				CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
+				CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
+				CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
+				CLANG_WARN_EMPTY_BODY = YES;
+				CLANG_WARN_ENUM_CONVERSION = YES;
+				CLANG_WARN_INFINITE_RECURSION = YES;
+				CLANG_WARN_INT_CONVERSION = YES;
+				CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
+				CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
+				CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
+				CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
+				CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
+				CLANG_WARN_STRICT_PROTOTYPES = YES;
+				CLANG_WARN_SUSPICIOUS_MOVE = YES;
+				CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
+				CLANG_WARN_UNREACHABLE_CODE = YES;
+				CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
+				CODE_SIGN_IDENTITY = "-";
+				COPY_PHASE_STRIP = NO;
+				DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
+				ENABLE_NS_ASSERTIONS = NO;
+				ENABLE_STRICT_OBJC_MSGSEND = YES;
+				GCC_C_LANGUAGE_STANDARD = gnu11;
+				GCC_NO_COMMON_BLOCKS = YES;
+				GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
+				GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
+				GCC_WARN_UNDECLARED_SELECTOR = YES;
+				GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
+				GCC_WARN_UNUSED_FUNCTION = YES;
+				GCC_WARN_UNUSED_VARIABLE = YES;
+				MACOSX_DEPLOYMENT_TARGET = 10.13;
+				MTL_ENABLE_DEBUG_INFO = NO;
+				MTL_FAST_MATH = YES;
+				SDKROOT = macosx;
+				USER_HEADER_SEARCH_PATHS = "$(PROJECT_DIR)/include";
+			};
+			name = Release;
+		};
+		CDED6A4721B2F5A700AB91D0 /* Debug */ = {
+			isa = XCBuildConfiguration;
+			buildSettings = {
+				CODE_SIGN_STYLE = Automatic;
+				DYLIB_COMPATIBILITY_VERSION = 1;
+				DYLIB_CURRENT_VERSION = 1;
+				EXECUTABLE_PREFIX = lib;
+				PRODUCT_NAME = "$(TARGET_NAME)";
+				SKIP_INSTALL = YES;
+			};
+			name = Debug;
+		};
+		CDED6A4821B2F5A700AB91D0 /* Release */ = {
+			isa = XCBuildConfiguration;
+			buildSettings = {
+				CODE_SIGN_STYLE = Automatic;
+				DYLIB_COMPATIBILITY_VERSION = 1;
+				DYLIB_CURRENT_VERSION = 1;
+				EXECUTABLE_PREFIX = lib;
+				PRODUCT_NAME = "$(TARGET_NAME)";
+				SKIP_INSTALL = YES;
+			};
+			name = Release;
+		};
+/* End XCBuildConfiguration section */
+
+/* Begin XCConfigurationList section */
+		CDED6A3D21B2F5A700AB91D0 /* Build configuration list for PBXProject "shared_random_generator" */ = {
+			isa = XCConfigurationList;
+			buildConfigurations = (
+				CDED6A4421B2F5A700AB91D0 /* Debug */,
+				CDED6A4521B2F5A700AB91D0 /* Release */,
+			);
+			defaultConfigurationIsVisible = 0;
+			defaultConfigurationName = Release;
+		};
+		CDED6A4621B2F5A700AB91D0 /* Build configuration list for PBXNativeTarget "shared_random_generator" */ = {
+			isa = XCConfigurationList;
+			buildConfigurations = (
+				CDED6A4721B2F5A700AB91D0 /* Debug */,
+				CDED6A4821B2F5A700AB91D0 /* Release */,
+			);
+			defaultConfigurationIsVisible = 0;
+			defaultConfigurationName = Release;
+		};
+/* End XCConfigurationList section */
+	};
+	rootObject = CDED6A3A21B2F5A700AB91D0 /* Project object */;
+}

+ 49 - 0
src/random.cxx

@@ -0,0 +1,49 @@
+#include <cassert>
+#include <cfloat>
+#include <random>
+
+#include "shared_random_generator/random.h"
+#include "shared_random_generator/random_impl.h"
+
+namespace {
+  class default_random_number_generator : public engine::detail::random_impl {
+  public:
+    unsigned int exclusive(unsigned int max) override {
+      assert(max != 0);
+      return uniform(0, max - 1)(rng);
+    }
+
+    double exclusive(double min, double max) override {
+      return uniform_real(min, max)(rng);
+    }
+
+    double inclusive(double min, double max) override {
+      double real_max = std::nextafter(max, DBL_MAX);
+      return std::min(uniform_real(min, real_max)(rng), max);
+    }
+
+  private:
+    using engine = std::mt19937;
+    using uniform = std::uniform_int_distribution<typename engine::result_type>;
+    using uniform_real = std::uniform_real_distribution<>;
+
+    engine rng{std::random_device{}()};
+  };
+}
+
+namespace engine {
+  random_number_generator::random_number_generator()
+      : p_impl(std::make_shared<default_random_number_generator>()) {}
+
+  unsigned int random_number_generator::exclusive(unsigned int max) const {
+    return p_impl->exclusive(max);
+  }
+
+  double random_number_generator::exclusive(double min, double max) const {
+    return p_impl->exclusive(min, max);
+  }
+
+  double random_number_generator::inclusive(double min, double max) const {
+    return p_impl->inclusive(min, max);
+  }
+}