Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- Author: Djeefther Souza Albuquerque ([email protected])
- Created at 20/09/2015
- */
- #include <iostream>
- #include <string>
- #include <vector>
- #include <algorithm>
- #include <functional>
- #include <utility>
- #include <memory>
- //rename the main(), main1(), main2(), main3(), main4() and main5() at the end to select wich of the test do you want to try
- #include "VectorFuncRange.h"
- struct Min {
- public:
- template<class T>
- T operator()(const T& a, const T& b) const {
- return std::min(a, b);
- }
- };
- struct Max{
- public:
- template<class T>
- T operator()(const T& a, const T& b) const {
- return std::max(a,b);
- }
- };
- struct Product {
- public:
- template<class T>
- T operator()(const T& a, const T& b) const {
- return a*b;
- }
- };
- struct MaxRef {
- public:
- template<class T>
- T operator()(const T& a, const T& b) const {
- return (*a > *b) ? a : b;
- }
- };
- struct MinRef {
- public:
- template<class T>
- T operator()(const T& a, const T& b) const {
- return (*a < *b) ? a : b;
- }
- };
- struct Sum {
- public:
- template<class T>
- T operator()(const T& a, const T& b) const {
- return a + b;
- }
- };
- template<class Func>
- void tokenizer(const std::string& text, Func func_token, std::vector<std::string>& words) {
- //Tokenizer the original string from a text every time func_token return true values
- words.clear();
- bool has_word = false;
- size_t begin_word = 0;
- for (size_t i = 0; i < text.size(); i++) {
- bool is_token = func_token(text[i]);
- if (is_token && has_word) {
- words.push_back(text.substr(begin_word,i-begin_word));
- has_word = false;
- }
- else if (!is_token && !has_word) {
- has_word = true;
- begin_word = i;
- }
- }
- if (has_word) {
- words.push_back(text.substr(begin_word));
- }
- }
- #define CATCH_INVALID_COMMAND() catch (...) { std::cout << "Invalid command\n"; }
- #define ELSE_INVALID_COMMAND() else std::cout << "Invalid command\n"
- #define IF_COMMAND(level, command) if (words[level] == command)
- #define ELIF_COMMAND(level, command) else if (words[level] == command)
- std::string rand_string(size_t size) {
- std::string str;
- for (size_t i = 0; i < size; i++) {
- char c = rand() % 26 + 'a' + (rand() & 1 ? 'A' - 'a' : 0);
- str += c;
- }
- return str;
- }
- template<class T,size_t r, size_t c>
- class Matrix {
- T data_[r][c];
- public:
- Matrix(){
- }
- static Matrix Identity() {
- Matrix ret;
- for (size_t i = 0; i < r; i++) {
- for (size_t j = 0; j < c; j++) {
- if (i == j) {
- ret[i][j] = 1;
- }
- else {
- ret[i][j] = 0;
- }
- }
- }
- return ret;
- }
- ~Matrix() {
- }
- const T* operator[](size_t i) const {
- return data_[i];
- }
- T* operator[](size_t i) {
- return data_[i];
- }
- template<size_t n>
- Matrix<T,r,n> operator*(const Matrix<T,c,n>& other) const {
- Matrix<T, r, n> ret;
- for (size_t i = 0; i < r; i++) {
- for (size_t j = 0; j < n; j++) {
- ret[i][j] = 0;
- for (size_t k = 0; k < c; k++) {
- ret[i][j] += (*this)[i][k] * other[k][j];
- }
- }
- }
- return ret;
- }
- bool operator ==(const Matrix& other) const {
- auto it = this->begin();
- auto it_end = this->end();
- auto other_it = other.begin();
- for (; it != it_end; it++, other_it++) {
- if (*it != *other_it)
- return false;
- }
- return true;
- }
- bool operator != (const Matrix& other) const {
- auto it = this->begin();
- auto it_end = this->end();
- auto other_it = other.begin();
- for (; it != it_end; it++, other_it++) {
- if (*it != *other_it)
- return true;
- }
- return false;
- }
- T* begin() {
- return ((T*)data_);
- }
- T* end() {
- return begin() + r*c;
- }
- inline const T* begin() const {
- return cbegin();
- }
- inline const T* end() const {
- return cend();
- }
- const T* cbegin() const {
- return ((T*)data_);
- }
- const T* cend() const {
- return begin() + r*c;
- }
- constexpr size_t rows() const {
- return r;
- }
- constexpr size_t cols() const {
- return c;
- }
- };
- template<class T, size_t r, size_t c>
- std::ostream& operator<<(std::ostream& stream, const Matrix<T, r, c>& obj) {
- for (size_t i = 0; i < obj.rows(); i++) {
- for (size_t j = 0; j < obj.cols(); j++) {
- stream << obj[i][j] << " ";
- }
- stream << "\n";
- }
- return stream;
- }
- int main1() {
- VectorFuncRange<int> vector;
- vector.func(Sum());
- std::string input_buffer;
- std::vector<std::string> words;
- bool show_print_debug = true;
- while (true) {
- std::getline(std::cin, input_buffer);
- tokenizer(input_buffer, isspace, words);
- //for (auto & i : words) {
- // std::cout << "-" << i << "-\n";
- //}
- //std::cout << "\n\n";
- if (words.size() > 0) {
- if (words[0] == "push_back") {
- try {
- int value = std::stoi(words.at(1));
- vector.push_back(value);
- }
- CATCH_INVALID_COMMAND();
- }
- else if (words[0] == "resize") {
- try {
- size_t new_size = std::stoul(words.at(1));
- if (words.size() > 2) {
- int value = std::stoi(words.at(2));
- vector.resize(new_size, value);
- }
- else {
- vector.resize(new_size);
- }
- }
- CATCH_INVALID_COMMAND();
- }
- else if (words[0] == "pop_back") {
- vector.pop_back();
- }
- else if (words[0] == "func") {
- if (words.size() > 1) {
- if (words[1] == "sum") {
- vector.func(Sum());
- }
- else if (words[1] == "product") {
- vector.func(Product());
- }
- else if (words[1] == "max") {
- vector.func(Max());
- }
- else if (words[1] == "min") {
- vector.func(Min());
- }
- ELSE_INVALID_COMMAND();
- }
- ELSE_INVALID_COMMAND();
- }
- else if (words[0] == "shrink_to_fit") {
- vector.shrink_to_fit();
- }
- else if (words[0] == "clear") {
- vector.clear();
- }
- else if (words[0] == "show") {
- show_print_debug = true;
- }
- else if (words[0] == "no_show") {
- show_print_debug = false;
- }
- else if (words[0] == "set") {
- try {
- size_t index = stoul(words.at(1));
- int value = stoi(words.at(2));
- vector.set(index, value);
- }
- CATCH_INVALID_COMMAND();
- }
- else if (words[0] == "get") {
- try {
- size_t index = stoul(words.at(1));
- std::cout << vector.get(index);
- std::cout << "\n";
- }
- CATCH_INVALID_COMMAND();
- }
- else if (words[0] == "print") {
- for (size_t i = 0; i < vector.size(); i++) {
- std::cout << vector[i] << " ";
- }
- std::cout << "\n";
- }
- else if (words[0] == "range") {
- try {
- size_t begin = std::stoul(words.at(1));
- size_t end = std::stoul(words.at(2));
- std::cout << vector.range_func(begin, end) << " \n";
- }
- CATCH_INVALID_COMMAND();
- }
- else if (words[0] == "all") {
- std::cout << vector.all_range_func() << "\n";
- }
- ELSE_INVALID_COMMAND();
- }
- if (show_print_debug) {
- std::cout << "\n";
- vector.print_debug();
- std::cout << "\n";
- }
- else {
- std::cout << "\ndone\n\n";
- }
- }
- }
- int main2() {
- VectorFuncRange<int> v;
- v.func(Sum());
- v.print_debug();
- for (int i = 0; i < 7; i++) {
- v.push_back(rand() % 10);
- v.print_debug();
- }
- std::cout << "\n############\n\n";
- for (int i = 0; i < 7; i++) {
- v[i] = (rand() % 10) + 10;
- v.print_debug();
- }
- std::cout << "\n############\n\n";
- v.resize(3);
- v.print_debug();
- v.resize(7, 1);
- v.print_debug();
- v.resize(10, -1);
- v.print_debug();
- v.resize(3, -1);
- v.print_debug();
- v.push_back(1000);
- v.print_debug();
- v.func(Product());
- v.print_debug();
- v.resize(8);
- v.print_debug();
- v.resize(4);
- v.resize(8, 2);
- v.print_debug();
- v.func(Sum());
- v.print_debug();
- v.push_back(-1);
- v.print_debug();
- v.resize(0);
- v.print_debug();
- v.resize(8, 2);
- v.print_debug();
- v.func(Product());
- v.print_debug();
- v[3] = 0;
- v.print_debug();
- v[3] = -1;
- v.print_debug();
- v[5] = 22;
- v.print_debug();
- v.func(Sum());
- v.print_debug();
- v.shrink_to_fit();
- v.print_debug();
- for (size_t i = 0; i < v.size(); i++) {
- std::cout << v[i] << " ";
- }
- getchar();
- return 0;
- }
- int main3() {
- VectorFuncRange<double, Product> v;
- for (int i = 0; i < 1000; i++) {
- v.push_back(rand() % 10 + 10);
- }
- v.print_debug();
- getchar();
- return 0;
- }
- int main4() {
- VectorFuncRange<std::string,Sum> v;
- for (int i = 0; i < 100; i++) {
- //v.push_back(rand() % 10);
- v.push_back(rand_string(rand()%10));
- }
- for (int i = 0; i < 10000000; i++) {
- size_t first = rand() % v.size();
- size_t lenght = rand() % (v.size() - first);
- size_t last = first + lenght;
- auto r1 = v.range_func_dumb(first, last);
- auto r2 = v.range_func(first, last);
- //auto r1 = r2;
- if (r1 != r2) {
- std::cout << "Test " << i << "\n";
- std::cout << "[" << first << "," << last << ")\n";
- std::cout << r1 << "\n";
- std::cout << r2 << "\n";
- std::cout << "\n";
- getchar();
- }
- if (i % 10000 == 0) {
- std::cout << "Test " << i << "...\n";
- }
- }
- return 0;
- }
- int main() {
- //A vector of Matrix with rand range products that match in linear and O(log n) evaluation
- //If they do not match the test is stoped and shows the difference
- VectorFuncRange<Matrix<int,3,3>, Product> v;
- v.neutral_element(Matrix<int,3,3>::Identity());
- for (int i = 0; i < 10000; i++) {
- //v.push_back(rand() % 10);
- Matrix<int, 3, 3> a;
- for (auto & i : a) {
- i = 1 + rand() % 10;
- }
- v.push_back(a);
- }
- for (int i = 0; i < 1000000; i++) {
- size_t first = rand() % v.size();
- size_t lenght = rand() % (v.size() - first);
- size_t last = first + lenght;
- auto r1 = v.range_func_dumb(first, last);
- auto r2 = v.range_func(first, last);
- // auto r1 = r2;
- if (r1 != r2) {
- std::cout << "Test " << i << "\n";
- std::cout << "[" << first << "," << last << ")\n";
- std::cout << r1 << "\n";
- std::cout << r2 << "\n";
- std::cout << "\n";
- getchar();
- }
- if (i % 10000 == 0) {
- std::cout << "Test " << i << "...\n";
- }
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment