![]() |
RTBKit
0.9
Open-source framework to create real-time ad bidding systems.
|
00001 /* segments.h -*- C++ -*- 00002 Structure that holds a list of segments. 00003 */ 00004 00005 #pragma once 00006 00007 #include "jml/utils/compact_vector.h" 00008 #include "jml/db/persistent_fwd.h" 00009 #include "soa/jsoncpp/json.h" 00010 #include "soa/types/value_description_fwd.h" 00011 #include <boost/shared_ptr.hpp> 00012 #include <map> 00013 00014 00015 namespace RTBKIT { 00016 00017 00019 enum SegmentResult { 00020 SEG_NOT_PRESENT, 00021 SEG_PRESENT, 00022 SEG_MISSING 00023 }; 00024 00025 /*****************************************************************************/ 00026 /* SEGMENTS */ 00027 /*****************************************************************************/ 00028 00033 struct SegmentList { 00034 SegmentList(); 00035 SegmentList(const std::vector<std::string> & segs); 00036 SegmentList(const std::vector<int> & segs); 00037 SegmentList(const std::vector<std::pair<int, float> > & segs); 00038 00039 bool contains(int i) const; 00040 bool contains(const std::string & str) const; 00041 00042 //float weight(int i) const; 00043 //float weight(const std::string & str) const; 00044 00045 bool match(const SegmentList & other) const; 00046 bool match(const std::vector<int> & other) const; 00047 bool match(const std::vector<std::string> & other) const; 00048 00049 size_t size() const; 00050 bool empty() const; 00051 00052 /* JSON */ 00053 00054 static SegmentList createFromJson(const Json::Value & json); 00055 Json::Value toJson() const; 00056 std::string toJsonStr() const; 00057 std::string toString() const; 00058 00059 /* Mutation */ 00060 00061 void add(int i, float weight = 1.0); 00062 void add(const std::string & str, float weight = 1.0); 00063 00064 void sort(); 00065 00066 static int parseSegmentNum(const std::string & str); 00067 00069 bool intsOnly() const { return strings.empty(); } 00070 00072 void forEach(const std::function<void (int, std::string, float)> & onSegment) 00073 const; 00074 00075 //private: 00076 ML::compact_vector<int, 7> ints; 00077 std::vector<std::string> strings; 00078 ML::compact_vector<float, 5> weights; 00079 00080 void serialize(ML::DB::Store_Writer & store) const; 00081 void reconstitute(ML::DB::Store_Reader & store); 00082 std::string serializeToString() const; 00083 static SegmentList reconstituteFromString(const std::string & str); 00084 }; 00085 00086 IMPL_SERIALIZE_RECONSTITUTE(SegmentList); 00087 00088 inline std::ostream & 00089 operator << (std::ostream & stream, const SegmentList & segs) 00090 { 00091 return stream << segs.toString(); 00092 } 00093 00094 00095 /*****************************************************************************/ 00096 /* SEGMENTS BY SOURCE */ 00097 /*****************************************************************************/ 00098 00099 typedef std::map<std::string, std::shared_ptr<SegmentList> > 00100 SegmentsBySourceBase; 00101 00104 struct SegmentsBySource 00105 : public SegmentsBySourceBase { 00106 00107 SegmentsBySource(); 00108 00109 SegmentsBySource(SegmentsBySourceBase && other); 00110 SegmentsBySource(const SegmentsBySourceBase & other); 00111 00112 const SegmentList & get(const std::string & str) const; 00113 00114 void sortAll(); 00115 00116 void add(const std::string & source, 00117 const std::shared_ptr<SegmentList> & segs) 00118 { 00119 addSegment(source, segs); 00120 } 00121 00122 void addSegment(const std::string & source, 00123 const std::shared_ptr<SegmentList> & segs); 00124 void addInts(const std::string & source, 00125 const std::vector<int> & segs); 00126 void addWeightedInts(const std::string & source, 00127 const std::vector<std::pair<int, float> > & segs); 00128 void addStrings(const std::string & source, 00129 const std::vector<std::string> & segs); 00130 00134 void add(const std::string & source, const std::string & segment, 00135 float weight = 1.0); 00136 00140 void add(const std::string & source, int segment, 00141 float weight = 1.0); 00142 00143 Json::Value toJson() const; 00144 00145 static SegmentsBySource createFromJson(const Json::Value & json); 00146 00147 void serialize(ML::DB::Store_Writer & store) const; 00148 void reconstitute(ML::DB::Store_Reader & store); 00149 00150 std::string serializeToString() const; 00151 static SegmentsBySource reconstituteFromString(const std::string & str); 00152 }; 00153 00154 IMPL_SERIALIZE_RECONSTITUTE(SegmentsBySource); 00155 00156 Datacratic::ValueDescriptionT<RTBKIT::SegmentsBySource> * 00157 getDefaultDescription(RTBKIT::SegmentsBySource * = 0); 00158 00159 } // namespace RTBKIT