crawlserv++  [under development]
Application for crawling and analyzing textual content of websites.
TextMap.hpp
Go to the documentation of this file.
1 /*
2  *
3  * ---
4  *
5  * Copyright (C) 2023 Anselm Schmidt (ans[ät]ohai.su)
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version in addition to the terms of any
11  * licences already herein identified.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <https://www.gnu.org/licenses/>.
20  *
21  * ---
22  *
23  * TextMap.hpp
24  *
25  * Text map (entries) used to annotate parts of a corpus (e.g. to store the dates or articles contained in these parts).
26  *
27  * Created on: Mar 4, 2020
28  * Author: ans
29  */
30 
31 #ifndef STRUCT_TEXTMAP_HPP_
32 #define STRUCT_TEXTMAP_HPP_
33 
34 #include "../Helper/Memory.hpp"
35 
36 #include <cstddef> // std::size_t
37 #include <string> // std::string
38 #include <utility> // std::pair, std::swap
39 #include <vector> // std::vector
40 
41 namespace crawlservpp::Struct {
42 
44 
49  struct TextMapEntry {
52 
54 
58  std::size_t p{};
59 
61  std::size_t l{};
62 
64 
69  std::string value;
70 
74 
76  TextMapEntry() = default;
77 
79 
88  std::size_t setPos,
89  std::size_t setLength
90  ) : p(setPos), l(setLength) {}
91 
93 
105  std::size_t setPos,
106  std::size_t setLength,
107  const std::string& setValue
108  ) : p(setPos), l(setLength), value(setValue) {}
109 
113 
115 
119  void swap(TextMapEntry& other) {
120  std::swap(*this, other);
121  }
122 
126 
128  void free() {
129  this->p = 0;
130  this->l = 0;
131 
132  Helper::Memory::free(this->value);
133  }
134 
138 
140 
146  [[nodiscard]] std::string str() const {
147  std::string result{"pos="};
148 
149  result += std::to_string(this->p);
150  result += "; length=";
151  result += std::to_string(this->l);
152  result += "; value=\"";
153  result += this->value;
154  result += "\"";
155 
156  return result;
157  }
158 
162 
164 
172  static std::size_t& pos(TextMapEntry& entry) {
173  return entry.p;
174  }
175 
177 
185  static std::size_t& pos(std::pair<std::size_t, std::size_t>& entry) {
186  return entry.first;
187  }
188 
190 
197  static std::size_t pos(const TextMapEntry& entry) {
198  return entry.p;
199  }
200 
202 
210  static std::size_t pos(const std::pair<std::size_t, std::size_t>& entry) {
211  return entry.first;
212  }
213 
215 
221  template<typename T> static std::size_t end(const T& entry) {
222  return TextMapEntry::pos(entry) + TextMapEntry::length(entry);
223  }
224 
226 
234  static std::size_t& length(TextMapEntry& entry) {
235  return entry.l;
236  }
237 
239 
247  static std::size_t& length(std::pair<std::size_t, std::size_t>& entry) {
248  return entry.second;
249  }
250 
252 
259  static std::size_t length(const TextMapEntry& entry) {
260  return entry.l;
261  }
262 
264 
272  static std::size_t length(const std::pair<std::size_t, std::size_t>& entry) {
273  return entry.second;
274  }
275 
277  };
278 
280  using TextMap = std::vector<TextMapEntry>;
281 
282 } /* namespace crawlservpp::Struct */
283 
284 #endif /* STRUCT_TEXTMAP_HPP_ */
std::size_t l
The length of the annotated part inside the text.
Definition: TextMap.hpp:61
static std::size_t & pos(TextMapEntry &entry)
Gets a reference to the position of a text map entry.
Definition: TextMap.hpp:172
Text map entry.
Definition: TextMap.hpp:49
static std::size_t pos(const std::pair< std::size_t, std::size_t > &entry)
Gets the position of a sentence map entry.
Definition: TextMap.hpp:210
static std::size_t & pos(std::pair< std::size_t, std::size_t > &entry)
Gets a reference to the position of a sentence map entry.
Definition: TextMap.hpp:185
TextMapEntry(std::size_t setPos, std::size_t setLength)
Constructor creating an empty annotation.
Definition: TextMap.hpp:87
TextMapEntry()=default
Default constructor.
std::vector< TextMapEntry > TextMap
A text map is defined as a vector of text map entries.
Definition: TextMap.hpp:280
static std::size_t & length(std::pair< std::size_t, std::size_t > &entry)
Gets a reference to the length of a sentence map entry.
Definition: TextMap.hpp:247
std::string str() const
Converts the text map entry into a string.
Definition: TextMap.hpp:146
TextMapEntry(std::size_t setPos, std::size_t setLength, const std::string &setValue)
Constructor creating a non-empty annotation.
Definition: TextMap.hpp:104
void free()
Resets its properties to their default values and frees the memory used by the entry.
Definition: TextMap.hpp:128
std::string value
Value of the annotation.
Definition: TextMap.hpp:69
void swap(TextMapEntry &other)
Swaps the text map entry with another.
Definition: TextMap.hpp:119
Namespace for data structures.
Definition: AlgoThreadProperties.hpp:43
static std::size_t pos(const TextMapEntry &entry)
Gets the position of a text map entry.
Definition: TextMap.hpp:197
static void free(T &target)
Frees memory by swapping.
Definition: Memory.hpp:42
static std::size_t & length(TextMapEntry &entry)
Gets a reference to the length of a text map entry.
Definition: TextMap.hpp:234
std::size_t p
The position of the annotated part inside the text.
Definition: TextMap.hpp:58
static std::size_t end(const T &entry)
Gets the end of a map entry.
Definition: TextMap.hpp:221
static std::size_t length(const TextMapEntry &entry)
Gets the length of a text map entry.
Definition: TextMap.hpp:259
static std::size_t length(const std::pair< std::size_t, std::size_t > &entry)
Gets the length of a sentence map entry.
Definition: TextMap.hpp:272