SOMHunter Core
common-types.h
Go to the documentation of this file.
1 /* This file is part of SOMHunter.
2  *
3  * Copyright (C) 2021 Frantisek Mejzlik <frankmejzlik@protonmail.com>
4  * Mirek Kratochvil <exa.exa@gmail.com>
5  * Patrik Vesely <prtrikvesely@gmail.com>
6  * Vit Skrhak <v.skrhak@gmail.com>
7  *
8  * SOMHunter is free software: you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License as published by the Free
10  * Software Foundation, either version 2 of the License, or (at your option)
11  * any later version.
12  *
13  * SOMHunter is distributed in the hope that it will be useful, but WITHOUT ANY
14  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU General Public License along with
19  * SOMHunter. If not, see <https://www.gnu.org/licenses/>.
20  */
21 
22 #ifndef COMMON_TYPES_H_
23 #define COMMON_TYPES_H_
24 
25 #include <cstdint>
26 #include <limits>
27 #include <set>
28 #include <sstream>
29 #include <stdexcept>
30 #include <string>
31 #include <vector>
32 
33 namespace sh {
34 /* ***
35  * Custom exceptions
36  */
37 class NotLoggedInEx : public std::runtime_error {
38 public:
39  NotLoggedInEx(const std::string& msg) : std::runtime_error{ msg } {}
40 };
41 
42 // ---
43 
45  DebugLogStreamPtrs() = delete;
48  // ---
49  std::stringstream& _summary;
50  std::stringstream& _actions;
51  std::stringstream& _results;
52 };
53 
54 using UnixTimestamp = std::int64_t;
55 
57 
58 // some types
59 using LogHash = std::string;
60 using KeywordId = size_t;
61 using KeywordIds = std::vector<KeywordId>;
62 using KwSearchId = std::pair<KeywordId, size_t>;
63 using KwSearchIds = std::vector<KwSearchId>;
64 using KwDescription = std::string;
65 
66 // wordnet IDs
67 using SynsetId = size_t;
68 using SynsetStrings = std::vector<std::string>;
69 using SynsetIds = std::vector<SynsetId>;
70 
71 using VideoId = unsigned;
72 using FrameNum = unsigned;
73 using ShotId = unsigned;
74 
75 using FrameId = unsigned long;
76 using ScreenImgsCont = std::vector<FrameId>;
77 using FeatureMatrix = std::vector<std::vector<float>>;
78 using FeatureVector = std::vector<float>;
79 
80 template <typename T_ = float>
81 using StdMatrix = std::vector<std::vector<T_>>;
82 
83 template <typename T_ = float>
84 using StdVector = std::vector<T_>;
85 
86 using LikesCont = std::set<FrameId>;
87 using BookmarksCont = std::set<FrameId>;
88 using ShownFramesCont = std::set<FrameId>;
89 
90 using ScreenVideosCont = std::set<VideoId>;
91 
92 #define SIZE_T_ERR_VAL (std::numeric_limits<size_t>::max)()
93 #define IMAGE_ID_ERR_VAL (std::numeric_limits<FrameId>::max)()
94 #define VIDEO_ID_ERR_VAL (std::numeric_limits<VideoId>::max)()
95 
97 template <typename T_>
98 constexpr T_ ERR_VAL() {
99  return std::numeric_limits<T_>::max();
100 }
101 
102 enum class DisplayType {
103  DNull,
104  DTopKNN,
105  DLoading,
106  DSom,
107  DEmbed,
108  DTopN,
109  DTopNContext,
110  DRand,
111  DVideoDetail,
112  DVideoReplay,
113  DRelocation,
114  NumItems
115 };
116 
117 inline DisplayType str_to_disp_type(const std::string& type_str) {
118  /* !!! THIS MUST MATCH WITH `config/config-core.json` FILE !!!
119  strings->displayTypes
120  */
121  if (type_str == "topn_display") return DisplayType::DTopN;
122  if (type_str == "topn_context_display") return DisplayType::DTopNContext;
123  if (type_str == "SOM_display") return DisplayType::DSom;
124  if (type_str == "topknn_display") return DisplayType::DTopKNN;
125  if (type_str == "video_detail") return DisplayType::DVideoDetail;
126  if (type_str == "video_replay") return DisplayType::DVideoReplay;
127 
128  return DisplayType::NumItems;
129 }
130 
131 inline std::string disp_type_to_str(DisplayType type) {
132  /* !!! THIS MUST MATCH WITH `config/config-core.json` FILE !!!
133  strings->displayTypes
134  */
135  std::string disp_type;
136  switch (type) {
137  case DisplayType::DTopN:
138  disp_type = "topn_display";
139  break;
140 
142  disp_type = "topn_context_display";
143  break;
144 
145  case DisplayType::DSom:
146  disp_type = "SOM_display";
147  break;
148 
150  disp_type = "topknn_display";
151  break;
152 
154  disp_type = "video_detail";
155  break;
156 
158  disp_type = "video_replay";
159  break;
160  default:
161  throw std::runtime_error{ "Uknown dusplay!" };
162  break;
163  }
164 
165  return disp_type;
166 }
167 
171 constexpr size_t operator""_z(unsigned long long int x) { return static_cast<size_t>(x); }
172 
176 struct Filters;
177 
178 struct UsedTools {
179  bool operator==(const UsedTools& other) const {
180  return (text_search_used == other.text_search_used && bayes_used == other.bayes_used &&
184  }
186  : text_search_used(false),
187  bayes_used(false),
188  topknn_used(false),
189  canvas_bitmap_used{ false },
190  canvas_text_used{ false },
191  relocation_used{ false },
192  temporal_query_used{ false },
193  filters{ nullptr } {}
194 
195  void reset() {
196  text_search_used = false;
197  bayes_used = false;
198  topknn_used = false;
199  canvas_bitmap_used = false;
200  canvas_text_used = false;
201  relocation_used = false;
202  temporal_query_used = false;
203  filters = nullptr;
204  }
205 
213 
214  const Filters* filters;
215 };
216 
217 struct SubmitData {
219 
220  bool push_submit(FrameId fr_ID) {
221  // If we're busy, notify caller
222  if (want_submit) return false;
223 
224  want_submit = true;
225  frame_ID = fr_ID;
226  return true;
227  }
228 
230  want_submit = false;
231  return frame_ID;
232  }
233 
234  bool submit_requested() const { return want_submit; }
235 
238 };
239 
240 using PageId = unsigned;
241 
242 using Weekday = uint8_t;
243 using Hour = uint8_t;
244 using Year = uint16_t;
245 using LscId = std::string;
246 
247 struct FiltersData {
249 
252 
255 
257 };
258 
259 }; // namespace sh
260 
261 #endif // COMMON_TYPES_H_
Definition: common-types.h:37
NotLoggedInEx(const std::string &msg)
Definition: common-types.h:39
#define IMAGE_ID_ERR_VAL
Definition: common-types.h:93
Definition: common-types.h:33
unsigned VideoId
Definition: common-types.h:71
std::set< VideoId > ScreenVideosCont
Definition: common-types.h:90
std::set< FrameId > ShownFramesCont
Definition: common-types.h:88
unsigned PageId
Definition: common-types.h:240
size_t SynsetId
Definition: common-types.h:67
std::string LogHash
Definition: common-types.h:59
size_t KeywordId
Definition: common-types.h:60
std::vector< std::string > SynsetStrings
Definition: common-types.h:68
DisplayType str_to_disp_type(const std::string &type_str)
Definition: common-types.h:117
uint8_t Hour
Definition: common-types.h:243
std::vector< KwSearchId > KwSearchIds
Definition: common-types.h:63
std::string KwDescription
Definition: common-types.h:64
uint16_t Year
Definition: common-types.h:244
constexpr T_ ERR_VAL()
Value indicating error/invalid/"NULL" value for the given type.
Definition: common-types.h:98
unsigned FrameNum
Definition: common-types.h:72
uint8_t Weekday
Definition: common-types.h:242
std::set< FrameId > LikesCont
Definition: common-types.h:86
std::vector< float > FeatureVector
Definition: common-types.h:78
std::vector< KeywordId > KeywordIds
Definition: common-types.h:61
std::set< FrameId > BookmarksCont
Definition: common-types.h:87
unsigned ShotId
Definition: common-types.h:73
DisplayType
Definition: common-types.h:102
std::vector< std::vector< float > > FeatureMatrix
Definition: common-types.h:77
std::vector< SynsetId > SynsetIds
Definition: common-types.h:69
std::vector< std::vector< T_ > > StdMatrix
Definition: common-types.h:81
std::vector< T_ > StdVector
Definition: common-types.h:84
std::string disp_type_to_str(DisplayType type)
Definition: common-types.h:131
SubmitResult
Definition: common-types.h:56
std::vector< FrameId > ScreenImgsCont
Definition: common-types.h:76
std::string LscId
Definition: common-types.h:245
std::pair< KeywordId, size_t > KwSearchId
Definition: common-types.h:62
std::int64_t UnixTimestamp
Definition: common-types.h:54
unsigned long FrameId
Definition: common-types.h:75
Definition: common-types.h:44
std::stringstream & _actions
Definition: common-types.h:50
DebugLogStreamPtrs & operator=(DebugLogStreamPtrs &)=delete
std::stringstream & _summary
Definition: common-types.h:49
std::stringstream & _results
Definition: common-types.h:51
DebugLogStreamPtrs & operator=(DebugLogStreamPtrs &&)=delete
Definition: common-types.h:247
Weekday weekday
Definition: common-types.h:248
Year year
Year interval.
Definition: common-types.h:254
Hour hour
In interval [0, 23].
Definition: common-types.h:251
LscId LSC_id
Definition: common-types.h:256
Container for all the available filters for the rescore.
Definition: query-types.h:103
Definition: common-types.h:217
FrameId get_and_pop_submit()
Definition: common-types.h:229
SubmitData()
Definition: common-types.h:218
bool push_submit(FrameId fr_ID)
Definition: common-types.h:220
bool want_submit
Definition: common-types.h:236
bool submit_requested() const
Definition: common-types.h:234
FrameId frame_ID
Definition: common-types.h:237
Definition: common-types.h:178
void reset()
Definition: common-types.h:195
bool temporal_query_used
Definition: common-types.h:212
bool topknn_used
Definition: common-types.h:208
bool text_search_used
Definition: common-types.h:206
bool operator==(const UsedTools &other) const
Definition: common-types.h:179
bool bayes_used
Definition: common-types.h:207
bool canvas_text_used
Definition: common-types.h:210
bool canvas_bitmap_used
Definition: common-types.h:209
const Filters * filters
Definition: common-types.h:214
bool relocation_used
Definition: common-types.h:211
UsedTools()
Definition: common-types.h:185