Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #include <string>
- #include <deque>
- #include <vector>
- #include <list>
- #include <iterator>
- #include <functional>
- #include <limits>
- #include <cstdlib>
- #include <cassert>
- template< typename type >
- struct reversed_container
- {
- reversed_container(type && _container)
- : container_(std::forward< type >(_container))
- { ; }
- decltype(auto)
- begin() noexcept
- {
- return container_.rbegin();
- }
- decltype(auto)
- end() noexcept
- {
- return container_.rend();
- }
- private :
- type container_;
- };
- template< typename type >
- reversed_container< type >
- reverse(type && _container)
- {
- return std::forward< type >(_container);
- }
- using size_type = std::size_t;
- using boolean_type = bool;
- using symbol = std::string;
- using char_type = typename std::string::value_type;
- using symbols = std::list< symbol >;
- struct derivator_of_arguments
- {
- using symbol_cref = symbol;//std::reference_wrapper< symbol const >;
- using symbol_crefs = std::list< symbol_cref >;
- using symbols_pack = std::list< symbol_crefs >;
- using result_type = std::deque< symbols_pack >;
- symbols_pack
- tail(result_type const & _head, symbol const & _wrt) const
- {
- symbols_pack tail_;
- for (symbols_pack const & symbols_pack_ : reverse(_head)) {
- for (symbol_crefs const & symbol_crefs_ : reverse(symbols_pack_)) {
- tail_.push_front(symbol_crefs_);
- tail_.front().push_back(_wrt);
- }
- symbol const & wrt_ = symbols_pack_.back().back();
- if (wrt_ == _wrt) {
- return tail_;
- }
- }
- tail_.push_front({{_wrt}});
- return tail_;
- }
- result_type
- head(symbols const & _wrts) const
- {
- result_type head_;
- for (symbol const & wrt_ : _wrts) {
- head_.push_back(tail(head_, wrt_));
- }
- return head_;
- }
- };
- template< size_type n >
- struct test
- {
- static_assert(!(size_type('z' - 'a') + 1 < n)); // static_assert(n < 16); // x^x = 2^64 => x = 16
- using symbols_pack = typename derivator_of_arguments::symbols_pack;
- using letters_type = std::vector< char_type >;
- boolean_type
- operator () (letters_type const & _letters) const
- {
- symbols wrts_;
- for (char_type const & letter_ : _letters) {
- wrts_.emplace_back(letter_, 1);
- }
- derivator_of_arguments const derivator_of_arguments_{};
- auto const derived_arguments_ = derivator_of_arguments_.head(wrts_);
- {
- std::list< std::reference_wrapper< symbols_pack const > > orderer_(std::cbegin(derived_arguments_), std::cend(derived_arguments_));
- orderer_.sort(std::less< symbols_pack >{});
- auto const oend = std::cend(orderer_);
- if (std::adjacent_find(std::cbegin(orderer_), oend, std::equal_to< symbols_pack >{}) != oend) {
- return false;
- }
- }
- return true;
- }
- boolean_type
- operator () () const
- {
- letters_type mishmash_(n);
- size_type count_ = 1;
- for (size_type i = 0; i < n; ++i) {
- assert(!(std::numeric_limits< size_type >::max() / n < count_));
- count_ *= n;
- }
- for (size_type i = 0; i < count_; ++i) {
- size_type k = i;
- for (size_type j = 0; j < n; ++j) {
- mishmash_[j] = char_type('a' + (k % n));
- k /= n;
- }
- if (!operator () (mishmash_)) {
- return false;
- }
- }
- return true;
- }
- };
- int
- main()
- {
- test< 5 > const test_{};
- if (test_()) {
- std::cout << "Success!" << std::endl;
- return EXIT_SUCCESS;
- } else {
- std::cerr << "Failure!" << std::endl;
- return EXIT_FAILURE;
- }
- /*
- symbols wrts_;
- wrts_.emplace_back("x");
- wrts_.emplace_back("y");
- wrts_.emplace_back("y");
- wrts_.emplace_back("z");
- wrts_.emplace_back("y");
- derivator_of_arguments const derivator_of_arguments_{};
- auto const derived_arguments_ = derivator_of_arguments_.head(wrts_);
- std::cout << 1 << std::endl << std::endl; // argument pack itself
- for (auto const & i : derived_arguments_) { // its derivatives
- for (auto const & j : i) {
- std::copy(std::cbegin(j), std::cend(j), std::ostream_iterator< symbol const & >(std::cout, ""));
- std::cout << std::endl;
- }
- std::cout << std::endl;
- }
- #if 1
- { // check uniqueness
- using symbols_pack = typename derivator_of_arguments::symbols_pack;
- std::list< std::reference_wrapper< symbols_pack const > > orderer_(std::cbegin(derived_arguments_), std::cend(derived_arguments_));
- orderer_.sort(std::less< symbols_pack >{});
- auto const oend = std::cend(orderer_);
- if (std::adjacent_find(std::cbegin(orderer_), oend, std::equal_to< symbols_pack >{}) != oend) {
- std::cerr << "Failure!" << std::endl;
- return EXIT_FAILURE;
- }
- }
- #endif
- std::cout << "Success!" << std::endl;
- return EXIT_SUCCESS;*/
- }
Advertisement
Add Comment
Please, Sign In to add comment