add container v_multiset

This commit is contained in:
chatlanin 2022-03-02 12:11:21 +03:00
parent 3148ecd29e
commit 9d3c2a4153
14 changed files with 72 additions and 12 deletions

View File

@ -2,16 +2,23 @@
#include "string/string.hpp" #include "string/string.hpp"
#include "range/range.hpp" #include "range/range.hpp"
#include "container/container.hpp"
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::v_str v = hack::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 << hack::within(12, 34, 12, 23, 31) << std::endl; 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;
} }
} }

View File

@ -1,5 +1,6 @@
deps += string_dep deps += string_dep
deps += range_dep deps += range_dep
deps += range_dep
executable( executable(
'hack', 'main.cpp', 'hack', 'main.cpp',

View File

@ -0,0 +1,5 @@
#include "container.hpp"
namespace hack::container
{
}

View File

@ -0,0 +1,14 @@
#pragma once
#include <vector>
namespace hack::container
{
template<typename Range, typename... Args>
void v_multiset(Range& r, Args... args)
{
std::size_t t = sizeof... (args);
r.reserve(t);
(r.emplace_back(std::forward<Args>(args)), ...);
}
}

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

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

View File

@ -2,3 +2,4 @@ inc += include_directories('.')
subdir('string') subdir('string')
subdir('range') subdir('range')
subdir('container')

View File

@ -1,5 +1,5 @@
#include "range.hpp" #include "range.hpp"
namespace hack namespace hack::renge
{ {
} }

View File

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

View File

@ -1,6 +1,6 @@
#include "string.hpp" #include "string.hpp"
namespace hack 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

@ -3,7 +3,7 @@
#include <string> #include <string>
#include <vector> #include <vector>
namespace hack namespace hack::string
{ {
using v_str = std::vector<std::string>; using v_str = std::vector<std::string>;

10
tests/container.cpp Normal file
View File

@ -0,0 +1,10 @@
#include <gtest/gtest.h>
#include "container/container.hpp"
TEST(v_multiset, check)
{
std::vector<int> v;
hack::container::v_multiset(v, 1, 2, 3);
ASSERT_EQ(v.at(0), 1);
}

View File

@ -18,3 +18,12 @@ test(
dependencies: [ range_dep, gtest_dep ] dependencies: [ range_dep, gtest_dep ]
) )
) )
test(
'container',
executable(
'container',
'container.cpp',
dependencies: [ range_dep, gtest_dep ]
)
)

View File

@ -4,6 +4,6 @@
TEST(within, check) TEST(within, check)
{ {
ASSERT_EQ(hack::within(23, 123, 34, 44, 55, 66), true); ASSERT_EQ(hack::range::within(23, 123, 34, 44, 55, 66), true);
ASSERT_EQ(hack::within(23, 123, 134, 44, 55, 66), false); ASSERT_EQ(hack::range::within(23, 123, 134, 44, 55, 66), false);
} }

View File

@ -7,7 +7,6 @@
TEST(split_str, check) TEST(split_str, check)
{ {
hack::v_str v { "asdf", "qwer", "zxcv" }; std::vector<std::string> v { "asdf", "qwer", "zxcv" };
ASSERT_EQ(hack::string::split_str("asdf,qwer,zxcv", ','), v);
ASSERT_EQ(hack::split_str("asdf,qwer,zxcv", ','), v);
} }