add vector_remove_at

This commit is contained in:
chatlanin 2022-03-21 10:42:52 +03:00
parent 6b124255a0
commit f32a1e49a6
10 changed files with 123 additions and 32 deletions

View File

@ -1,6 +1,7 @@
#include <iostream> #include <iostream>
#include <map> #include <map>
#include <set>
#include <vector> #include <vector>
#include "string/string.hpp" #include "string/string.hpp"
@ -10,27 +11,27 @@
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
// {// ex: split_str {// ex: split_str
// std::string str { "asdf,qwer,zxcv" }; std::string str { "asdf,qwer,zxcv" };
// hack::string::v_str v = hack::string::split_str(str, ','); hack::string::v_str v = hack::string::split_str(str, ',');
// for (const auto& c : v) std::cout << c << std::endl; for (const auto& c : v) std::cout << c << std::endl;
// } }
//
// {// ex: within {// ex: within
// std::cout << std::boolalpha << hack::range::within(12, 34, 12, 23, 31, 17, 22, 33) << std::endl; std::cout << std::boolalpha << hack::range::within(12, 34, 12, 23, 31, 17, 22, 33) << std::endl;
// } }
//
// {// ex: v_multiset {// ex: vector_multiset
// std::vector<std::string> v; std::vector<std::string> v;
// hack::container::v_multiset(v, "asdf", "qwer", "zcv"); hack::container::vector_multiset(v, "asdf", "qwer", "zcv");
// for(const auto& c : v) std::cout << c << std::endl; for(const auto& c : v) std::cout << c << std::endl;
// } }
//
// {// ex: s_multiset {// ex: set_multiset
// std::set<int> s; std::set<int> s;
// hack::container::s_multiset(s, 1, 2, 3, 3, 2, 1); hack::container::set_multiset(s, 1, 2, 3, 3, 2, 1);
// for(const auto& c : s) std::cout << c << std::endl; for(const auto& c : s) std::cout << c << std::endl;
// } }
{// ex: log {// ex: log
hack::log()(1234, "run in main", 1234); hack::log()(1234, "run in main", 1234);
@ -52,4 +53,15 @@ int main(int argc, char *argv[])
std::tuple<int, std::string, bool> tp { 1, "tuple test", false }; std::tuple<int, std::string, bool> tp { 1, "tuple test", false };
hack::log()(tp); hack::log()(tp);
} }
{// ex: matches
std::vector<int> v { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
hack::log()(hack::container::matches(v, 2, 5, 4, 12));
}
{// ex: vector_remove_at
std::vector<int> v { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
hack::container::vector_remove_at(v, 3);
hack::log()(v);
}
} }

View File

@ -1,4 +1,5 @@
deps += view_dep deps += view_dep
deps += concepts_dep
deps += iterators_dep deps += iterators_dep
deps += string_dep deps += string_dep
deps += range_dep deps += range_dep

27
src/concepts/concepts.hpp Normal file
View File

@ -0,0 +1,27 @@
#pragma once
#include <iostream>
#include <vector>
#include <list>
#include <map>
#include <unordered_map>
namespace hack::concepts
{
template<typename T>
concept is_sequence_container = std::same_as<T, std::vector<typename T::value_type>> ||
std::same_as<T, std::list<typename T::value_type>>;
template<typename T>
concept is_tuple = requires (T t)
{
std::tuple_cat(t, std::make_tuple(1, "tuple"));
};
template<class T>
concept is_string = std::is_convertible_v<T, std::string_view>;
template<class T>
concept is_map = std::same_as<T, std::map<typename T::key_type, typename T::mapped_type, typename T::key_compare, typename T::allocator_type>> ||
std::same_as<T, std::unordered_map<typename T::key_type, typename T::mapped_type, typename T::hasher, typename T::key_equal, typename T::allocator_type>>;
}

14
src/concepts/meson.build Normal file
View File

@ -0,0 +1,14 @@
headers = ['concepts.hpp']
sources = []
lib = library(
'concepts',
include_directories : inc,
install : true,
sources: [headers, sources]
)
concepts_dep = declare_dependency(
include_directories: inc,
link_with: lib
)

