Skylark (Sketching Library)
0.1
|
00001 #ifndef SKYLARK_CONTEXT_HPP 00002 #define SKYLARK_CONTEXT_HPP 00003 00004 #include "../config.h" 00005 00006 #include "exception.hpp" 00007 #include "../utility/randgen.hpp" 00008 00009 #include "boost/smart_ptr.hpp" 00010 #include "boost/property_tree/ptree.hpp" 00011 #include "boost/property_tree/json_parser.hpp" 00012 00013 namespace skylark { namespace base { 00014 00019 struct context_t { 00024 context_t (int seed, int counter=0) : 00025 _counter(counter), 00026 _seed(seed) {} 00027 00028 #if 0 00029 context_t (context_t&& ctxt) : 00030 _counter(std::move(ctxt._counter)), _seed(std::move(ctxt._seed)) 00031 {} 00032 00033 context_t(const context_t& other) { 00034 _seed = other._seed; 00035 _counter = other._counter; 00036 } 00037 00038 context_t& operator=(const context_t& other) { 00039 _seed = other._seed; 00040 _counter = other._counter; 00041 return *this; 00042 } 00043 #endif 00044 00049 context_t (const boost::property_tree::ptree& json) { 00050 _seed = json.get<int>("seed"); 00051 _counter = json.get<size_t>("counter"); 00052 } 00053 00054 boost::property_tree::ptree to_ptree() const { 00055 boost::property_tree::ptree pt; 00056 pt.put("skylark_object_type", "context"); 00057 pt.put("skylark_version", VERSION); 00058 pt.put("seed", _seed); 00059 pt.put("counter", _counter); 00060 return pt; 00061 } 00062 00063 00093 template <typename Distribution> 00094 skylark::utility::random_samples_array_t<Distribution> 00095 allocate_random_samples_array(size_t size, Distribution& distribution) { 00096 skylark::utility::random_samples_array_t<Distribution> 00097 random_samples_array(_counter, size, _seed, distribution); 00098 _counter += size; 00099 return random_samples_array; 00100 } 00101 00108 template <typename Distribution > 00109 std::vector<typename Distribution::result_type> 00110 generate_random_samples_array(size_t size, 00111 Distribution& distribution) { 00112 skylark::utility::random_samples_array_t<Distribution> 00113 allocated_random_samples_array(_counter, size, _seed, distribution); 00114 _counter += size; 00115 std::vector<typename Distribution::result_type> random_samples_array; 00116 try { 00117 random_samples_array.resize(size); 00118 } catch (std::bad_alloc ba) { 00119 SKYLARK_THROW_EXCEPTION ( 00120 base::allocation_exception() 00121 << base::error_msg(ba.what()) ); 00122 } 00123 for(size_t i = 0; i < size; i++) { 00124 random_samples_array[i] = allocated_random_samples_array[i]; 00125 } 00126 return random_samples_array; 00127 } 00128 00129 00138 skylark::utility::random_array_t allocate_random_array(size_t size) { 00139 skylark::utility::random_array_t random_array(_counter, size, _seed); 00140 _counter += size; 00141 return random_array; 00142 } 00143 00144 00152 int random_int() { 00153 skylark::utility::random_array_t random_array = 00154 allocate_random_array(1); 00155 int sample = random_array[0]; 00156 return sample; 00157 } 00158 00159 00160 size_t get_counter() { return _counter; } 00161 00166 friend boost::property_tree::ptree& operator<<( 00167 boost::property_tree::ptree &sk, const context_t &data); 00168 private: 00169 00171 size_t _counter; 00173 int _seed; 00174 }; 00175 00176 boost::property_tree::ptree& operator<<(boost::property_tree::ptree &sk, 00177 const context_t &data) { 00178 sk.put("sketch.context.seed", data._seed); 00179 sk.put("sketch.context.counter", data._counter); 00180 return sk; 00181 } 00182 00183 } } 00185 #endif // SKYLARK_CONTEXT_HPP