Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
Vector.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2018 DIVIDE-Studio
3 Copyright (c) 2009 Ionut Cava
4
5 This file is part of DIVIDE Framework.
6
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software
9 and associated documentation files (the "Software"), to deal in the Software
10 without restriction,
11 including without limitation the rights to use, copy, modify, merge, publish,
12 distribute, sublicense,
13 and/or sell copies of the Software, and to permit persons to whom the
14 Software is furnished to do so,
15 subject to the following conditions:
16
17 The above copyright notice and this permission notice shall be included in
18 all copies or substantial portions of the Software.
19
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED,
22 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
23 PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
25 DAMAGES OR OTHER LIABILITY,
26 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
27 IN CONNECTION WITH THE SOFTWARE
28 OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30 */
31
32#pragma once
33#ifndef DVD_VECTOR_H_
34#define DVD_VECTOR_H_
35
36#include <EASTL/sort.h>
37#include <EASTL/vector.h>
38
39namespace Divide
40{
41 template<typename Type>
42 using vector = eastl::vector<Type>;
43
44 template<typename T, typename Type>
45 concept is_non_fixed_vector = std::is_same_v<T, vector<Type>>;
46 template<typename T, typename... Ts>
47 concept is_fixed_vector = std::is_same_v<T, eastl::fixed_vector<Ts...>>;
48 template<typename T, typename... Ts>
49 concept is_vector = is_non_fixed_vector<T, std::tuple_element_t<0, std::tuple<Ts...>>> || is_fixed_vector<T, Ts...>;
50
51 template <typename T, size_t nodeCount, bool bEnableOverflow = true, typename OverflowAllocator = typename eastl::type_select<bEnableOverflow, EASTLAllocatorType, EASTLDummyAllocatorType>::type>
52 inline void efficient_clear(eastl::fixed_vector<T, nodeCount, bEnableOverflow, OverflowAllocator> & fixed_vector)
53 {
54 if constexpr (bEnableOverflow)
55 {
56 fixed_vector.clear();
57 }
58 else
59 {
60 fixed_vector.reset_lose_memory();
61 }
62 }
63
64 template< typename T, typename A>
65 inline void efficient_clear( eastl::vector<T, A>& vec )
66 {
67 vec.resize(0);
68 }
69
70 template< typename T, typename Pred, typename A>
71 typename eastl::vector<T, A>::iterator insert_sorted( eastl::vector<T, A>& vec, T const& item, Pred&& pred )
72 {
73 return vec.insert( eastl::upper_bound( eastl::begin( vec ), eastl::end( vec ), item, pred ), item );
74 }
75
76 template<typename T, typename A>
77 void insert_unique( eastl::vector<T, A>& target, const T& item )
78 {
79 if ( eastl::find( eastl::cbegin( target ), eastl::cend( target ), item ) == eastl::cend( target ) )
80 {
81 target.push_back( item );
82 }
83
84 }
85
86 template<typename T, typename A1, typename A2>
87 void insert_unique( eastl::vector<T, A1>& target, const eastl::vector<T, A2>& source )
88 {
89 eastl::for_each( eastl::cbegin( source ), eastl::cend( source ),
90 [&target]( T const& item )
91 {
92 insert_unique( target, item );
93 } );
94 }
95
96 template<typename T, typename A1, typename A2>
97 void insert( eastl::vector<T, A1>& target, const eastl::vector<T, A2>& source )
98 {
99 target.insert( end( target ), begin( source ), end( source ) );
100 }
101
102 template <typename T, typename A>
103 bool contains( eastl::vector<T, A>& vec, const T& val )
104 {
105 return eastl::find( vec.cbegin(), vec.cend(), val ) != vec.cend();
106 }
107
108 template <typename T, typename A, class Predicate>
109 bool dvd_erase_if( eastl::vector<T, A>& vec, Predicate&& pred )
110 {
111 return erase_if( vec, pred ) > 0u;
112 }
113
114 template<typename T, typename A>
115 void pop_front( eastl::vector<T, A>& vec )
116 {
117 assert( !vec.empty() );
118 vec.erase( eastl::begin( vec ) );
119 }
120
121 template<typename T, typename A1, typename A2>
122 void unchecked_copy( eastl::vector<T, A1>& dst, const eastl::vector<T, A2>& src )
123 {
124 dst.resize( src.size() );
125 memcpy( dst.data(), src.data(), src.size() * sizeof( T ) );
126 }
127
128 template<typename T, typename U, typename A>
129 eastl::vector<T, A> convert( const eastl::vector<U, A>& data )
130 {
131 return eastl::vector<T, A>( eastl::cbegin( data ), eastl::cend( data ) );
132 }
133
134 template<typename Cont, typename It>
135 auto ToggleIndices( Cont& cont, It beg, It end ) -> decltype(eastl::end( cont ))
136 {
137 int helpIndx = 0;
138 return eastl::stable_partition( eastl::begin( cont ), eastl::end( cont ),
139 [&]( [[maybe_unused]] decltype(*eastl::begin( cont )) const& val ) -> bool
140 {
141 return eastl::find( beg, end, helpIndx++ ) == end;
142 } );
143 }
144
145 template<typename Cont, typename IndCont>
146 void EraseIndicesSorted( Cont& cont, IndCont& indices )
147 {
148 for ( auto it = indices.rbegin(); it != indices.rend(); ++it )
149 {
150 cont.erase( cont.begin() + *it );
151 }
152 }
153
154 template<typename Cont, typename IndCont>
155 void EraseIndices( Cont& cont, IndCont& indices )
156 {
157 eastl::sort( indices.begin(), indices.end() );
158 EraseIndicesSorted( cont, indices );
159 }
160
161 //ref: https://stackoverflow.com/questions/7571937/how-to-delete-items-from-a-stdvector-given-a-list-of-indices
162
163 template<typename T, typename A1, typename A2>
164 eastl::vector<T, A1> erase_indices( const eastl::vector<T, A1>& data, eastl::vector<size_t, A2>& indicesToDelete/* can't assume copy elision, don't pass-by-value */ )
165 {
166 eastl::sort( begin( indicesToDelete ), end( indicesToDelete ) );
167 return erase_sorted_indices( data, indicesToDelete );
168 }
169
170 template<typename T, typename A1, typename A2>
171 eastl::vector<T, A1> erase_sorted_indices( const eastl::vector<T, A1>& data, eastl::vector<size_t, A2>& indicesToDelete/* can't assume copy elision, don't pass-by-value */ )
172 {
173 if ( indicesToDelete.empty() )
174 {
175 return data;
176 }
177 assert( indicesToDelete.size() <= data.size() );
178
179 eastl::vector<T, A1> ret;
180 ret.reserve( data.size() - indicesToDelete.size() );
181
182 // new we can assume there is at least 1 element to delete. copy blocks at a time.
183 typename eastl::vector<T, A1>::const_iterator itBlockBegin = eastl::cbegin( data );
184 for ( size_t it : indicesToDelete )
185 {
186 typename eastl::vector<T, A1>::const_iterator itBlockEnd = eastl::cbegin( data ) + it;
187 if ( itBlockBegin != itBlockEnd )
188 {
189 eastl::copy( itBlockBegin, itBlockEnd, eastl::back_inserter( ret ) );
190 }
191 itBlockBegin = itBlockEnd + 1;
192 }
193
194 // copy last block.
195 if ( itBlockBegin != data.end() )
196 {
197 eastl::copy( itBlockBegin, eastl::cend( data ), eastl::back_inserter( ret ) );
198 }
199
200 return ret;
201 }
202}; //namespace Divide
203
204#endif //DVD_VECTOR_H_
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
eastl::vector< T, A1 > erase_indices(const eastl::vector< T, A1 > &data, eastl::vector< size_t, A2 > &indicesToDelete)
Definition: Vector.h:164
eastl::vector< T, A >::iterator insert_sorted(eastl::vector< T, A > &vec, T const &item, Pred &&pred)
Definition: Vector.h:71
void pop_front(eastl::vector< T, A > &vec)
Definition: Vector.h:115
eastl::vector< T, A > convert(const eastl::vector< U, A > &data)
Definition: Vector.h:129
void insert(eastl::vector< T, A1 > &target, const eastl::vector< T, A2 > &source)
Definition: Vector.h:97
eastl::vector< Type > vector
Definition: Vector.h:42
void unchecked_copy(eastl::vector< T, A1 > &dst, const eastl::vector< T, A2 > &src)
Definition: Vector.h:122
void EraseIndices(Cont &cont, IndCont &indices)
Definition: Vector.h:155
void efficient_clear(eastl::fixed_vector< T, nodeCount, bEnableOverflow, OverflowAllocator > &fixed_vector)
Definition: Vector.h:52
auto ToggleIndices(Cont &cont, It beg, It end) -> decltype(eastl::end(cont))
Definition: Vector.h:135
eastl::vector< T, A1 > erase_sorted_indices(const eastl::vector< T, A1 > &data, eastl::vector< size_t, A2 > &indicesToDelete)
Definition: Vector.h:171
bool contains(eastl::vector< T, A > &vec, const T &val)
Definition: Vector.h:103
void EraseIndicesSorted(Cont &cont, IndCont &indices)
Definition: Vector.h:146
void insert_unique(eastl::vector< T, A > &target, const T &item)
Definition: Vector.h:77
bool dvd_erase_if(eastl::vector< T, A > &vec, Predicate &&pred)
Definition: Vector.h:109