View File

@ -1,7 +1,6 @@
#pragma once #pragma once
#include <vector> #include <algorithm>
#include <set>
namespace hack::container namespace hack::container
{ {
@ -18,4 +17,33 @@ namespace hack::container
{ {
(r.insert(args), ...); (r.insert(args), ...);
} }
template<typename Range, typename... Args>
int matches(Range r, Args... args)
{
return (std::count(std::cbegin(r), std::cend(r), args) + ...);
}
// -----------------------------------------------------
// remove vector element if vector sorting no important
template<typename T>
void vector_remove_at(std::vector<T>& v, std::size_t idx)
{
if (idx < v.size())
{
v[idx] = std::move(v.back());
v.pop_back();
}
}
template<typename T>
void vector_remove_at(std::vector<T>& v, typename std::vector<T>::iterator it)
{
if (it != v.end())
{
*it = std::move(v.back());
v.pop_back();
}
}
// -----------------------------------------------------
} }

View File

@ -1,13 +1,9 @@
#pragma once #pragma once
#include <iostream>
#include <experimental/source_location> #include <experimental/source_location>
#include <vector>
#include <list>
#include <map>
#include <unordered_map>
#include "view/color.hpp" #include "view/color.hpp"
#include "concepts/concepts.hpp"
#include "iterators/sequence_ostream_iterator.hpp" #include "iterators/sequence_ostream_iterator.hpp"
#include "iterators/associative_ostream_iterator.hpp" #include "iterators/associative_ostream_iterator.hpp"
@ -113,6 +109,14 @@ namespace hack
std::cout << " }" << (count != 0 ? devider : ""); std::cout << " }" << (count != 0 ? devider : "");
} }
template <template <class, class> typename Function, class ...Args>
auto LogCall(std::string fun_name, Function<Args...> fun, std::string fun_param = "", Args... args)
{
auto temp{ fun(args...) };
std::cout << fun_name << "of" << fun_param << "/t -> /t" << temp << '\n';
return temp;
}
friend class warn; friend class warn;
friend class error; friend class error;
}; };

View File

@ -1,6 +1,7 @@
inc += include_directories('.') inc += include_directories('.')
subdir('view') subdir('view')
subdir('concepts')
subdir('iterators') subdir('iterators')
subdir('string') subdir('string')
subdir('range') subdir('range')

View File

@ -5,8 +5,8 @@ namespace hack::range
template<typename T, typename... Args> template<typename T, typename... Args>
bool within(const T min, const T max, Args... args) bool within(const T min, const T max, Args... args)
{ {
return ((min <= args && max >= args) && ...);
// 1, 5, 2, 3, 4 // 1, 5, 2, 3, 4
// ( (1 <= 2 && 5 >= 2) && (1 <= 3 && 5 >= 3) && (1 <= 4 && 5 >= 4) ) // ( (1 <= 2 && 5 >= 2) && (1 <= 3 && 5 >= 3) && (1 <= 4 && 5 >= 4) )
return ((min <= args && max >= args) && ...);
} }
} }

View File

@ -1,7 +1,5 @@
#include "string.hpp" #include "string.hpp"
#include <iostream>
namespace hack::string namespace hack::string
{ {
v_str split_str(const std::string& str, char t) v_str split_str(const std::string& str, char t)

View File

@ -2,9 +2,15 @@
#include "container/container.hpp" #include "container/container.hpp"
TEST(v_multiset, check) TEST(vector_multiset, check)
{ {
std::vector<int> v; std::vector<int> v;
hack::container::vector_multiset(v, 1, 2, 3); hack::container::vector_multiset(v, 1, 2, 3);
ASSERT_EQ(v.at(0), 1); ASSERT_EQ(v.at(0), 1);
} }
TEST(matches, check)
{
std::vector<int> v { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
ASSERT_EQ(hack::container::matches(v, 2, 5, 4, 12), 3);
}