-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubarr.hpp
More file actions
69 lines (58 loc) · 1.85 KB
/
subarr.hpp
File metadata and controls
69 lines (58 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// algocpp/array/subarr.hpp
//
// This file is part of algocpp and is copyrighted by algocpp.
// If used, it must comply with the MIT License.
#ifndef ALGOCPP_ARRAY_SUBARR
#define ALGOCPP_ARRAY_SUBARR
#include <vector>
#include <array>
#include <list>
#include <cstdint>
#include <stdexcept>
namespace algocpp
{
namespace array
{
namespace base
{
template <typename T>
inline std::vector<T> base_subarr(const std::vector<T> v, const unsigned long long pos, unsigned long long n = SIZE_MAX)
{
// Processing when returning to the end
if (n == SIZE_MAX)
{
n = v.size() - pos;
}
if (pos > v.size() - 1 || pos + n > v.size())
{
throw std::out_of_range("You cannot get an array of length " + std::to_string(pos) + " starting from " + std::to_string(n) + ".");
}
std::vector<T> result(n);
for (unsigned long long i = pos; i < pos + n; ++i)
{
result[i - pos] = v[i];
}
return result;
}
}
template <typename T>
inline std::vector<T> subarr(const std::vector<T> v, const unsigned long long pos, unsigned long long n = SIZE_MAX)
{
return base::base_subarr<T>(v, pos, n);
}
template <typename T>
inline std::list<T> subarr(const std::list<T> v, const unsigned long long pos, unsigned long long n = SIZE_MAX)
{
std::vector<T> tmp = base::base_subarr<T>(std::vector<T>(v.begin(), v.end()), pos, n);
return std::list<T>(tmp.begin(), tmp.end());
}
// TODO: Make it possible to call it with `std::array`.
// template <typename T, std::size_t N, std::size_t len>
// inline std::array<T, len> subarr(const std::array<T, N> v, const unsigned long long pos, unsigned long long n = SIZE_MAX)
// {
// std::vector<T> tmp = base::base_subarr<T>(std::vector<T>(v.begin(), v.end()), pos, n);
// return std::array<T, len>(tmp.begin(), tmp.end());
// }
}
}
#endif // ALGOCPP_ARRAY_SUBARR