diff options
author | Oskar <[email protected]> | 2024-10-10 18:29:06 +0200 |
---|---|---|
committer | Oskar <[email protected]> | 2024-10-10 18:29:06 +0200 |
commit | 8eac964cd864f190861d0f68f6bcfc117c2ff6ed (patch) | |
tree | 35945d39babf23376988dfc76408e4b70792e206 |
some experimentation
-rw-r--r-- | Makefile | 18 | ||||
-rwxr-xr-x | reverse | bin | 0 -> 1765056 bytes | |||
-rwxr-xr-x | reverse-TESTING | bin | 0 -> 468176 bytes | |||
-rw-r--r-- | reverse-containers.cpp | 277 |
4 files changed, 295 insertions, 0 deletions
diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..c0bdd11 --- /dev/null +++ b/Makefile @@ -0,0 +1,18 @@ +CC=g++ +CFLAGS_TESTBIN=-O0 -Wfatal-errors -Wall -Werror -Wextra -g3 -fsanitize=address -fsanitize=leak -Wpedantic -Wformat=2 -Wshadow -Wformat-truncation=2 -Wformat-overflow -fno-common -std=c++20 +CFLAGS=-O3 -flto -march=native -DNDEBUG -fomit-frame-pointer -s -static -std=c++20 +TARGET=reverse +TESTTARGET=reverse-TESTING +MAKEFLAGS += +SRCS=reverse-containers.cpp + +all: release tests +clean: + rm -f $(TARGET) + rm -f $(TESTTARGET) + +tests: + $(CC) $(CFLAGS_TESTBIN) $(SRCS) -o $(TESTTARGET) + +release: + $(CC) $(CFLAGS) $(SRCS) -o $(TARGET) Binary files differdiff --git a/reverse-TESTING b/reverse-TESTING Binary files differnew file mode 100755 index 0000000..015d555 --- /dev/null +++ b/reverse-TESTING diff --git a/reverse-containers.cpp b/reverse-containers.cpp new file mode 100644 index 0000000..4639887 --- /dev/null +++ b/reverse-containers.cpp @@ -0,0 +1,277 @@ +#include <iostream> +#include <string> +#include <vector> +#include <list> +#include <deque> +#include <array> +#include <unistd.h> +#include <cstring> +#define CONTAINER_SIZE 1048576 +char *g_argv = nullptr; +inline void usageOUT() { + std::cerr << g_argv << " [-1][-2][-3][-4][-5][-6][-7]" << std::endl; +} + +std::vector<char> &reverse_vector_reverse_iterator(std::vector<char> &vc) { + + auto size_vc = vc.size(); + std::vector<char> tmp_vc(size_vc); + auto vc_rbeg = vc.rbegin(); + auto vc_rend = vc.rend(); + for(auto &a : tmp_vc) { + if(vc_rbeg == vc_rend) { + break; + } + a = *vc_rbeg++; + } + + vc = tmp_vc; + return vc; +} + +std::vector<char> &reverse_vector_no_tmp_iterators(std::vector<char> &vc) { + + auto r_beg = vc.rbegin(); + auto beg = vc.begin(); + for(decltype(vc.size()) a = 0 ; a != (vc.size() / 2) ; ++a, ++beg, ++r_beg) { + char tmp = *beg; + *beg = *r_beg; + *r_beg = tmp; + } + + return vc; +} + +std::vector<char> &reverse_vector_construct_swap(std::vector<char> &vc) { + + std::vector tmp_vc(vc.crbegin(), vc.crend()); + vc.swap(tmp_vc); + return vc; +} + +std::vector<char> &reverse_vector_subscript(std::vector<char> &vc) { + + auto imax = vc.size()-1; + for(decltype(vc.size()) i = 0 ; i != (vc.size() / 2) ; ++i, --imax) { + char tmp = vc[i]; + vc[i] = vc[imax]; + vc[imax] = tmp; + } + + return vc; +} + +std::deque<char> &reverse_deque_no_tmp_iterators(std::deque<char> &dc) { + + auto rbeg = dc.rbegin(); + auto beg = dc.begin(); + for(decltype(dc.size()) i = 0 ; i != (dc.size() / 2) ; ++i, ++beg, ++rbeg) { + char tmp = *beg; + *beg = *rbeg; + *rbeg = tmp; + } + + return dc; +} + +std::deque<char> &reverse_deque_construct_swap(std::deque<char> &dc) { + + std::deque<char> tmp_dc(dc.crbegin(), dc.crend()); + dc.swap(tmp_dc); + return dc; +} + +char *reverse_cstring(const char *cstr, char cstr2[CONTAINER_SIZE]) { + + auto max_len = std::strlen(cstr) -1; + for(size_t i = 0 ; cstr[i] != '\0' && i != CONTAINER_SIZE ; ++i, --max_len) { + cstr2[i] = cstr[max_len]; + } + + return cstr2; +} + +std::string &reverse_string_construct_swap(std::string &sc) { + + std::string tmp_sc(sc.crbegin(), sc.crend()); + sc.swap(tmp_sc); + return sc; +} + +std::string &reverse_string_no_tmp_iterators(std::string &sc) { + + auto rbeg = sc.rbegin(); + auto beg = sc.begin(); + for(decltype(sc.size()) i = 0 ; i != (sc.size() / 2); ++i, ++beg, ++rbeg) { + char tmp = *beg; + *beg = *rbeg; + *rbeg = tmp; + } + + return sc; +} + +void print_container(std::string &sc) { + + std::cout << sc << std::endl; +} + +void print_container(const char *ap) { + + std::cout << ap; + std::cout << std::endl; +} + +void print_container(std::vector<char> &vc) { + + for(auto &b : vc) { + std::cout << b << " "; + } + + std::cout << std::endl; +} + +void print_container(std::deque<char> &dc) { + + for(auto &b : dc) { + std::cout << b << " "; + } + + std::cout << std::endl; +} + +int main(int argc, char **argv) { + + g_argv = argv[0]; + if(argc < 2 || argc > 2) { + usageOUT(); + return EXIT_FAILURE; + } + + int opt; + int one = 0; + int two = 0; + int three = 0; + int four = 0; + int five = 0; + int six = 0; + int seven = 0; + int eight = 0; + int nine = 0; + while ((opt = getopt(argc, argv, "123456789")) != -1) { + switch (opt) { + case '1': + + one = 1; + + break; + case '2': + + two = 1; + + break; + case '3': + + three = 1; + + break; + case '4': + + four = 1; + + break; + case '5': + + five = 1; + + break; + case '6': + + six = 1; + + break; + case '7': + + seven = 1; + + break; + case '8': + + eight = 1; + + break; + case '9': + + nine = 1; + + break; + } + } + + if((one + two + three + four + five + six + seven + eight + nine) > 1) { + usageOUT(); + return EXIT_FAILURE; + } + + std::string input_str = "abcdef"; + const char *input_cstr = "abcdef"; + std::vector<char> input_vec = {'a', 'b', 'c', 'd', 'e', 'f'}; + std::deque<char> input_deq = {'a', 'b', 'c', 'd', 'e', 'f'}; + if(one == 1) { + reverse_vector_reverse_iterator(input_vec); + print_container(input_vec); + std::cout << "reverse_vector_reverse_iterator" << std::endl; + } + + if(two == 1) { + reverse_vector_no_tmp_iterators(input_vec); + print_container(input_vec); + std::cout << "reverse_vector_no_tmp_iterators" << std::endl; + } + + if(three == 1) { + reverse_vector_construct_swap(input_vec); + print_container(input_vec); + std::cout << "reverse_vector_construct_swap" << std::endl; + } + + if(four == 1) { + reverse_vector_subscript(input_vec); + print_container(input_vec); + std::cout << "reverse_vector_subscript" << std::endl; + } + + if(five == 1) { + reverse_deque_no_tmp_iterators(input_deq); + print_container(input_deq); + std::cout << "reverse_deque_no_tmp_iterators" << std::endl; + } + + if(six == 1) { + reverse_deque_construct_swap(input_deq); + print_container(input_deq); + std::cout << "reverse_deque_construct_swap" << std::endl; + } + + if(seven == 1) { + char cstr2[CONTAINER_SIZE]; + cstr2[0] = '\0'; + reverse_cstring(input_cstr, cstr2); + print_container(cstr2); + std::cout << "reverse_cstring" << std::endl; + } + + if(eight == 1) { + reverse_string_construct_swap(input_str); + print_container(input_str); + std::cout << "reverse_string_construct_swap" << std::endl; + } + + if(nine == 1) { + reverse_string_no_tmp_iterators(input_str); + print_container(input_str); + std::cout << "reverse_string_no_tmp_iterators" << std::endl; + } + + return 0; +} |