Wildmeshing Toolkit
Loading...
Searching...
No Matches
VectorUtils.h
1#pragma once
2
3#include <algorithm>
4#include <array>
5#include <iostream>
6#include <map>
7#include <set>
8#include <vector>
9#include "wmtk/utils/Logger.hpp"
10
11namespace wmtk {
12
13template <class T>
14inline std::vector<T> set_intersection(const std::vector<T>& v1, const std::vector<T>& v2)
15{
16 if (v1.size() > 1) {
17 assert(std::is_sorted(v1.begin(), v1.end()));
18 }
19 if (v2.size() > 1) {
20 assert(std::is_sorted(v2.begin(), v2.end()));
21 }
22
23 std::vector<T> v;
24 v.reserve(std::min(v1.size(), v2.size()));
25 std::set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), std::back_inserter(v));
26 return v;
27}
28
29template <class T>
30inline void vector_unique(std::vector<T>& v)
31{
32 if (v.size() > 1) {
33 std::sort(v.begin(), v.end());
34 v.erase(std::unique(v.begin(), v.end()), v.end());
35 }
36}
37
38template <class T, typename Comp, typename Equal>
39inline void vector_unique(std::vector<T>& v, Comp comp, Equal equal)
40{
41 if (v.size() > 1) {
42 std::sort(v.begin(), v.end(), comp);
43 v.erase(std::unique(v.begin(), v.end(), equal), v.end());
44 }
45}
46
47
48template <class T>
49inline void vector_print(std::vector<T>& v)
50{
51 wmtk::logger().info("vector {}", v);
52}
53
54template <class T>
55inline void vector_sort(std::vector<T>& v)
56{
57 if (v.size() > 1) {
58 std::sort(v.begin(), v.end());
59 }
60}
61
62template <class T>
63inline bool vector_erase(std::vector<T>& v, const T& t)
64{
65 auto it = std::find(v.begin(), v.end(), t);
66 if (it == v.end()) return false;
67 v.erase(it);
68 return true;
69}
70
71template <class T>
72inline bool vector_contains(const std::vector<T>& v, const T& t)
73{
74 auto it = std::find(v.begin(), v.end(), t);
75 if (it == v.end()) return false;
76 return true;
77}
78
79template <typename T>
80inline bool set_erase(std::vector<T>& v, const T& t)
81{
82 assert(std::is_sorted(v.begin(), v.end()));
83 auto it = std::lower_bound(v.begin(), v.end(), t);
84 if (it == v.end() || *it != t) return false; // not found
85 v.erase(it);
86 return true;
87}
88
89template <typename T>
90inline bool set_insert(std::vector<T>& vec, const T& val)
91{
92 assert(std::is_sorted(vec.begin(), vec.end()));
93 auto it = std::lower_bound(vec.begin(), vec.end(), val);
94 vec.insert(it, val);
95 return true;
96}
97template <typename T>
98std::vector<T> set_union(const std::vector<T>& vec, const std::vector<T>& vec2)
99{
100 assert(std::is_sorted(vec.begin(), vec.end()));
101 assert(std::is_sorted(vec2.begin(), vec2.end()));
102 std::vector<T> merged;
103 merged.reserve(vec2.size() + vec.size());
104 std::merge(vec.begin(), vec.end(), vec2.begin(), vec2.end(), std::back_inserter(merged));
105 vector_unique(merged);
106 return merged;
107}
108
109template <typename T>
110void set_union_inplace(std::vector<T>& vec, const std::vector<T>& vec2)
111{
112 assert(std::is_sorted(vec.begin(), vec.end()));
113 assert(std::is_sorted(vec2.begin(), vec2.end()));
114 // boundary_it specifies the boundary between vec and vec2 data
115 const auto boundary_it = vec.insert(vec.end(), vec2.begin(), vec2.end());
116 std::inplace_merge(vec.begin(), boundary_it, vec.end());
117 vector_unique(vec);
118}
119
120template <typename T, size_t N>
121inline void array_replace_inline(std::array<T, N>& arr, const T& v0, const T& v1)
122{
123 for (auto j = 0; j < N; j++) {
124 if (arr[j] == v0) {
125 arr[j] = v1;
126 break;
127 }
128 }
129}
130
131template <typename T, size_t N>
132inline std::array<T, N> array_replace(const std::array<T, N>& arr, const T& v0, const T& v1)
133{
134 std::array<size_t, 4> out = arr;
135 array_replace_inline(out, v0, v1);
136 return out;
137}
138
139} // namespace wmtk