add vector_remove_at
This commit is contained in:
parent
6b124255a0
commit
f32a1e49a6
54
bin/main.cpp
54
bin/main.cpp
@ -1,6 +1,7 @@
|
||||
#include <iostream>
|
||||
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <vector>
|
||||
|
||||
#include "string/string.hpp"
|
||||
@ -10,27 +11,27 @@
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
// {// ex: split_str
|
||||
// std::string str { "asdf,qwer,zxcv" };
|
||||
// hack::string::v_str v = hack::string::split_str(str, ',');
|
||||
// for (const auto& c : v) std::cout << c << std::endl;
|
||||
// }
|
||||
//
|
||||
// {// ex: within
|
||||
// std::cout << std::boolalpha << hack::range::within(12, 34, 12, 23, 31, 17, 22, 33) << std::endl;
|
||||
// }
|
||||
//
|
||||
// {// ex: v_multiset
|
||||
// std::vector<std::string> v;
|
||||
// hack::container::v_multiset(v, "asdf", "qwer", "zcv");
|
||||
// for(const auto& c : v) std::cout << c << std::endl;
|
||||
// }
|
||||
//
|
||||
// {// ex: s_multiset
|
||||
// std::set<int> s;
|
||||
// hack::container::s_multiset(s, 1, 2, 3, 3, 2, 1);
|
||||
// for(const auto& c : s) std::cout << c << std::endl;
|
||||
// }
|
||||
{// ex: split_str
|
||||
std::string str { "asdf,qwer,zxcv" };
|
||||
hack::string::v_str v = hack::string::split_str(str, ',');
|
||||
for (const auto& c : v) std::cout << c << std::endl;
|
||||
}
|
||||
|
||||
{// ex: within
|
||||
std::cout << std::boolalpha << hack::range::within(12, 34, 12, 23, 31, 17, 22, 33) << std::endl;
|
||||
}
|
||||
|
||||
{// ex: vector_multiset
|
||||
std::vector<std::string> v;
|
||||
hack::container::vector_multiset(v, "asdf", "qwer", "zcv");
|
||||
for(const auto& c : v) std::cout << c << std::endl;
|
||||
}
|
||||
|
||||
{// ex: set_multiset
|
||||
std::set<int> s;
|
||||
hack::container::set_multiset(s, 1, 2, 3, 3, 2, 1);
|
||||
for(const auto& c : s) std::cout << c << std::endl;
|
||||
}
|
||||
|
||||
{// ex: log
|
||||
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 };
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
deps += view_dep
|
||||
deps += concepts_dep
|
||||
deps += iterators_dep
|
||||
deps += string_dep
|
||||
deps += range_dep
|
||||
|
27
src/concepts/concepts.hpp
Normal file
27
src/concepts/concepts.hpp
Normal 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
14
src/concepts/meson.build
Normal 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
|
||||
)
|
@ -1,7 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include <set>
|
||||
#include <algorithm>
|
||||
|
||||
namespace hack::container
|
||||
{
|
||||
@ -18,4 +17,33 @@ namespace hack::container
|
||||
{
|
||||
(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();
|
||||
}
|
||||
}
|
||||
// -----------------------------------------------------
|
||||
}
|
||||
|
@ -1,13 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include <experimental/source_location>
|
||||
#include <vector>
|
||||
#include <list>
|
||||
#include <map>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "view/color.hpp"
|
||||
#include "concepts/concepts.hpp"
|
||||
#include "iterators/sequence_ostream_iterator.hpp"
|
||||
#include "iterators/associative_ostream_iterator.hpp"
|
||||
|
||||
@ -113,6 +109,14 @@ namespace hack
|
||||
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 error;
|
||||
};
|
||||
|
@ -1,6 +1,7 @@
|
||||
inc += include_directories('.')
|
||||
|
||||
subdir('view')
|
||||
subdir('concepts')
|
||||
subdir('iterators')
|
||||
subdir('string')
|
||||
subdir('range')
|
||||
|
@ -5,8 +5,8 @@ namespace hack::range
|
||||
template<typename T, typename... Args>
|
||||
bool within(const T min, const T max, Args... args)
|
||||
{
|
||||
return ((min <= args && max >= args) && ...);
|
||||
// 1, 5, 2, 3, 4
|
||||
// ( (1 <= 2 && 5 >= 2) && (1 <= 3 && 5 >= 3) && (1 <= 4 && 5 >= 4) )
|
||||
return ((min <= args && max >= args) && ...);
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,5 @@
|
||||
#include "string.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
namespace hack::string
|
||||
{
|
||||
v_str split_str(const std::string& str, char t)
|
||||
|
@ -2,9 +2,15 @@
|
||||
|
||||
#include "container/container.hpp"
|
||||
|
||||
TEST(v_multiset, check)
|
||||
TEST(vector_multiset, check)
|
||||
{
|
||||
std::vector<int> v;
|
||||
hack::container::vector_multiset(v, 1, 2, 3);
|
||||
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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user