LibUDB  1.0.1
UDBase.h
1 /*
2  * Copyright (C) 2025 Yury Bobylev <bobilev_yury@mail.ru>
3  *
4  * This program is free software: you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the Free
6  * Software Foundation, version 3.
7  *
8  * This program is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11  * more details.
12  *
13  * You should have received a copy of the GNU General Public License along with
14  * this program. If not, see <https://www.gnu.org/licenses/>.
15  */
16 #ifndef UDBASE_H
17 #define UDBASE_H
18 
19 #include <UDBElement.h>
20 #include <filesystem>
21 #include <functional>
22 #include <omp.h>
23 #include <vector>
24 
57 class UDBase
58 {
59 public:
60  UDBase();
61 
62  virtual ~UDBase();
63 
70  UDBase(const UDBase &other);
71 
78  UDBase(UDBase &&other);
79 
85  UDBase &
86  operator=(const UDBase &other);
87 
93  UDBase &
94  operator=(UDBase &&other);
95 
102  UDBase &
103  operator+=(const UDBase &other);
104 
111  void
112  writeToBuffer(std::vector<char> &result);
113 
126  void
127  readFromBuffer(const std::vector<char> &buf, const size_t &offset = 0,
128  const size_t &bytes_to_read = 0);
137  void
138  writeToFile(const std::filesystem::path &f_path);
139 
147  void
148  readFromFile(const std::filesystem::path &f_path);
149 
155  std::vector<UDBElement> *
156  getRawBase() const;
157 
163  void
164  addElement(const UDBElement &el);
165 
172  void
173  addElements(const std::vector<UDBElement> &elements);
174 
181  virtual void
182  removeElement(const UDBElement &el);
183 
191  virtual void
192  removeElements(std::function<bool(const UDBElement &)> predicate);
193 
201  virtual void
202  replaceElement(const UDBElement &old_el, const UDBElement &new_el);
203 
207  void
208  clearBase();
209 
213  void
214  shrinkToFit();
215 
224  virtual UDBase
225  searchElement(std::function<bool(const UDBElement &)> search_function);
226 
233  virtual UDBase
235  std::function<void(const UDBElement &, UDBase &)> search_function);
236 
244  virtual std::vector<UDBElement>
245  searchElementV(std::function<bool(const UDBElement &)> search_function);
246 
253  virtual void
254  sortBase(std::function<bool(const UDBElement &, const UDBElement &)>
255  sort_function);
256 
262  size_t
263  baseSize() const;
264 
271  static UDBase
272  convertToBase(const std::vector<UDBElement> &elements);
273 
280  size_t
282 
292  std::vector<UDBase>
293  splitBase(const size_t &bytes_limit,
294  const size_t &minimum_elements = size_t(1));
295 
296 protected:
304  void
305  readFromBufferInner(const std::vector<char> &buf, const size_t &offset,
306  const size_t &bytes_to_read);
307 
314  void
315  writeToBufferInner(std::vector<char> &result);
316 
320  std::vector<UDBElement> base;
321 
325  std::string base_version = "UDB 1.0";
326 };
327 
328 #endif // UDBASE_H
void writeToBufferInner(std::vector< char > &result)
virtual void removeElements(std::function< bool(const UDBElement &)> predicate)
virtual void replaceElement(const UDBElement &old_el, const UDBElement &new_el)
UDBase & operator+=(const UDBase &other)
void readFromFile(const std::filesystem::path &f_path)
std::vector< UDBase > splitBase(const size_t &bytes_limit, const size_t &minimum_elements=size_t(1))
virtual void removeElement(const UDBElement &el)
static UDBase convertToBase(const std::vector< UDBElement > &elements)
virtual std::vector< UDBElement > searchElementV(std::function< bool(const UDBElement &)> search_function)
The UDBElement class.
Definition: UDBElement.h:27
UDBase & operator=(const UDBase &other)
void readFromBuffer(const std::vector< char > &buf, const size_t &offset=0, const size_t &bytes_to_read=0)
virtual UDBase searchElement(std::function< bool(const UDBElement &)> search_function)
void addElement(const UDBElement &el)
void shrinkToFit()
std::vector< UDBElement > base
Internal base object.
Definition: UDBase.h:320
void writeToBuffer(std::vector< char > &result)
std::string base_version
Base version string used to identify base in buffers and files.
Definition: UDBase.h:325
void readFromBufferInner(const std::vector< char > &buf, const size_t &offset, const size_t &bytes_to_read)
virtual void sortBase(std::function< bool(const UDBElement &, const UDBElement &)> sort_function)
std::vector< UDBElement > * getRawBase() const
void addElements(const std::vector< UDBElement > &elements)
void writeToFile(const std::filesystem::path &f_path)
The UDBase class.
Definition: UDBase.h:57
void clearBase()
size_t calculateWriteSize()
size_t baseSize() const