crawlserv++  [under development]
Application for crawling and analyzing textual content of websites.
Container.hpp
Go to the documentation of this file.
1 /*
2  *
3  * ---
4  *
5  * Copyright (C) 2021 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  * Container.hpp
24  *
25  * Helper function templates for container operations.
26  *
27  * Created on: Feb 24, 2021
28  * Author: ans
29  */
30 
31 #ifndef HELPER_CONTAINER_HPP_
32 #define HELPER_CONTAINER_HPP_
33 
34 #include <iterator> // std::begin, std::end, std::make_move_iterator
35 #include <numeric> // std::accumulate
36 
39 
41 
51  template<typename T> static void append(
52  T& to,
53  const T& from,
54  typename T::size_type startAt,
55  typename T::size_type endAt
56  ) {
57  to.insert(std::end(to), std::begin(from) + startAt, std::begin(from) + endAt);
58  }
59 
61 
69  template<typename T> static void append(
70  T& to,
71  const T& from,
72  typename T::size_type startAt
73  ) {
74  to.insert(std::end(to), std::begin(from) + startAt, std::end(from));
75  }
76 
78 
84  template<typename T> static void append(
85  T& to,
86  const T& from
87  ) {
88  to.insert(std::end(to), std::begin(from), std::end(from));
89  }
90 
92 
99  template<typename T> static void moveInto(T& to, T& from) {
100  to.insert(
101  std::end(to),
102  std::make_move_iterator(std::begin(from)),
103  std::make_move_iterator(std::end(from))
104  );
105  }
106 
108 
117  template<typename T> static void moveInto(T& to, T& from, typename T::size_type at) {
118  to.insert(
119  std::begin(to) + at,
120  std::make_move_iterator(std::begin(from)),
121  std::make_move_iterator(std::end(from))
122  );
123  }
124 
126 
133  template<typename T> static void eraseFirst(T& container, typename T::size_type n) {
134  container.erase(std::begin(container), std::begin(container) + n);
135  }
136 
138 
144  template<typename T> static typename T::size_type bytes(const T& container) {
145  return std::accumulate(
146  std::begin(container),
147  std::end(container),
148  typename T::size_type{},
149  [](const auto size, const auto& element) {
150  return size + element.size();
151  }
152  );
153  }
154 
155 } /* namespace crawlservpp::Helper::Container */
156 
157 #endif /* HELPER_CONTAINER_HPP_ */
static void eraseFirst(T &container, typename T::size_type n)
Erases the first elements of an iterable container.
Definition: Container.hpp:133
static void moveInto(T &to, T &from)
Moves the elements of an iterable container into another iterable container.
Definition: Container.hpp:99
static T::size_type bytes(const T &container)
Returns the number of bytes in an iterable container.
Definition: Container.hpp:144
static void append(T &to, const T &from, typename T::size_type startAt, typename T::size_type endAt)
Appends (part of) an iterable container to another container.
Definition: Container.hpp:51
Namespace for global container helper function templates.
Definition: Container.hpp:38