From 0edcd0edade9802243100f95a1f352258e6d485c Mon Sep 17 00:00:00 2001 From: Ivan Smirnov Date: Fri, 19 Feb 2016 01:01:00 +0300 Subject: [PATCH] base array class --- array.h | 117 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ main.cpp | 21 ++-------- 2 files changed, 120 insertions(+), 18 deletions(-) create mode 100644 array.h diff --git a/array.h b/array.h new file mode 100644 index 0000000..79c3526 --- /dev/null +++ b/array.h @@ -0,0 +1,117 @@ +#pragma once +#include + +#include "common.h" +#include "random.h" +#include "named_arguments.h" + +namespace impl { + +typedef std::pair Range; + +template +class GenericArray : public std::vector { +public: + typedef std::vector Base; + + GenericArray() {} + GenericArray(const GenericArray&) = default; + GenericArray& operator=(const GenericArray&) = default; + /* implicit */ GenericArray(const Base& base) : + Base(base) + { } + template + GenericArray(Args... args) : + Base(args...) + { } + + static GenericArray random(size_t size, T max); + static GenericArray random(size_t size, T min, T max); + static GenericArray random(const Range& size, T max); + static GenericArray random(const Range& size, T min, T max); + + GenericArray& shuffle(); + GenericArray& reverse(); +}; + +template +GenericArray GenericArray::random(size_t size, T max) { + GenericArray result(size); + for (T& x: result) { + x = rnd.next(max); + } + return result; +} + +template +GenericArray GenericArray::random(size_t size, T min, T max) { + GenericArray result(size); + for (T& x: result) { + x = rnd.next(min, max); + } + return result; +} + +template +GenericArray GenericArray::random(const Range& size, T max) { + return GenericArray::random(rnd.next(size.first, size.second), max); +} + +template +GenericArray GenericArray::random(const Range& size, T min, T max) { + return GenericArray::random(rnd.next(size.first, size.second), min, max); +} + + +template +using ArrayRepresentation = std::pair, TraitMap>; + +template +ArrayRepresentation repr(const GenericArray& array, Traits... traits) { + return { array, collectTraits(traits...) }; +} + +template +std::ostream& operator<<(std::ostream& out, const ArrayRepresentation& repr) { + const GenericArray& array = repr.first; + const TraitMap& map = repr.second; + + if (!map.count("printSize") || (int)map.at("printSize")) { + out << array.size() << "\n"; + } + + int addition = map.count("addOne") && (int)map.at("addOne") ? 1 : 0; + const std::string sep = map.count("sep") ? (std::string)map.at("sep") : " "; + + bool first = true; + for (const T& x: array) { + if (!first) { + out << sep; + } else { + first = false; + } + out << x + addition; + } + return out; +} + +template +std::ostream& operator<<(std::ostream& out, const GenericArray& array) { + return out << repr(array); +} + + +typedef GenericArray Array; +typedef GenericArray Array64; +typedef GenericArray Arrayf; + +} // namespace impl + +using impl::Array; +using impl::Array64; +using impl::Arrayf; + +DECLARE_NAMED_PARAMETER(printSize); +DECLARE_NAMED_PARAMETER(sep); +DECLARE_NAMED_PARAMETER(addOne); + diff --git a/main.cpp b/main.cpp index 5dce6da..296abe1 100644 --- a/main.cpp +++ b/main.cpp @@ -2,26 +2,11 @@ #include "named_arguments.h" #include "random.h" +#include "array.h" using namespace std; -DECLARE_NAMED_PARAMETER(needParent); -DECLARE_NAMED_PARAMETER(sep); -DECLARE_NAMED_PARAMETER(another); - - -template -void func(Traits... traits) { - auto traitMap = impl::collectTraits(traits...); - - for (auto kv: traitMap) { - cout << kv.first << ": " << (int)kv.second << " " << (string)kv.second << endl; - } -} - - int main() { - for (int i = 0; i < 10; ++i) { - cout << rnd.next(10) << endl; - } + auto a = Arrayf::random({5, 10}, 100, 150); + cout << repr(a, $sep=" ", $printSize=false, $addOne=true) << endl; }