/strtk.hpp
C++ Header | 13496 lines | 12692 code | 752 blank | 52 comment | 847 complexity | 0a8accbcd9730aa9e8c7743f9e912017 MD5 | raw file
- /*
- *****************************************************************
- * String Toolkit Library *
- * *
- * Author: Arash Partow (2002-2013) *
- * URL: http://www.partow.net/programming/strtk/index.html *
- * *
- * Copyright notice: *
- * Free use of the String Toolkit Library is permitted under the *
- * guidelines and in accordance with the most current version of *
- * the Common Public License. *
- * http://www.opensource.org/licenses/cpl1.0.php *
- * *
- *****************************************************************
- */
- #ifndef INCLUDE_STRTK_HPP
- #define INCLUDE_STRTK_HPP
- #include <cstddef>
- #include <cctype>
- #include <cstring>
- #include <cerrno>
- #include <exception>
- #include <cmath>
- #include <iterator>
- #include <limits>
- #include <iostream>
- #include <fstream>
- #include <sstream>
- #include <utility>
- #include <algorithm>
- #include <string>
- #include <vector>
- #include <deque>
- #include <list>
- #include <set>
- #include <map>
- #include <stack>
- #include <queue>
- #ifndef strtk_no_tr1_or_boost
- #define strtk_enable_lexical_cast
- #define strtk_enable_random
- #define strtk_enable_regex
- #endif
- #ifdef strtk_enable_lexical_cast
- #include <boost/lexical_cast.hpp>
- #endif
- #ifdef strtk_enable_random
- // Requires definition of a TR1 compatible random library header
- //#include <random>
- #include <boost/random.hpp>
- #endif
- #ifdef strtk_enable_regex
- // Requires definition of a TR1 compatible regex library header
- //#include <regex>
- #include <boost/regex.hpp>
- #endif
- namespace strtk
- {
- static const std::size_t one_kilobyte = 1024;
- static const std::size_t one_megabyte = 1024 * one_kilobyte;
- static const std::size_t one_gigabyte = 1024 * one_megabyte;
- static const std::size_t magic_seed = 0xA5A5A5A5;
- template <typename Tokenizer, typename Function>
- inline std::size_t for_each_token(const std::string& buffer,
- Tokenizer& tokenizer,
- Function function)
- {
- std::size_t token_count = 0;
- tokenizer.assign(buffer);
- typename Tokenizer::iterator itr = tokenizer.begin();
- typename Tokenizer::const_iterator end = tokenizer.end();
- while (end != itr)
- {
- function(*itr);
- ++itr;
- ++token_count;
- }
- return token_count;
- }
- template <typename Function>
- inline std::size_t for_each_line(std::istream& stream,
- Function function,
- const std::size_t& buffer_size = one_kilobyte)
- {
- std::string buffer;
- buffer.reserve(buffer_size);
- std::size_t line_count = 0;
- while (std::getline(stream,buffer))
- {
- function(buffer);
- ++line_count;
- }
- return line_count;
- }
- template <typename Function>
- inline std::size_t for_each_line_n(std::istream& stream,
- const std::size_t& n,
- Function function,
- const std::size_t& buffer_size = one_kilobyte)
- {
- std::string buffer;
- buffer.reserve(buffer_size);
- std::size_t line_count = 0;
- while (std::getline(stream,buffer))
- {
- function(buffer);
- if (n == ++line_count)
- break;
- }
- return line_count;
- }
- template <typename Function>
- inline std::size_t for_each_line(const std::string& file_name,
- Function function,
- const std::size_t& buffer_size = one_kilobyte)
- {
- std::ifstream stream(file_name.c_str());
- if (stream)
- return for_each_line(stream,function,buffer_size);
- else
- return 0;
- }
- template <typename Function>
- inline std::size_t for_each_line_n(const std::string& file_name,
- const std::size_t& n,
- Function function,
- const std::size_t& buffer_size = one_kilobyte)
- {
- std::ifstream stream(file_name.c_str());
- if (stream)
- return for_each_line_n(stream,n,function,buffer_size);
- else
- return 0;
- }
- template <typename Function>
- inline std::size_t for_each_line_conditional(std::istream& stream,
- Function function,
- const std::size_t& buffer_size = one_kilobyte)
- {
- std::string buffer;
- buffer.reserve(buffer_size);
- std::size_t line_count = 0;
- while (std::getline(stream,buffer))
- {
- if (!function(buffer))
- {
- return line_count;
- }
- ++line_count;
- }
- return line_count;
- }
- template <typename Function>
- inline std::size_t for_each_line_n_conditional(std::istream& stream,
- const std::size_t& n,
- Function function,
- const std::size_t& buffer_size = one_kilobyte)
- {
- std::string buffer;
- buffer.reserve(buffer_size);
- std::size_t line_count = 0;
- while (std::getline(stream,buffer))
- {
- if (!function(buffer))
- {
- return line_count;
- }
- if (n == ++line_count)
- break;
- }
- return line_count;
- }
- template <typename Function>
- inline std::size_t for_each_line_conditional(const std::string& file_name,
- Function function,
- const std::size_t& buffer_size = one_kilobyte)
- {
- std::ifstream stream(file_name.c_str());
- if (stream)
- return for_each_line_conditional(stream,function,buffer_size);
- else
- return 0;
- }
- template <typename Function>
- inline std::size_t for_each_line_n_conditional(const std::string& file_name,
- const std::size_t& n,
- Function function,
- const std::size_t& buffer_size = one_kilobyte)
- {
- std::ifstream stream(file_name.c_str());
- if (stream)
- return for_each_line_n_conditional(stream,n,function,buffer_size);
- else
- return 0;
- }
- template <typename T>
- inline bool read_line_as_value(std::istream& stream,
- T& t,
- const std::size_t& buffer_size = one_kilobyte)
- {
- std::string buffer;
- buffer.reserve(buffer_size);
- if (!std::getline(stream,buffer))
- return false;
- return string_to_type_converter(buffer,t);
- }
- namespace details
- {
- struct not_supported_type_tag {};
- struct unsigned_type_tag {};
- struct signed_type_tag {};
- struct real_type_tag {};
- struct byte_type_tag {};
- struct bool_type_tag {};
- struct hex_number_type_tag {};
- struct hex_string_type_tag {};
- struct base64_type_tag {};
- struct ignore_token_type_tag {};
- struct stdstring_type_tag {};
- struct stdstring_range_type_tag {};
- struct value_type_tag {};
- struct sink_type_tag {};
- struct stl_seq_type_tag {};
- struct attribute_type_tag {};
- struct semantic_action_type_tag {};
- struct expect_type_tag {};
- struct like_type_tag {};
- struct inrange_type_tag {};
- struct trim_type_tag {};
- struct lcase_type_tag {};
- struct ucase_type_tag {};
- struct fillchararray_type_tag {};
- template <typename T>
- struct supported_conversion_to_type
- {
- typedef not_supported_type_tag type;
- };
- template <typename T>
- struct supported_conversion_from_type
- {
- typedef not_supported_type_tag type;
- };
- template <bool, typename T = void>
- struct enable_if {};
- template <typename T>
- struct enable_if<true, T> { typedef T type; };
- template <typename T>
- struct supported_iterator_type
- {
- enum { value = false };
- };
- template <typename T>
- struct is_valid_iterator
- {
- typedef typename details::enable_if<details::supported_iterator_type<T>::value,T>::type type;
- };
- template <typename T> struct numeric;
- template <typename T> inline std::size_t type_length(const T&);
- struct no_t {};
- struct yes_t {};
- template <typename T>
- struct is_pod
- {
- typedef no_t result_t;
- enum { result = false };
- };
- template <typename T>
- struct is_stl_container
- { typedef no_t result_t; };
- #define register_stl_container1(C) \
- template <typename T1, typename T2>struct is_stl_container<C<T1,T2> >{ typedef yes_t result_t; };
- #define register_stl_container2(C) \
- template <typename T1, typename T2, typename T3>struct is_stl_container<C<T1,T2,T3> >{ typedef yes_t result_t; };
- register_stl_container1(std::vector)
- register_stl_container1(std::deque)
- register_stl_container1(std::list)
- register_stl_container1(std::queue)
- register_stl_container1(std::stack)
- register_stl_container2(std::set)
- register_stl_container2(std::multiset)
- register_stl_container2(std::priority_queue)
- #undef register_stl_container1
- #undef register_stl_container2
- } // namespace details
- template <typename Iterator, typename T>
- inline bool string_to_type_converter(const Iterator begin, const Iterator end, T& t)
- {
- typedef typename details::is_valid_iterator<Iterator>::type itr_type;
- typename details::supported_conversion_to_type<T>::type type;
- Iterator itr = begin;
- return string_to_type_converter_impl(itr,end,t,type);
- }
- template <typename Iterator, typename T>
- inline bool string_to_type_converter(const std::pair<Iterator,Iterator>& range, T& t)
- {
- return string_to_type_converter(range.first,range.second,t);
- }
- template <typename T, typename Iterator>
- inline T string_to_type_converter(const Iterator begin, const Iterator end)
- {
- typedef typename details::is_valid_iterator<Iterator>::type itr_type;
- typename details::supported_conversion_to_type<T>::type type;
- T t;
- Iterator itr = begin;
- if (string_to_type_converter_impl(itr,end,t,type))
- return t;
- else
- throw std::invalid_argument("string_to_type_converter() - Failed to convert: " +
- std::string(begin,end));
- }
- template <typename T, typename Iterator>
- inline T string_to_type_converter(const std::pair<Iterator,Iterator>& range)
- {
- return string_to_type_converter<T>(range.first,range.second);
- }
- template <typename T>
- inline bool string_to_type_converter(const std::string& s, T& t)
- {
- return string_to_type_converter<const char*,T>(s.data(),s.data() + s.size(),t);
- }
- template <typename T>
- inline T string_to_type_converter(const std::string& s)
- {
- return string_to_type_converter<T>(s.data(),s.data() + s.size());
- }
- template <typename T>
- inline bool type_to_string(const T& t, std::string& s)
- {
- typename details::supported_conversion_from_type<T>::type type;
- return type_to_string_converter_impl(t,s,type);
- }
- template <typename T>
- inline std::string type_to_string(const T& t)
- {
- std::string s;
- if (type_to_string<T>(t,s))
- return s;
- else
- throw std::invalid_argument("type_to_string() - Failed to convert type to string");
- }
- #define strtk_begin_register_string_to_type \
- namespace strtk { namespace details {
- #define strtk_end_register_string_to_type \
- }}
- #define strtk_string_to_type_begin(Type) \
- namespace strtk { namespace details { template <typename Iterator> \
- inline bool string_to_type_converter_impl(const Iterator& begin, const Iterator& end, \
- Type& t, details::not_supported_type_tag&) {
- #define strtk_string_to_type_end()\
- }}}
- template <typename T,
- typename Allocator,
- template <typename,typename> class Sequence>
- inline std::size_t load_from_text_file(std::istream& stream,
- Sequence<T,Allocator>& sequence,
- const std::size_t& buffer_size = one_kilobyte)
- {
- if (!stream) return 0;
- std::string buffer;
- buffer.reserve(buffer_size);
- std::size_t line_count = 0;
- while (std::getline(stream,buffer))
- {
- ++line_count;
- sequence.push_back(string_to_type_converter<T>(buffer));
- }
- return line_count;
- }
- template <typename T,
- typename Comparator,
- typename Allocator>
- inline std::size_t load_from_text_file(std::istream& stream,
- std::set<T,Comparator,Allocator>& set,
- const std::size_t& buffer_size = one_kilobyte)
- {
- if (!stream) return 0;
- std::string buffer;
- buffer.reserve(buffer_size);
- std::size_t line_count = 0;
- while (std::getline(stream,buffer))
- {
- ++line_count;
- set.insert(string_to_type_converter<T>(buffer));
- }
- return line_count;
- }
- template <typename T,
- typename Comparator,
- typename Allocator>
- inline std::size_t load_from_text_file(std::istream& stream,
- std::multiset<T,Comparator,Allocator>& multiset,
- const std::size_t& buffer_size = one_kilobyte)
- {
- if (!stream) return 0;
- std::string buffer;
- buffer.reserve(buffer_size);
- std::size_t line_count = 0;
- while (std::getline(stream,buffer))
- {
- ++line_count;
- multiset.insert(string_to_type_converter<T>(buffer));
- }
- return line_count;
- }
- template <typename T, typename Container>
- inline std::size_t load_from_text_file(std::istream& stream,
- std::queue<T,Container>& queue,
- const std::size_t& buffer_size = one_kilobyte)
- {
- if (!stream) return 0;
- std::string buffer;
- buffer.reserve(buffer_size);
- std::size_t line_count = 0;
- while (std::getline(stream,buffer))
- {
- ++line_count;
- queue.push(string_to_type_converter<T>(buffer));
- }
- return line_count;
- }
- template <typename T, typename Container>
- inline std::size_t load_from_text_file(std::istream& stream,
- std::stack<T,Container>& stack,
- const std::size_t& buffer_size = one_kilobyte)
- {
- if (!stream) return 0;
- std::string buffer;
- buffer.reserve(buffer_size);
- std::size_t line_count = 0;
- while (std::getline(stream,buffer))
- {
- ++line_count;
- stack.push(string_to_type_converter<T>(buffer));
- }
- return line_count;
- }
- template <typename T,
- typename Container,
- typename Comparator>
- inline std::size_t load_from_text_file(std::istream& stream,
- std::priority_queue<T,Container,Comparator>& priority_queue,
- const std::size_t& buffer_size = one_kilobyte)
- {
- if (!stream) return 0;
- std::string buffer;
- buffer.reserve(buffer_size);
- std::size_t line_count = 0;
- while (std::getline(stream,buffer))
- {
- ++line_count;
- priority_queue.push(string_to_type_converter<T>(buffer));
- }
- return line_count;
- }
- template <typename T,
- typename Allocator,
- template <typename,typename> class Sequence>
- inline std::size_t load_from_text_file(const std::string& file_name,
- Sequence<T,Allocator>& sequence,
- const std::size_t& buffer_size = one_kilobyte)
- {
- std::ifstream stream(file_name.c_str());
- if (!stream)
- return 0;
- else
- return load_from_text_file(stream,sequence,buffer_size);
- }
- template <typename T,
- typename Comparator,
- typename Allocator>
- inline std::size_t load_from_text_file(const std::string& file_name,
- std::set<T,Comparator,Allocator>& set,
- const std::size_t& buffer_size = one_kilobyte)
- {
- std::ifstream stream(file_name.c_str());
- if (!stream)
- return 0;
- else
- return load_from_text_file(stream,set,buffer_size);
- }
- template <typename T,
- typename Comparator,
- typename Allocator>
- inline std::size_t load_from_text_file(const std::string& file_name,
- std::multiset<T,Comparator,Allocator>& multiset,
- const std::size_t& buffer_size = one_kilobyte)
- {
- std::ifstream stream(file_name.c_str());
- if (!stream)
- return 0;
- else
- return load_from_text_file(stream,multiset,buffer_size);
- }
- template <typename T, typename Container>
- inline std::size_t load_from_text_file(const std::string& file_name,
- std::queue<T,Container>& queue,
- const std::size_t& buffer_size = one_kilobyte)
- {
- std::ifstream stream(file_name.c_str());
- if (!stream)
- return 0;
- else
- return load_from_text_file(stream,queue,buffer_size);
- }
- template <typename T, typename Container>
- inline std::size_t load_from_text_file(const std::string& file_name,
- std::stack<T,Container>& stack,
- const std::size_t& buffer_size = one_kilobyte)
- {
- std::ifstream stream(file_name.c_str());
- if (!stream)
- return 0;
- else
- return load_from_text_file(stream,stack,buffer_size);
- }
- template <typename T,
- typename Container,
- typename Comparator>
- inline std::size_t load_from_text_file(const std::string& file_name,
- std::priority_queue<T,Container,Comparator>& priority_queue,
- const std::size_t& buffer_size = one_kilobyte)
- {
- std::ifstream stream(file_name.c_str());
- if (!stream)
- return 0;
- else
- return load_from_text_file(stream,priority_queue,buffer_size);
- }
- template <typename T,
- typename Allocator,
- template <typename,typename> class Sequence>
- inline std::size_t write_to_text_file(std::ostream& stream,
- const Sequence<T,Allocator>& sequence,
- const std::string& delimiter = "")
- {
- if (!stream) return 0;
- std::size_t count = 0;
- typename Sequence<T,Allocator>::const_iterator itr = sequence.begin();
- typename Sequence<T,Allocator>::const_iterator end = sequence.end();
- if (!delimiter.empty())
- {
- while (end != itr)
- {
- stream << (*itr) << delimiter;
- ++itr;
- ++count;
- }
- }
- else
- {
- while (end != itr)
- {
- stream << (*itr);
- ++itr;
- ++count;
- }
- }
- return count;
- }
- template <typename T,
- typename Comparator,
- typename Allocator>
- inline std::size_t write_to_text_file(std::ostream& stream,
- const std::set<T,Comparator,Allocator>& set,
- const std::string& delimiter = "")
- {
- if (!stream) return 0;
- std::size_t count = 0;
- typename std::set<T,Comparator,Allocator>::const_iterator itr = set.begin();
- typename std::set<T,Comparator,Allocator>::const_iterator end = set.end();
- if (!delimiter.empty())
- {
- while (end != itr)
- {
- stream << (*itr) << delimiter;
- ++itr;
- ++count;
- }
- }
- else
- {
- while (end != itr)
- {
- stream << (*itr);
- ++itr;
- ++count;
- }
- }
- return count;
- }
- template <typename T,
- typename Comparator,
- typename Allocator>
- inline std::size_t write_to_text_file(std::ostream& stream,
- const std::multiset<T,Comparator,Allocator>& multiset,
- const std::string& delimiter = "")
- {
- if (!stream) return 0;
- std::size_t count = 0;
- typename std::multiset<T,Comparator,Allocator>::const_iterator itr = multiset.begin();
- typename std::multiset<T,Comparator,Allocator>::const_iterator end = multiset.end();
- if (!delimiter.empty())
- {
- while (end != itr)
- {
- stream << (*itr) << delimiter;
- ++itr;
- ++count;
- }
- }
- else
- {
- while (end != itr)
- {
- stream << (*itr);
- ++itr;
- ++count;
- }
- }
- return count;
- }
- template <typename T,
- typename Allocator,
- template <typename,typename> class Sequence>
- inline std::size_t write_to_text_file(const std::string& file_name,
- const Sequence<T,Allocator>& sequence,
- const std::string& delimiter = "")
- {
- std::ofstream stream(file_name.c_str());
- if (!stream)
- return 0;
- else
- return write_to_text_file(stream,sequence,delimiter);
- }
- template <typename T,
- typename Comparator,
- typename Allocator>
- inline std::size_t write_to_text_file(const std::string& file_name,
- const std::set<T,Comparator,Allocator>& set,
- const std::string& delimiter = "")
- {
- std::ofstream stream(file_name.c_str());
- if (!stream)
- return 0;
- else
- return write_to_text_file(stream,set,delimiter);
- }
- template <typename T,
- typename Comparator,
- typename Allocator>
- inline std::size_t write_to_text_file(const std::string& file_name,
- const std::multiset<T,Comparator,Allocator>& multiset,
- const std::string& delimiter = "")
- {
- std::ofstream stream(file_name.c_str());
- if (!stream)
- return 0;
- else
- return write_to_text_file(stream,multiset,delimiter);
- }
- template <typename InputIterator, typename OutputIterator>
- inline void copy_n(InputIterator itr, std::size_t n, OutputIterator out)
- {
- while (n)
- {
- (*out) = (*itr);
- ++itr;
- ++out;
- --n;
- }
- }
- template <typename Predicate,
- typename InputIterator,
- typename OutputIterator>
- inline void copy_if(Predicate predicate,
- const InputIterator begin, const InputIterator end,
- OutputIterator out)
- {
- InputIterator itr = begin;
- while (end != itr)
- {
- if (predicate(*itr))
- {
- *(out++) = (*itr);
- }
- ++itr;
- }
- }
- template <typename Predicate,
- typename InputIterator,
- typename OutputIterator>
- inline InputIterator copy_while(Predicate predicate,
- const InputIterator begin, const InputIterator end,
- OutputIterator out)
- {
- InputIterator itr = begin;
- while (end != itr)
- {
- if (!predicate(*itr))
- return itr;
- *(out++) = *(itr++);
- }
- return end;
- }
- template <typename Predicate,
- typename InputIterator,
- typename OutputIterator>
- inline InputIterator copy_until(Predicate predicate,
- const InputIterator begin, const InputIterator end,
- OutputIterator out)
- {
- InputIterator itr = begin;
- while (end != itr)
- {
- if (predicate(*itr))
- return itr;
- *(out++) = *(itr++);
- }
- return end;
- }
- template <typename InputIterator, typename OutputIterator>
- inline void extract_unique(const InputIterator begin, const InputIterator end,
- OutputIterator out)
- {
- typedef typename std::iterator_traits<InputIterator>::value_type T;
- std::vector<T> buffer(begin,end);
- std::sort(buffer.begin(),buffer.end());
- std::unique_copy(buffer.begin(),buffer.end(),out);
- }
- template <typename Predicate, typename InputIterator>
- inline bool range_only_contains(Predicate predicate,
- const InputIterator begin,
- const InputIterator end)
- {
- InputIterator itr = begin;
- while (end != itr)
- {
- if (!predicate(*itr))
- {
- return false;
- }
- ++itr;
- }
- return true;
- }
- namespace range
- {
- template <typename T>
- class adapter
- {
- public:
- typedef T value_type;
- typedef T* iterator;
- typedef const iterator const_iterator;
- adapter(T* const begin, T* const end)
- : begin_(begin),
- end_(end)
- {}
- adapter(const std::pair<T*,T*>& r)
- : begin_(r.first),
- end_(r.second)
- {}
- adapter(T* const begin, const std::size_t length)
- : begin_(begin),
- end_(begin_ + length)
- {}
- inline iterator begin() const
- {
- return begin_;
- }
- inline iterator end() const
- {
- return end_;
- }
- inline std::size_t size() const
- {
- return std::distance(begin_,end_);
- }
- inline operator std::string() const
- {
- return stringify(begin_,end_);
- }
- inline const T& operator[](const std::size_t& index) const
- {
- return *(begin_ + index);
- }
- inline T& operator[](const std::size_t& index)
- {
- return *(begin_ + index);
- }
- private:
- template <typename Type>
- static inline std::string stringify(Type*,Type*)
- {
- static std::string result = "";
- return result;
- }
- static inline std::string stringify(const char* begin, const char* end)
- {
- return std::string(begin,end);
- }
- iterator begin_;
- iterator end_;
- };
- typedef adapter<const char> string;
- typedef adapter<const unsigned char> ustring;
- template <typename T>
- inline adapter<T> type(const T* begin, const T* end)
- {
- return adapter<T>(begin,end);
- }
- template <typename T, std::size_t N>
- inline adapter<T> type(const T (&t)[N])
- {
- return adapter<T>(t,N);
- }
- static inline adapter<const char> type(const std::string& s)
- {
- return adapter<const char>(s.data(),s.size());
- }
- template <typename T,
- typename Allocator,
- template <typename,typename> class Sequence>
- inline adapter<typename Sequence<T,Allocator>::iterator> type(const Sequence<T,Allocator>& seq)
- {
- return adapter<typename Sequence<T,Allocator>::iterator>(seq.begin(),seq.end());
- }
- inline std::string as_string(const adapter<const char>& a)
- {
- return std::string(a.begin(),a.end());
- }
- inline std::string as_string(const adapter<const unsigned char>& a)
- {
- return std::string(a.begin(),a.end());
- }
- } // namespace range
- template <typename T>
- struct single_delimiter_predicate
- {
- public:
- typedef T value_type;
- single_delimiter_predicate(const T& d)
- : delimiter_(d)
- {}
- inline bool operator()(const T& d) const
- {
- return delimiter_ == d;
- }
- private:
- single_delimiter_predicate<T>& operator=(const single_delimiter_predicate<T>&);
- const T delimiter_;
- };
- template <typename T>
- struct multiple_delimiter_predicate
- {
- public:
- typedef T value_type;
- multiple_delimiter_predicate(const T* d_begin, const T* d_end)
- : length_(std::distance(d_begin,d_end)),
- delimiter_((length_ <= sbo_buffer_size) ? sbo_buffer : new T[length_]),
- delimiter_end_(delimiter_ + length_)
- {
- std::copy(d_begin,d_end, delimiter_);
- }
- multiple_delimiter_predicate(const T d[], const std::size_t& length)
- : length_(length),
- delimiter_((length_ <= sbo_buffer_size) ? sbo_buffer : new T[length_]),
- delimiter_end_(delimiter_ + length_)
- {
- std::copy(d,d + length, delimiter_);
- }
- template <typename Iterator>
- multiple_delimiter_predicate(const Iterator begin, const Iterator end)
- : length_(std::distance(begin,end)),
- delimiter_((length_ <= sbo_buffer_size) ? sbo_buffer : new T[length_]),
- delimiter_end_(delimiter_ + length_)
- {
- //static_assert(T == std::iterator_traits<Iterator>::value_type);
- std::copy(begin,end, delimiter_);
- }
- template <typename Type>
- multiple_delimiter_predicate(const range::adapter<Type>& r)
- : length_(std::distance(r.begin(),r.end())),
- delimiter_((length_ <= sbo_buffer_size) ? sbo_buffer : new T[length_]),
- delimiter_end_(delimiter_ + length_)
- {
- //static_assert(T == std::iterator_traits<Iterator>::value_type);
- std::copy(r.begin(),r.end(), delimiter_);
- }
- ~multiple_delimiter_predicate()
- {
- if (length_ > sbo_buffer_size)
- {
- delete[] delimiter_;
- }
- }
- inline bool operator()(const T& d) const
- {
- return (std::find(delimiter_,delimiter_end_,d) != delimiter_end_);
- }
- private:
- multiple_delimiter_predicate(const multiple_delimiter_predicate<T>& mdp);
- multiple_delimiter_predicate& operator=(const multiple_delimiter_predicate<T>& mdp);
- std::size_t length_;
- T* delimiter_;
- T* delimiter_end_;
- enum { sbo_buffer_size = 32 };
- T sbo_buffer[sbo_buffer_size];
- };
- struct multiple_char_delimiter_predicate
- {
- public:
- template <typename Iterator>
- multiple_char_delimiter_predicate(const Iterator begin, const Iterator end)
- {
- setup_delimiter_table(begin,end);
- }
- multiple_char_delimiter_predicate(const std::string& s)
- {
- setup_delimiter_table(s.data(),s.data() + s.size());
- }
- inline bool operator()(const unsigned char& c) const
- {
- return (delimiter_table_[c]);
- }
- inline bool operator()(const char& c) const
- {
- return operator()(static_cast<unsigned char>(c));
- }
- private:
- static const std::size_t table_size = 256;
- template <typename Iterator>
- inline void setup_delimiter_table(const Iterator begin, const Iterator end)
- {
- std::fill_n(delimiter_table_,table_size,false);
- for (Iterator itr = begin; itr != end; ++itr)
- {
- delimiter_table_[static_cast<unsigned char>(*itr)] = true;
- }
- }
- bool delimiter_table_[table_size];
- };
- namespace details
- {
- template <typename Allocator,
- template <typename,typename> class Sequence>
- struct index_remover_impl
- {
- typedef Sequence<std::size_t,Allocator> sequence_t;
- index_remover_impl(const sequence_t& sequence)
- : itr_(sequence.begin()),
- end_(sequence.end()),
- current_index_(0),
- check_(true)
- {}
- template <typename T>
- inline bool operator()(const T&)
- {
- if (check_)
- {
- if (current_index_++ == *itr_)
- {
- if (end_ == ++itr_)
- {
- check_ = false;
- }
- return true;
- }
- }
- return false;
- }
- typename sequence_t::const_iterator itr_;
- typename sequence_t::const_iterator end_;
- std::size_t current_index_;
- bool check_;
- };
- }
- template <typename Allocator,
- template <typename,typename> class Sequence>
- inline details::index_remover_impl<Allocator,Sequence> index_remover(const Sequence<std::size_t,Allocator>& sequence)
- {
- return details::index_remover_impl<Allocator,Sequence>(sequence);
- }
- template <typename Iterator, typename Predicate>
- inline std::size_t remove_inplace(Predicate predicate,
- Iterator begin,
- Iterator end)
- {
- Iterator itr1 = begin;
- Iterator itr2 = begin;
- std::size_t removal_count = 0;
- while (end != itr1)
- {
- if (predicate(*itr1))
- {
- ++itr1;
- ++removal_count;
- }
- else
- {
- if (itr1 != itr2)
- {
- (*itr2) = (*itr1);
- }
- ++itr1;
- ++itr2;
- }
- }
- return removal_count;
- }
- template <typename T, typename Predicate>
- inline std::size_t remove_inplace(Predicate predicate, const range::adapter<T>& r)
- {
- return remove_inplace(predicate,r.begin(),r.end());
- }
- template <typename Predicate,
- typename T,
- typename Allocator,
- template <typename,typename> class Sequence>
- inline std::size_t remove_inplace(Predicate predicate, Sequence<T,Allocator>& sequence)
- {
- const std::size_t removal_count = remove_inplace(predicate,sequence.begin(),sequence.end());
- sequence.resize(sequence.size() - removal_count);
- return removal_count;
- }
- inline void remove_inplace(const std::string::value_type c, std::string& s)
- {
- const std::size_t removal_count = remove_inplace(single_delimiter_predicate<std::string::value_type>(c),
- const_cast<char*>(s.data()),
- const_cast<char*>(s.data() + s.size()));
- if (removal_count > 0)
- {
- s.resize(s.size() - removal_count);
- }
- }
- template <typename Predicate>
- inline void remove_inplace(Predicate predicate, std::string& s)
- {
- const std::size_t removal_count = remove_inplace(predicate,
- const_cast<char*>(s.data()),
- const_cast<char*>(s.data() + s.size()));
- if (removal_count > 0)
- {
- s.resize(s.size() - removal_count);
- }
- }
- template <typename Iterator, typename Predicate>
- inline std::size_t remove_consecutives_inplace(Predicate predicate,
- Iterator begin,
- Iterator end)
- {
- if (0 == std::distance(begin,end)) return 0;
- Iterator itr1 = begin;
- Iterator itr2 = begin;
- typename std::iterator_traits<Iterator>::value_type prev = *begin;
- std::size_t removal_count = 0;
- ++itr1;
- ++itr2;
- while (end != itr1)
- {
- while ((end != itr1) && (!predicate(*itr1) || !predicate(prev)))
- {
- if (itr1 != itr2)
- {
- (*itr2) = (*itr1);
- }
- prev = (*itr1);
- ++itr1;
- ++itr2;
- }
- while ((end != itr1) && predicate(*itr1))
- {
- ++itr1;
- ++removal_count;
- }
- }
- return removal_count;
- }
- template <typename T, typename Predicate>
- inline std::size_t remove_consecutives_inplace(Predicate predicate, const range::adapter<T>& r)
- {
- return remove_consecutives_inplace(predicate,r.begin(),r.end());
- }
- inline void remove_consecutives_inplace(const std::string::value_type c, std::string& s)
- {
- if (s.empty()) return;
- const std::size_t removal_count = remove_consecutives_inplace(single_delimiter_predicate<std::string::value_type>(c),
- const_cast<char*>(s.data()),
- const_cast<char*>(s.data() + s.size()));
- if (removal_count > 0)
- {
- s.resize(s.size() - removal_count);
- }
- }
- inline void remove_consecutives_inplace(const std::string& rem_chars, std::string& s)
- {
- if (s.empty()) return;
- const std::size_t removal_count = remove_consecutives_inplace(multiple_char_delimiter_predicate(rem_chars),
- const_cast<char*>(s.data()),
- const_cast<char*>(s.data() + s.size()));
- if (removal_count > 0)
- {
- s.resize(s.size() - removal_count);
- }
- }
- namespace details
- {
- #if (defined(__MINGW32_VERSION)) ||\
- (defined(__APPLE__) && (__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 1070)) ||\
- (defined(_WIN32) && (_MSC_VER < 1400))
- inline std::size_t strnlength(const char* s, const std::size_t& n)
- {
- const char *end = reinterpret_cast<const char*>(memchr(s, '\0', n));
- return end ? (size_t) (end - s) : n;
- }
- #else
- inline std::size_t strnlength(const char* s, const std::size_t& n)
- {
- return strnlen(s,n);
- }
- #endif
- }
- inline void remove_consecutives_inplace(const char* rem_chars, std::string& s)
- {
- if (s.empty()) return;
- const std::size_t removal_count = remove_consecutives_inplace(multiple_char_delimiter_predicate(
- rem_chars,
- rem_chars + details::strnlength(rem_chars,256)),
- const_cast<char*>(s.data()),
- const_cast<char*>(s.data() + s.size()));
- if (removal_count > 0)
- {
- s.resize(s.size() - removal_count);
- }
- }
- template <typename Predicate>
- inline void remove_consecutives_inplace(Predicate predicate, std::string& s)
- {
- if (s.empty()) return;
- const std::size_t removal_count = remove_consecutives_inplace(predicate,
- const_cast<char*>(s.data()),
- const_cast<char*>(s.data() + s.size()));
- if (removal_count > 0)
- {
- s.resize(s.size() - removal_count);
- }
- }
- template <typename Iterator>
- inline std::size_t remove_consecutives_inplace(Iterator begin, Iterator end)
- {
- if (0 == std::distance(begin,end)) return 0;
- Iterator itr1 = begin; ++itr1;
- Iterator itr2 = begin; ++itr2;
- typename std::iterator_traits<Iterator>::value_type prev = *begin;
- std::size_t removal_count = 0;
- while (end != itr1)
- {
- while ((end != itr1) && (prev != (*itr1)))
- {
- if (itr1 != itr2)
- {
- (*itr2) = (*itr1);
- }
- prev = (*itr1);
- ++itr1;
- ++itr2;
- }
- while ((end != itr1) && (prev == (*itr1)))
- {
- ++itr1;
- ++removal_count;
- }
- }
- return removal_count;
- }
- template <typename T>
- inline std::size_t remove_consecutives_inplace(const range::adapter<T>& r)
- {
- return remove_consecutives_inplace(r.begin(),r.end());
- }
- template <typename T,
- typename Allocator,
- template <typename,typename> class Sequence>
- inline void remove_consecutives_inplace(Sequence<T,Allocator>& sequence)
- {
- const std::size_t removal_count = remove_consecutives_inplace(sequence.begin(),sequence.end());
- sequence.resize(sequence.size() - removal_count);
- }
- inline void remove_consecutives_inplace(std::string& s)
- {
- std::size_t removal_count = remove_consecutives_inplace(const_cast<char*>(s.data()),
- const_cast<char*>(s.data() + s.size()));
- if (removal_count > 0)
- {
- s.resize(s.size() - removal_count);
- }
- }
- inline std::string remove_duplicates(const std::string& str)
- {
- std::string::value_type table[0xFF];
- std::fill_n(table,0xFF,static_cast<char>(0));
- std::string result;
- result.reserve(str.size());
- for (std::size_t i = 0; i < str.size(); ++i)
- {
- const char c = str[i];
- if (0 == table[static_cast<std::size_t>(c)])
- {
- table[static_cast<std::size_t>(c)] = 0x01;
- result += c;
- }
- }
- return result;
- }
- inline std::string remove_duplicates_inplace(std::string& str)
- {
- return remove_duplicates(str);
- }
- template <typename Iterator, typename Predicate>
- inline std::size_t remove_trailing(Predicate predicate,
- Iterator begin,
- Iterator end)
- {
- const std::size_t length = std::distance(begin,end);
- if (0 == length)
- return 0;
- Iterator itr = begin + (length - 1);
- std::size_t removal_count = 0;
- while ((begin != itr) && predicate(*itr))
- {
- --itr;
- ++removal_count;
- }
- return removal_count;
- }
- template <typename T, typename Predicate>
- inline std::size_t remove_trailing(Predicate predicate, const range::adapter<T>& r)
- {
- return remove_trailing(predicate,r.begin(),r.end());
- }
- inline void remove_trailing(const std::string::value_type c, std::string& s)
- {
- if (s.empty()) return;
- const std::size_t removal_count = remove_trailing(single_delimiter_predicate<std::string::value_type>(c),
- const_cast<char*>(s.data()),
- const_cast<char*>(s.data() + s.size()));
- if (removal_count > 0)
- {
- s.resize(s.size() - removal_count);
- }
- }
- inline void remove_trailing(const std::string& rem_chars, std::string& s)
- {
- if (s.empty()) return;
- const std::size_t removal_count = remove_trailing(multiple_char_delimiter_predicate(rem_chars),
- const_cast<char*>(s.data()),
- const_cast<char*>(s.data() + s.size()));
- if (removal_count > 0)
- {
- s.resize(s.size() - removal_count);
- }
- }
- inline void remove_trailing(const char* rem_chars, std::string& s)
- {
- const std::size_t removal_count = remove_trailing(multiple_char_delimiter_predicate(
- rem_chars,
- rem_chars + details::strnlength(rem_chars,256)),
- const_cast<char*>(s.data()),
- const_cast<char*>(s.data() + s.size()));
- if (removal_count > 0)
- {
- s.resize(s.size() - removal_count);
- }
- }
- template <typename Predicate>
- inline void remove_trailing(Predicate predicate, std::string& s)
- {
- if (s.empty()) return;
- const std::size_t removal_count = remove_trailing(predicate,
- const_cast<char*>(s.data()),
- const_cast<char*>(s.data() + s.size()));
- if (removal_count > 0)
- {
- s.resize(s.size() - removal_count);
- }
- }
- template <typename Iterator, typename Predicate>
- inline std::size_t remove_leading(Predicate predicate,
- Iterator begin,
- Iterator end)
- {
- const std::size_t length = std::distance(begin,end);
- if (0 == length)
- return 0;
- Iterator itr = begin;
- std::size_t removal_count = 0;
- while ((end != itr) && predicate(*itr))
- {
- ++itr;
- ++removal_count;
- }
- std::copy(itr,end,begin);
- return removal_count;
- }
- template <typename T, typename Predicate>
- inline std::size_t remove_leading(Predicate predicate, const range::adapter<T>& r)
- {
- return remove_leading(predicate,r.begin(),r.end());
- }
- inline void remove_leading(const std::string::value_type c, std::string& s)
- {
- if (s.empty()) return;
- const std::size_t removal_count = remove_leading(single_delimiter_predicate<std::string::value_type>(c),
- const_cast<char*>(s.data()),
- const_cast<char*>(s.data() + s.size()));
- if (removal_count > 0)
- {
- s.resize(s.size() - removal_count);
- }
- }
- inline void remove_leading(const std::string& rem_chars, std::string& s)
- {
- if (s.empty()) return;
- const std::size_t removal_count = remove_leading(multiple_char_delimiter_predicate(rem_chars),
- const_cast<char*>(s.data()),
- const_cast<char*>(s.data() + s.size()));
- if (removal_count > 0)
- {
- s.resize(s.size() - removal_count);
- }
- }
- inline void remove_leading(const char* rem_chars, std::string& s)
- {
- if (s.empty()) return;
- const std::size_t removal_count = remove_leading(multiple_char_delimiter_predicate(
- rem_chars,
- rem_chars + details::strnlength(rem_chars,256)),
- const_cast<char*>(s.data()),
- const_cast<char*>(s.data() + s.size()));
- if (removal_count > 0)
- {
- s.resize(s.size() - removal_count);
- }
- }
- inline void remove_leading_trailing(const std::string& rem_chars, std::string& s)
- {
- remove_leading(rem_chars,s);
- remove_trailing(rem_chars,s);
- }
- template <typename Predicate>
- inline void remove_leading(Predicate predicate, std::string& s)
- {
- if (s.empty()) return;
- const std::size_t removal_count = remove_leading(predicate,
- const_cast<char*>(s.data()),
- const_cast<char*>(s.data() + s.size()));
- if (removal_count > 0)
- {
- s.resize(s.size() - removal_count);
- }
- }
- template <typename Allocator,
- template <typename,typename> class Sequence>
- void remove_empty_strings(Sequence<std::string,Allocator>& seq)
- {
- struct is_empty { static inline bool check(const std::string& s) { return s.empty(); } };
- seq.erase(std::remove_if(seq.begin(),seq.end(),is_empty::check),seq.end());
- }
- template <typename Allocator>
- void remove_empty_strings(std::list<std::string,Allocator>& l)
- {
- struct is_empty { static inline bool check(const std::string& s) { return s.empty(); } };
- l.remove_if(is_empty::check);
- }
- template <typename Comparator, typename Allocator>
- void remove_empty_strings(std::set<std::string,Comparator,Allocator>& set)
- {
- struct is_empty { static inline bool check(const std::string& s) { return s.empty(); } };
- typename std::set<std::string,Comparator,Allocator>::iterator itr = set.begin();
- while (set.end() != itr)
- {
- if ((*itr).empty())
- set.erase(itr++);
- else
- ++itr;
- }
- }
- template <typename Comparator, typename Allocator>
- void remove_empty_strings(std::multiset<std::string,Comparator,Allocator>& set)
- {
- struct is_empty { static inline bool check(const std::string& s) { return s.empty(); } };
- typename std::multiset<std::string,Comparator,Allocator>::iterator itr = set.begin();
- while (set.end() != itr)
- {
- if ((*itr).empty())
- set.erase(itr++);
- else
- ++itr;
- }
- }
- template <typename Iterator>
- inline void replace(const typename std::iterator_traits<Iterator>::value_type& c1,
- const typename std::iterator_traits<Iterator>::value_type& c2,
- const Iterator begin,
- const Iterator end)
- {
- for (Iterator itr = begin; end != itr; ++itr)
- {
- if (c1 == (*itr))
- {
- (*itr) = c2;
- }
- }
- }
- inline void replace(const std::string::value_type& c0,
- const std::string::value_type& c1,
- std::string& s)
- {
- replace(c0,c1,const_cast<char*>(s.data()),const_cast<char*>(s.data() + s.size()));
- }
- template <typename T>
- inline void replace(const T& c1, const T& c2, const range::adapter<T>& r)
- {
- replace(c1,c2,r.begin(),r.end());
- }
- inline void replace_pattern(const std::string& s, // input
- const std::string& p, // pattern
- const std::string& r, // replacement
- std::string& n)
- {
- if (p.empty() || (p == r))
- {
- n.assign(s);
- return;
- }
- const std::size_t p_size = p.size();
- const std::size_t r_size = r.size();
- int inc = static_cast<int>(r_size) - static_cast<int>(p_size);
- std::size_t pos = 0;
- std::vector<std::size_t> delta_list;
- delta_list.reserve(std::min<std::size_t>(32,(s.size() / p_size) + 1));
- while (std::string::npos != (pos = s.find(p,pos)))
- {
- delta_list.push_back(pos);
- pos += p_size;
- }
- if (delta_list.empty())
- {
- n.assign(s);
- return;
- }
- n.resize(delta_list.size() * inc + s.size(), 0x00);
- char* n_itr = const_cast<char*>(n.data());
- const char* s_end = s.data() + s.size();
- const char* s_itr = s.data();
- const char* r_begin = r.data();
- const char* r_end = r.data() + r_size;
- const std::size_t delta_list_size = delta_list.size();
- std::size_t i = 0;
- std::size_t delta = delta_list[0];
- for ( ; ; )
- {
- std::copy(s_itr, s_itr + delta, n_itr);
- s_itr += p_size + delta;
- n_itr += delta;
- std::copy(r_begin, r_end, n_itr);
- n_itr += r_size;
- if (++i >= delta_list_size)
- break;
- delta = delta_list[i] - (delta_list[i - 1] + p_size);
- }
- if (s_end != s_itr)
- {
- std::copy(s_itr, s_end, n_itr);
- }
- }
- template <typename InputIterator, typename OutputIterator>
- inline std::size_t replace_pattern(const InputIterator s_begin, const InputIterator s_end, // input
- const InputIterator p_begin, const InputIterator p_end, // pattern
- const InputIterator r_begin, const InputIterator r_end, // replacement
- OutputIterator out)
- {
- typedef typename std::iterator_traits<InputIterator>::value_type T;
- InputIterator s_itr = s_begin;
- InputIterator r_itr = r_begin;
- InputIterator p_itr = p_begin;
- const std::size_t p_size = std::distance(p_begin,p_end);
- const std::size_t r_size = std::distance(r_begin,r_end);
- if ((0 == p_size) || ((p_size == r_size) && std::equal(p_begin,p_end,r_begin)))
- {
- std::copy(s_begin,s_end,out);
- return std::distance(s_begin,s_end);
- }
- std::size_t pos = 0;
- std::size_t prev_pos = 0;
- std::size_t count = 0;
- std::size_t new_size = std::distance(s_begin,s_end);
- int inc = r_size - p_size;
- InputIterator temp_s_itr = s_itr;
- while (s_end != s_itr)
- {
- /*
- Need to replace the following search code with
- Knuth-Pratt-Morris or Boyer-Moore string search
- algorithms.
- */
- bool found = true;
- p_itr = p_begin;
- temp_s_itr = s_itr;
- while ((p_end != p_itr) && (s_end != temp_s_itr))
- {
- if (*(temp_s_itr++) != *(p_itr++))
- {
- found = false;
- break;
- }
- }
- if (found && (p_itr == p_end))
- {
- ++count;
- new_size += inc;
- s_itr = temp_s_itr;
- }
- else
- ++s_itr;
- }
- s_itr = s_begin;
- p_itr = p_begin;
- pos = 0;
- prev_pos = 0;
- temp_s_itr = s_itr;
- while (0 < count)
- {
- p_itr = p_begin;
- bool found = true;
- InputIterator pattern_start = temp_s_itr;
- while ((p_end != p_itr) && (s_end != temp_s_itr))
- {
- if (*(temp_s_itr++) != *(p_itr++))
- {
- found = false;
- temp_s_itr = pattern_start;
- ++temp_s_itr;
- break;
- }
- }
- if (!found || (p_itr != p_end)) continue;
- pos = std::distance(s_begin,temp_s_itr) - p_size;
- int diff = pos - prev_pos;
- std::copy(s_itr,s_itr + diff, out);
- s_itr = temp_s_itr;
- std::copy(r_itr,r_end, out);
- pos += p_size;
- prev_pos = pos;
- --count;
- }
- std::copy(s_itr,s_end,out);
- return new_size;
- }
- inline void remove_pattern(const std::string& s,
- const std::string& p,
- std::string& n)
- {
- static const std::string r("");
- replace_pattern(s,p,r,n);
- }
- inline void sort(std::string& s)
- {
- std::sort(s.begin(),s.end());
- }
- template <typename Iterator>
- inline bool match(const Iterator pattern_begin,
- const Iterator pattern_end,
- const Iterator data_begin,
- const Iterator data_end,
- const typename std::iterator_traits<Iterator>::value_type& zero_or_more,
- const typename std::iterator_traits<Iterator>::value_type& zero_or_one)
- {
- /*
- Credits: Adapted from code by Jack Handy (2001)
- */
- if (0 == std::distance(data_begin,data_end)) return false;
- Iterator d_itr = data_begin;
- Iterator p_itr = pattern_begin;
- Iterator c_itr = data_begin;
- Iterator m_itr = data_begin;
- while ((data_end != d_itr) && (zero_or_more != (*p_itr)))
- {
- if (((*p_itr) != (*d_itr)) && (zero_or_one != (*p_itr)))
- {
- return false;
- }
- ++p_itr;
- ++d_itr;
- }
- while (data_end != d_itr)
- {
- if (zero_or_more == (*p_itr))
- {
- if (pattern_end == (++p_itr))
- {
- return true;
- }
- m_itr = p_itr;
- c_itr = d_itr;
- ++c_itr;
- }
- else if (((*p_itr) == (*d_itr)) || (zero_or_one == (*p_itr)))
- {
- ++p_itr;
- ++d_itr;
- }
- else
- {
- p_itr = m_itr;
- d_itr = c_itr++;
- }
- }
- while ((p_itr != pattern_end) && (zero_or_more == (*p_itr))) ++p_itr;
- return (p_itr == pattern_end);
- }
- inline bool match(const std::string& wild_card,
- const std::string& str)
- {
- /*
- * : Zero or more match
- ? : Zero or one match
- */
- return match(wild_card.data(),
- wild_card.data() + wild_card.size(),
- str.data(),
- str.data() + str.size(),
- '*',
- '?');
- }
- inline bool imatch_char(const char c1, const char c2)
- {
- return std::toupper(c1) == std::toupper(c2);
- }
- template <typename InputIterator>
- inline bool imatch(const InputIterator begin1, const InputIterator end1,
- const InputIterator begin2, const InputIterator end2)
- {
- typedef typename details::is_valid_iterator<InputIterator>::type itr_type;
- if (std::distance(begin1,end1) != std::distance(begin2,end2))
- {
- return false;
- }
- InputIterator itr1 = begin1;
- InputIterator itr2 = begin2;
- while (end1 != itr1)
- {
- //if (std::toupper(*itr1, std::locale::classic()) != std::toupper(*it2, std::locale::classic()))
- if (std::toupper(*itr1) != std::toupper(*itr2))
- {
- return false;
- }
- ++itr1;
- ++itr2;
- }
- return true;
- }
- template <typename T>
- inline bool imatch(const range::adapter<T>& r1, const range::adapter<T>& r2)
- {
- return imatch(r1.begin(),r1.end(),r2.begin(),r2.end());
- }
- inline bool imatch(const std::string& s1, const std::string& s2)
- {
- return imatch(s1.data(),
- s1.data() + s1.size(),
- s2.data(),
- s2.data() + s2.size());
- }
- template <typename Iterator>
- inline Iterator imatch(const std::string& s, const Iterator begin, const Iterator end)
- {
- for (const std::string* itr = begin; end != itr; ++itr)
- {
- if (imatch(s,*itr))
- {
- return itr;
- }
- }
- return end;
- }
- template <typename Allocator,
- template <typename,typename> class Sequence>
- inline bool imatch(const std::string& s, const Sequence<std::string,Allocator>& sequence)
- {
- return (sequence.end() != imatch(s,sequence.begin(),sequence.end()));
- }
- template <typename Comparator, typename Allocator>
- inline bool imatch(const std::string& s, const std::set<std::string,Comparator,Allocator>& set)
- {
- return imatch(s,set.begin(),set.end());
- }
- template <typename Comparator, typename Allocator>
- inline bool imatch(const std::string& s, const std::multiset<std::string,Comparator,Allocator>& multiset)
- {
- return imatch(s,multiset.begin(),multiset.end());
- }
- template <typename Iterator, typename OutputIterator>
- inline std::size_t find_all(const Iterator pattern_begin,
- const Iterator pattern_end,
- const Iterator begin,
- const Iterator end,
- OutputIterator out)
- {
- Iterator itr = begin;
- const std::size_t pattern_length = std::distance(pattern_begin,pattern_end);
- std::size_t match_count = 0;
- while (end != (itr = std::search(itr, end, pattern_begin, pattern_end)))
- {
- (*out) = std::make_pair(itr,itr + pattern_length);
- itr += pattern_length;
- ++out;
- ++match_count;
- }
- return match_count;
- }
- template <typename Iterator,
- typename Range,
- typename Allocator,
- template <typename,typename> class Sequence>
- inline std::size_t find_all(const Iterator pattern_begin,
- const Iterator pattern_end,
- const Iterator begin,
- const Iterator end,
- Sequence<Range,Allocator>& seq)
- {
- return find_all(pattern_begin,pattern_end,begin,end,std::back_inserter(seq));
- }
- inline std::size_t ifind(const std::string& pattern, const std::string& data)
- {
- if (pattern.size() > data.size())
- return std::string::npos;
- const char* result_itr = std::search(data.data(),data.data() + data.size(),
- pattern.data(), pattern.data() + pattern.size(),
- imatch_char);
- if ((data.data() + data.size()) == result_itr)
- return std::string::npos;
- else
- return std::distance(data.data(),result_itr);
- }
- template <typename Iterator, typename OutputIterator>
- inline std::size_t ifind_all(const Iterator pattern_begin,
- const Iterator pattern_end,
- const Iterator begin,
- const Iterator end,
- OutputIterator out)
- {
- Iterator itr = begin;
- const std::size_t pattern_length = std::distance(pattern_begin,pattern_end);
- std::size_t match_count = 0;
- while (end != (itr = std::search(itr, end, pattern_begin, pattern_end, imatch_char)))
- {
- (*out) = std::make_pair(itr,itr + pattern_length);
- itr += pattern_length;
- ++out;
- ++match_count;
- }
- return match_count;
- }
- template <typename OutputIterator>
- inline std::size_t find_all(const std::string& pattern,
- const std::string& data,
- OutputIterator out)
- {
- return find_all(pattern.data(), pattern.data() + pattern.size(),
- data.data(), data.data() + data.size(),
- out);
- }
- template <typename Range,
- typename Allocator,
- template <typename,typename> class Sequence>
- inline std::size_t find_all(const std::string& pattern,
- const std::string& data,
- Sequence<Range,Allocator>& seq)
- {
- return find_all(pattern,data,std::back_inserter(seq));
- }
- template <typename OutputIterator>
- inline std::size_t ifind_all(const std::string& pattern,
- const std::string& data,
- OutputIterator out)
- {
- return ifind_all(pattern.data(), pattern.data() + pattern.size(),
- data.data(), data.data() + data.size(),
- out);
- }
- template <typename Range,
- typename Allocator,
- template <typename,typename> class Sequence>
- inline std::size_t ifind_all(const std::string& pattern,
- const std::string& data,
- Sequence<Range,Allocator>& seq)
- {
- return ifind_all(pattern,data,std::back_inserter(seq));
- }
- template <typename InputIterator>
- inline bool begins_with(const InputIterator pattern_begin,
- const InputIterator pattern_end,
- const InputIterator begin,
- const InputIterator end)
- {
- typedef typename details::is_valid_iterator<InputIterator>::type itr_type;
- if (std::distance(pattern_begin,pattern_end) <= std::distance(begin,end))
- {
- return std::equal(pattern_begin,pattern_end,begin);
- }
- else
- return false;
- }
- inline bool begins_with(const std::string& pattern, const std::string& data)
- {
- if (pattern.size() <= data.size())
- {
- return begins_with(pattern.data(),
- pattern.data() + pattern.size(),
- data.data(),
- data.data() + data.size());
- }
- else
- return false;
- }
- template <typename InputIterator>
- inline bool ibegins_with(const InputIterator pattern_begin,
- const InputIterator pattern_end,
- const InputIterator begin,
- const InputIterator end)
- {
- typedef typename details::is_valid_iterator<InputIterator>::type itr_type;
- if (std::distance(pattern_begin,pattern_end) <= std::distance(begin,end))
- {
- return std::equal(pattern_begin,pattern_end,begin,imatch_char);
- }
- else
- return false;
- }
- inline bool ibegins_with(const std::string& pattern, const std::string& data)
- {
- if (pattern.size() <= data.size())
- {
- return ibegins_with(pattern.data(),
- pattern.data() + pattern.size(),
- data.data(),
- data.data() + data.size());
- }
- else
- return false;
- }
- template <typename InputIterator>
- inline bool ends_with(const InputIterator pattern_begin,
- const InputIterator pattern_end,
- const InputIterator begin,
- const InputIterator end)
- {
- typedef typename details::is_valid_iterator<InputIterator>::type itr_type;
- const std::size_t pattern_length = std::distance(pattern_begin,pattern_end);
- const std::size_t data_length = std::distance(begin,end);
- if (pattern_length <= data_length)
- {
- return std::equal(pattern_begin,
- pattern_end,
- begin + (data_length - pattern_length));
- }
- else
- return false;
- }
- inline bool ends_with(const std::string& pattern, const std::string& data)
- {
- if (pattern.size() <= data.size())
- {
- return ends_with(pattern.data(),
- pattern.data() + pattern.size(),
- data.data(),
- data.data() + data.size());
- }
- else
- return false;
- }
- template <typename InputIterator>
- inline bool iends_with(const InputIterator pattern_begin,
- const InputIterator pattern_end,
- const InputIterator begin,
- const InputIterator end)
- {
- typedef typename details::is_valid_iterator<InputIterator>::type itr_type;
- const std::size_t pattern_length = std::distance(pattern_begin,pattern_end);
- const std::size_t data_length = std::distance(begin,end);
- if (pattern_length <= data_length)
- {
- return std::equal(pattern_begin,
- pattern_end,
- begin + (data_length - pattern_length),
- imatch_char);
- }
- else
- return false;
- }
- inline bool iends_with(const std::string& pattern, const std::string& data)
- {
- if (pattern.size() <= data.size())
- {
- return iends_with(pattern.data(),
- pattern.data() + pattern.size(),
- data.data(),
- data.data() + data.size());
- }
- else
- return false;
- }
- inline std::size_t index_of(const std::string& pattern, const std::string& data)
- {
- if (pattern.empty())
- return std::string::npos;
- else if (data.empty())
- return std::string::npos;
- else if (pattern.size() > data.size())
- return std::string::npos;
- const char* itr = std::search(data.data(),
- data.data() + data.size(),
- pattern.data(),
- pattern.data() + pattern.size());
- return ((data.data() + data.size()) == itr) ? std::string::npos : std::distance(data.data(),itr);
- }
- namespace tokenize_options
- {
- typedef std::size_t type;
- enum
- {
- default_mode = 0,
- compress_delimiters = 1,
- include_1st_delimiter = 2,
- include_all_delimiters = 4
- };
- static inline bool perform_compress_delimiters(const type& split_opt)
- {
- return compress_delimiters == (split_opt & compress_delimiters);
- }
- static inline bool perform_include_1st_delimiter(const type& split_opt)
- {
- return include_1st_delimiter == (split_opt & include_1st_delimiter);
- }
- static inline bool perform_include_all_delimiters(const type& split_opt)
- {
- return include_all_delimiters == (split_opt & include_all_delimiters);
- }
- } // namespace tokenize_options
- template <typename Iterator, typename DelimiterPredicate>
- class tokenizer
- {
- private:
- template <typename Iterartor,
- typename Predicate,
- typename T = std::pair<Iterator,Iterator> >
- class tokenizer_iterator : public std::iterator<std::forward_iterator_tag,T>
- {
- protected:
- typedef Iterator iterator;
- typedef const iterator const_iterator;
- typedef typename std::pair<iterator,iterator> range_type;
- public:
- explicit inline tokenizer_iterator(const iterator begin,
- const iterator end,
- const Predicate& predicate,
- const tokenize_options::type tokenize_option = tokenize_options::default_mode)
- : predicate_(predicate),
- end_(end),
- range_(begin,begin),
- current_token_(end,end),
- compress_delimiters_(tokenize_options::perform_compress_delimiters(tokenize_option)),
- include_1st_delimiter_(tokenize_options::perform_include_1st_delimiter(tokenize_option)),
- include_all_delimiters_(tokenize_options::perform_include_all_delimiters(tokenize_option)),
- include_delimiters_(include_1st_delimiter_ || include_all_delimiters_),
- last_token_done_(false)
- {
- if (end != begin)
- {
- this->operator++();
- }
- }
- inline tokenizer_iterator& operator++()
- {
- if (last_token_done_)
- {
- range_.first = range_.second;
- return (*this);
- }
- else if (end_ != range_.second)
- {
- range_.first = range_.second;
- }
- while (end_ != range_.second)
- {
- if (predicate_(*(range_.second)))
- {
- if (include_delimiters_)
- {
- if (include_1st_delimiter_)
- ++range_.second;
- else if (include_all_delimiters_)
- while ((end_ != range_.second) && predicate_(*(range_.second))) ++range_.second;
- current_token_ = range_;
- if ((!include_all_delimiters_) && compress_delimiters_)
- while ((end_ != range_.second) && predicate_(*(range_.second))) ++range_.second;
- }
- else
- {
- current_token_ = range_;
- if (compress_delimiters_)
- while ((end_ != (++range_.second)) && predicate_(*(range_.second))) ;
- else
- ++range_.second;
- }
- return (*this);
- }
- else
- ++range_.second;
- }
- if (range_.first != range_.second)
- {
- current_token_.second = range_.second;
- if (!last_token_done_)
- {
- if (predicate_(*(range_.second - 1)))
- current_token_.first = range_.second;
- else
- current_token_.first = range_.first;
- last_token_done_ = true;
- }
- else
- range_.first = range_.second;
- }
- return (*this);
- }
- inline tokenizer_iterator operator++(int)
- {
- tokenizer_iterator tmp = (*this);
- this->operator++();
- return tmp;
- }
- inline tokenizer_iterator& operator+=(const int inc)
- {
- if (inc > 0)
- {
- for (int i = 0; i < inc; ++i, ++(*this)) ;
- }
- return (*this);
- }
- inline T operator*() const
- {
- return current_token_;
- }
- inline std::string as_string() const
- {
- return std::string(current_token_.first,current_token_.second);
- }
- inline bool operator==(const tokenizer_iterator& itr) const
- {
- return (range_ == itr.range_) && (end_ == itr.end_);
- }
- inline bool operator!=(const tokenizer_iterator& itr) const
- {
- return (range_ != itr.range_) || (end_ != itr.end_);
- }
- inline tokenizer_iterator& operator=(const tokenizer_iterator& itr)
- {
- if (this != &itr)
- {
- range_ = itr.range_;
- current_token_ = itr.current_token_;
- end_ = itr.end_;
- compress_delimiters_ = itr.compress_delimiters_;
- include_1st_delimiter_ = itr.include_1st_delimiter_;
- include_all_delimiters_ = itr.include_all_delimiters_;
- include_delimiters_ = itr.include_delimiters_;
- last_token_done_ = itr.last_token_done_;
- }
- return (*this);
- }
- inline std::string remaining() const
- {
- return std::string(current_token_.first,end_);
- }
- protected:
- const Predicate& predicate_;
- iterator end_;
- range_type range_;
- range_type current_token_;
- bool compress_delimiters_;
- bool include_1st_delimiter_;
- bool include_all_delimiters_;
- bool include_delimiters_;
- bool last_token_done_;
- };
- public:
- typedef typename std::iterator_traits<Iterator>::value_type value_type;
- typedef DelimiterPredicate predicate;
- typedef tokenizer_iterator<Iterator,DelimiterPredicate> iterator;
- typedef const iterator const_iterator;
- typedef iterator& iterator_ref;
- typedef const_iterator& const_iterator_ref;
- inline tokenizer(const Iterator begin,
- const Iterator end,
- const DelimiterPredicate& predicate,
- const tokenize_options::type tokenize_options = tokenize_options::default_mode)
- : tokenize_options_(tokenize_options),
- predicate_(predicate),
- begin_(begin),
- end_(end),
- begin_itr_(begin_,end_,predicate_,tokenize_options_),
- end_itr_(end_,end_,predicate_,tokenize_options_)
- {}
- inline tokenizer(const std::string& s,
- const DelimiterPredicate& predicate,
- const tokenize_options::type tokenize_options = tokenize_options::default_mode)
- : tokenize_options_(tokenize_options),
- predicate_(predicate),
- begin_(s.data()),
- end_(s.data() + s.size()),
- begin_itr_(begin_,end_,predicate_,tokenize_options_),
- end_itr_(end_,end_,predicate_,tokenize_options_)
- {}
- inline tokenizer& operator=(const tokenizer& t)
- {
- if (this != &t)
- {
- begin_ = t.begin_;
- end_ = t.end_;
- end_itr_ = t.end_itr_;
- begin_itr_ = t.begin_itr_;
- tokenize_options_ = t.tokenize_options_;
- }
- return (*this);
- }
- inline void assign(const std::string& s) const
- {
- assign(s.data(),s.data() + s.size());
- }
- inline void assign(const std::string& s)
- {
- assign(s.data(),s.data() + s.size());
- }
- inline void assign(const Iterator begin, const Iterator end)
- {
- begin_ = begin;
- end_ = end;
- begin_itr_ = iterator(begin_,end_,predicate_,tokenize_options_);
- end_itr_ = iterator(end_,end_,predicate_,tokenize_options_);
- }
- inline const_iterator_ref begin() const
- {
- return begin_itr_;
- }
- inline const_iterator_ref end() const
- {
- return end_itr_;
- }
- private:
- tokenize_options::type tokenize_options_;
- const DelimiterPredicate& predicate_;
- Iterator begin_;
- Iterator end_;
- iterator begin_itr_;
- iterator end_itr_;
- };
- namespace std_string
- {
- template <typename DelimiterPredicate = single_delimiter_predicate<std::string::value_type> >
- struct tokenizer
- {
- typedef DelimiterPredicate predicate_type;
- typedef const std::string::value_type* string_iterator_type;
- typedef strtk::tokenizer<string_iterator_type,DelimiterPredicate> type;
- typedef strtk::tokenizer<string_iterator_type,multiple_char_delimiter_predicate> md_type;
- typedef std::pair<string_iterator_type,string_iterator_type> iterator_type;
- };
- typedef tokenizer<>::iterator_type iterator_type;
- typedef tokenizer<>::iterator_type range_t;
- typedef std::vector<iterator_type> token_vector_type;
- typedef std::deque<iterator_type> token_deque_type;
- typedef std::list<iterator_type> token_list_type;
- } // namespace std_string
- template <typename Sequence>
- class range_to_type_back_inserter_iterator : public std::iterator<std::output_iterator_tag,
- void,
- void,
- void,
- void>
- {
- public:
- typedef typename Sequence::value_type value_type;
- explicit inline range_to_type_back_inserter_iterator(Sequence& sequence)
- : sequence_(sequence)
- {}
- range_to_type_back_inserter_iterator(const range_to_type_back_inserter_iterator& it)
- : sequence_(it.sequence_)
- {}
- inline range_to_type_back_inserter_iterator& operator=(const range_to_type_back_inserter_iterator& it)
- {
- if (this != &it)
- {
- this->sequence_ = it.sequence_;
- }
- return (*this);
- }
- template <typename Iterator>
- inline range_to_type_back_inserter_iterator& operator=(const std::pair<Iterator,Iterator>& r)
- {
- value_type t = value_type();
- if (string_to_type_converter(r.first,r.second,t))
- sequence_.push_back(t);
- return (*this);
- }
- inline range_to_type_back_inserter_iterator& operator=(const std::string& s)
- {
- value_type t = value_type();
- if (string_to_type_converter(s.data(),s.data() + s.size(),t))
- sequence_.push_back(t);
- return (*this);
- }
- template <typename Iterator>
- inline void operator()(const std::pair<Iterator,Iterator>& r) const
- {
- value_type t;
- if (string_to_type_converter(r.first,r.second,t))
- sequence_.push_back(t);
- }
- template <typename Iterator>
- inline void operator()(const Iterator begin, const Iterator end)
- {
- sequence_.push_back(string_to_type_converter<value_type>(begin,end));
- }
- inline range_to_type_back_inserter_iterator& operator*()
- {
- return (*this);
- }
- inline range_to_type_back_inserter_iterator& operator++()
- {
- return (*this);
- }
- inline range_to_type_back_inserter_iterator operator++(int)
- {
- return (*this);
- }
- private:
- Sequence& sequence_;
- };
- template <typename Sequence>
- inline range_to_type_back_inserter_iterator<Sequence> range_to_type_back_inserter(Sequence& sequence)
- {
- return (range_to_type_back_inserter_iterator<Sequence>(sequence));
- }
- template <typename Set>
- class range_to_type_inserter_iterator : public std::iterator<std::output_iterator_tag,
- void,
- void,
- void,
- void>
- {
- public:
- typedef typename Set::value_type value_type;
- explicit inline range_to_type_inserter_iterator(Set& set)
- : set_(set)
- {}
- range_to_type_inserter_iterator(const range_to_type_inserter_iterator& it)
- : set_(it.set_)
- {}
- inline range_to_type_inserter_iterator& operator=(const range_to_type_inserter_iterator& it)
- {
- if (this != &it)
- {
- this->set_ = it.set_;
- }
- return (*this);
- }
- template <typename Iterator>
- inline range_to_type_inserter_iterator& operator=(const std::pair<Iterator,Iterator>& r)
- {
- value_type t;
- if (string_to_type_converter(r.first,r.second,t))
- set_.insert(t);
- return (*this);
- }
- template <typename Iterator>
- inline void operator()(const std::pair<Iterator,Iterator>& r)
- {
- value_type t;
- if (string_to_type_converter(r.first,r.second,t))
- set_.insert(t);
- }
- inline range_to_type_inserter_iterator& operator*()
- {
- return (*this);
- }
- inline range_to_type_inserter_iterator& operator++()
- {
- return (*this);
- }
- inline range_to_type_inserter_iterator operator++(int)
- {
- return (*this);
- }
- private:
- Set& set_;
- };
- template <typename Set>
- inline range_to_type_inserter_iterator<Set> range_to_type_inserter(Set& set)
- {
- return (range_to_type_inserter_iterator<Set>(set));
- }
- template <typename Container>
- class range_to_type_push_inserter_iterator : public std::iterator<std::output_iterator_tag,
- void,
- void,
- void,
- void>
- {
- public:
- typedef typename Container::value_type value_type;
- explicit inline range_to_type_push_inserter_iterator(Container& container)
- : container_(container)
- {}
- range_to_type_push_inserter_iterator(const range_to_type_push_inserter_iterator& it)
- : container_(it.container_)
- {}
- inline range_to_type_push_inserter_iterator& operator=(const range_to_type_push_inserter_iterator& it)
- {
- if (this != &it)
- {
- this->container_ = it.container_;
- }
- return (*this);
- }
- template <typename Iterator>
- inline range_to_type_push_inserter_iterator& operator=(const std::pair<Iterator,Iterator>& r)
- {
- value_type t;
- if (string_to_type_converter(r.first,r.second,t))
- container_.push(t);
- return (*this);
- }
- template <typename Iterator>
- inline void operator()(const std::pair<Iterator,Iterator>& r)
- {
- value_type t;
- if (string_to_type_converter(r.first,r.second,t))
- container_.push(t);
- }
- inline range_to_type_push_inserter_iterator& operator*()
- {
- return (*this);
- }
- inline range_to_type_push_inserter_iterator& operator++()
- {
- return (*this);
- }
- inline range_to_type_push_inserter_iterator operator++(int)
- {
- return (*this);
- }
- private:
- Container& container_;
- };
- template <typename Container>
- inline range_to_type_push_inserter_iterator<Container> range_to_type_push_inserter(Container& container)
- {
- return (range_to_type_push_inserter_iterator<Container>(container));
- }
- template <typename Sequence>
- class back_inserter_with_valuetype_iterator : public std::iterator<std::output_iterator_tag,
- typename Sequence::value_type,
- void,
- void,
- void>
- {
- public:
- explicit inline back_inserter_with_valuetype_iterator(Sequence& sequence)
- : sequence_(sequence)
- {}
- back_inserter_with_valuetype_iterator(const back_inserter_with_valuetype_iterator& it)
- : sequence_(it.sequence_)
- {}
- inline back_inserter_with_valuetype_iterator& operator=(const back_inserter_with_valuetype_iterator& it)
- {
- if (this != &it)
- {
- this->sequence_ = it.sequence_;
- }
- return (*this);
- }
- inline back_inserter_with_valuetype_iterator& operator=(const typename Sequence::value_type& v)
- {
- sequence_.push_back(v);
- return (*this);
- }
- inline void operator()(const typename Sequence::value_type& v)
- {
- sequence_.push_back(v);
- }
- inline back_inserter_with_valuetype_iterator& operator*()
- {
- return (*this);
- }
- inline back_inserter_with_valuetype_iterator& operator++()
- {
- return (*this);
- }
- inline back_inserter_with_valuetype_iterator operator++(int)
- {
- return (*this);
- }
- private:
- Sequence& sequence_;
- };
- template <typename Sequence>
- inline back_inserter_with_valuetype_iterator<Sequence> back_inserter_with_valuetype(Sequence& sequence_)
- {
- return (back_inserter_with_valuetype_iterator<Sequence>(sequence_));
- }
- template <typename Set>
- class inserter_with_valuetype_iterator : public std::iterator<std::output_iterator_tag,
- typename Set::value_type,
- void,
- void,
- void>
- {
- public:
- explicit inline inserter_with_valuetype_iterator(Set& set)
- : set_(set)
- {}
- inserter_with_valuetype_iterator(const inserter_with_valuetype_iterator& itr)
- : set_(itr.set_)
- {}
- inline inserter_with_valuetype_iterator& operator=(const inserter_with_valuetype_iterator& itr)
- {
- if (this != &itr)
- {
- this->set_ = itr.set_;
- }
- return (*this);
- }
- inline inserter_with_valuetype_iterator& operator=(const typename Set::value_type& v)
- {
- set_.insert(v);
- return (*this);
- }
- inline void operator()(const typename Set::value_type& v)
- {
- set_.insert(v);
- }
- inline inserter_with_valuetype_iterator& operator*()
- {
- return (*this);
- }
- inline inserter_with_valuetype_iterator& operator++()
- {
- return (*this);
- }
- inline inserter_with_valuetype_iterator operator++(int)
- {
- return (*this);
- }
- private:
- Set& set_;
- };
- template <typename Set>
- inline inserter_with_valuetype_iterator<Set> inserter_with_valuetype(Set& set_)
- {
- return (inserter_with_valuetype_iterator<Set>(set_));
- }
- template <typename Container>
- class push_inserter_iterator : public std::iterator<std::output_iterator_tag,
- void,
- void,
- void,
- void>
- {
- public:
- explicit inline push_inserter_iterator(Container& container)
- : container_(container)
- {}
- inline push_inserter_iterator& operator=(const push_inserter_iterator& itr)
- {
- if (this != &itr)
- {
- this->container_ = itr.container_;
- }
- return (*this);
- }
- inline push_inserter_iterator<Container>& operator=(typename Container::const_reference v)
- {
- container_.push(v);
- return (*this);
- }
- inline push_inserter_iterator<Container>& operator*()
- {
- return (*this);
- }
- inline push_inserter_iterator<Container>& operator++()
- {
- return (*this);
- }
- inline push_inserter_iterator<Container> operator++(int)
- {
- return (*this);
- }
- private:
- Container& container_;
- };
- template <typename Container>
- inline push_inserter_iterator<Container> push_inserter(Container& c)
- {
- return push_inserter_iterator<Container>(c);
- }
- template <typename T>
- class range_to_ptr_type_iterator : public std::iterator<std::output_iterator_tag,
- void,
- void,
- void,
- void>
- {
- public:
- typedef T value_type;
- explicit inline range_to_ptr_type_iterator(T* pointer, std::size_t& insert_count)
- : pointer_(pointer),
- insert_count_(insert_count)
- {}
- range_to_ptr_type_iterator(const range_to_ptr_type_iterator& it)
- : pointer_(it.pointer_)
- {}
- inline range_to_ptr_type_iterator& operator=(const range_to_ptr_type_iterator& it)
- {
- if (this != &it)
- {
- this->pointer_ = it.pointer_;
- }
- return (*this);
- }
- template <typename Iterator>
- inline range_to_ptr_type_iterator& operator=(const std::pair<Iterator,Iterator>& r)
- {
- value_type t = value_type();
- if (string_to_type_converter(r.first,r.second,t))
- {
- (*pointer_) = t;
- ++pointer_;
- ++insert_count_;
- }
- return (*this);
- }
- inline range_to_ptr_type_iterator& operator=(const std::string& s)
- {
- value_type t = value_type();
- if (string_to_type_converter(s.data(),s.data() + s.size(),t))
- {
- (*pointer_) = t;
- ++pointer_;
- ++insert_count_;
- }
- return (*this);
- }
- template <typename Iterator>
- inline void operator()(const std::pair<Iterator,Iterator>& r) const
- {
- value_type t;
- if (string_to_type_converter(r.first,r.second,t))
- {
- (*pointer_) = t;
- ++pointer_;
- ++insert_count_;
- }
- }
- template <typename Iterator>
- inline void operator()(const Iterator begin, const Iterator end)
- {
- (*pointer_) = string_to_type_converter<T>(begin,end);
- ++pointer_;
- ++insert_count_;
- }
- inline range_to_ptr_type_iterator& operator*()
- {
- return (*this);
- }
- inline range_to_ptr_type_iterator& operator++()
- {
- return (*this);
- }
- inline range_to_ptr_type_iterator operator++(int)
- {
- return (*this);
- }
- private:
- T* pointer_;
- std::size_t& insert_count_;
- };
- template <typename T>
- inline range_to_ptr_type_iterator<T> range_to_ptr_type(T* pointer, std::size_t& insert_count)
- {
- return (range_to_ptr_type_iterator<T>(pointer,insert_count));
- }
- template <typename T>
- inline range_to_ptr_type_iterator<T> range_to_ptr_type(T* pointer)
- {
- static std::size_t insert_count = 0;
- return (range_to_ptr_type_iterator<T>(pointer,insert_count));
- }
- template <typename T>
- class counting_back_inserter_iterator : public std::iterator<std::output_iterator_tag,
- T,
- void,
- void,
- void>
- {
- public:
- explicit inline counting_back_inserter_iterator(std::size_t& counter)
- : counter_(counter)
- {}
- counting_back_inserter_iterator(const counting_back_inserter_iterator& itr)
- : counter_(itr.counter_)
- {}
- inline counting_back_inserter_iterator& operator=(const counting_back_inserter_iterator& itr)
- {
- if (this != &itr)
- {
- this->counter_ = itr.counter_;
- }
- return (*this);
- }
- inline counting_back_inserter_iterator& operator=(const T&)
- {
- ++counter_;
- return (*this);
- }
- inline void operator()(const T&)
- {
- ++counter_;
- }
- inline counting_back_inserter_iterator& operator*()
- {
- return (*this);
- }
- inline counting_back_inserter_iterator& operator++()
- {
- return (*this);
- }
- inline counting_back_inserter_iterator operator++(int)
- {
- return (*this);
- }
- private:
- std::size_t& counter_;
- };
- template <typename T>
- inline counting_back_inserter_iterator<T> counting_back_inserter(std::size_t& counter_)
- {
- return (counting_back_inserter_iterator<T>(counter_));
- }
- template <typename Function>
- class functional_inserter_iterator : public std::iterator<std::output_iterator_tag,
- void,
- void,
- void,
- void>
- {
- public:
- explicit inline functional_inserter_iterator(Function function)
- : function_(function)
- {}
- functional_inserter_iterator(const functional_inserter_iterator& it)
- : function_(it.function_)
- {}
- inline functional_inserter_iterator& operator=(const functional_inserter_iterator& it)
- {
- if (this != &it)
- {
- this->function_ = it.function_;
- }
- return (*this);
- }
- template <typename T>
- inline functional_inserter_iterator& operator=(const T& t)
- {
- function_(t);
- return (*this);
- }
- template <typename T>
- inline void operator()(const T& t)
- {
- function_(t);
- }
- inline functional_inserter_iterator& operator*()
- {
- return (*this);
- }
- inline functional_inserter_iterator& operator++()
- {
- return (*this);
- }
- inline functional_inserter_iterator operator++(int)
- {
- return (*this);
- }
- private:
- Function function_;
- };
- template <typename Function>
- inline functional_inserter_iterator<Function> functional_inserter(Function function)
- {
- return (functional_inserter_iterator<Function>(function));
- }
- namespace split_options
- {
- typedef std::size_t type;
- enum
- {
- default_mode = 0,
- compress_delimiters = 1,
- include_1st_delimiter = 2,
- include_all_delimiters = 4
- };
- static inline bool perform_compress_delimiters(const type& split_opt)
- {
- return compress_delimiters == (split_opt & compress_delimiters);
- }
- static inline bool perform_include_1st_delimiter(const type& split_opt)
- {
- return include_1st_delimiter == (split_opt & include_1st_delimiter);
- }
- static inline bool perform_include_all_delimiters(const type& split_opt)
- {
- return include_all_delimiters == (split_opt & include_all_delimiters);
- }
- } // namespace split_options
- template <typename DelimiterPredicate,
- typename Iterator,
- typename OutputIterator>
- inline std::size_t split(const DelimiterPredicate& delimiter,
- const Iterator begin,
- const Iterator end,
- OutputIterator out,
- const split_options::type split_option = split_options::default_mode)
- {
- if (begin == end) return 0;
- std::size_t token_count = 0;
- std::pair<Iterator,Iterator> range(begin,begin);
- const bool compress_delimiters = split_options::perform_compress_delimiters(split_option);
- const bool include_1st_delimiter = split_options::perform_include_1st_delimiter(split_option);
- const bool include_all_delimiters = (!include_1st_delimiter) && split_options::perform_include_all_delimiters(split_option);
- const bool include_delimiters = include_1st_delimiter || include_all_delimiters;
- while (end != range.second)
- {
- if (delimiter(*range.second))
- {
- if (include_delimiters)
- {
- if (include_1st_delimiter)
- ++range.second;
- else if (include_all_delimiters)
- while ((end != range.second) && delimiter(*range.second)) ++range.second;
- (*out) = range;
- ++out;
- if ((!include_all_delimiters) && compress_delimiters)
- while ((end != range.second) && delimiter(*range.second)) ++range.second;
- }
- else
- {
- (*out) = range;
- ++out;
- if (compress_delimiters)
- while ((end != (++range.second)) && delimiter(*range.second)) ;
- else
- ++range.second;
- }
- ++token_count;
- range.first = range.second;
- }
- else
- ++range.second;
- }
- if ((range.first != range.second) || delimiter(*(range.second - 1)))
- {
- (*out) = range;
- ++out;
- ++token_count;
- }
- return token_count;
- }
- template <typename DelimiterPredicate,
- typename Iterator,
- typename OutputIterator>
- inline std::size_t split(const DelimiterPredicate& delimiter,
- const std::pair<Iterator,Iterator>& range,
- OutputIterator out,
- const split_options::type split_option = split_options::default_mode)
- {
- return split(delimiter,
- range.first,range.second,
- out,
- split_option);
- }
- template <typename DelimiterPredicate,
- typename Iterator,
- typename OutputIterator>
- inline std::size_t split(const char* delimiters,
- const std::pair<Iterator,Iterator>& range,
- OutputIterator out,
- const split_options::type split_option = split_options::default_mode)
- {
- if (1 == details::strnlength(delimiters,256))
- return split(single_delimiter_predicate<std::string::value_type>(delimiters[0]),
- range.first,range.second,
- out,
- split_option);
- else
- return split(multiple_char_delimiter_predicate(delimiters),
- range.first,range.second,
- out,
- split_option);
- }
- template <typename DelimiterPredicate,
- typename Iterator,
- typename OutputIterator>
- inline std::size_t split(const std::string& delimiters,
- const std::pair<Iterator,Iterator>& range,
- OutputIterator out,
- const split_options::type split_option = split_options::default_mode)
- {
- if (1 == delimiters.size())
- return split(single_delimiter_predicate<std::string::value_type>(delimiters[0]),
- range.first,range.second,
- out,
- split_option);
- else
- return split(multiple_char_delimiter_predicate(delimiters),
- range.first,range.second,
- out,
- split_option);
- }
- template <typename OutputIterator>
- inline std::size_t split(const char* delimiters,
- const std::string& str,
- OutputIterator out,
- const split_options::type& split_option = split_options::default_mode)
- {
- if (1 == details::strnlength(delimiters,256))
- return split(single_delimiter_predicate<std::string::value_type>(delimiters[0]),
- str.data(), str.data() + str.size(),
- out,
- split_option);
- else
- return split(multiple_char_delimiter_predicate(delimiters),
- str.data(), str.data() + str.size(),
- out,
- split_option);
- }
- template <typename OutputIterator>
- inline std::size_t split(const std::string& delimiters,
- const std::string& str,
- OutputIterator out,
- const split_options::type& split_option = split_options::default_mode)
- {
- if (1 == delimiters.size())
- return split(single_delimiter_predicate<std::string::value_type>(delimiters[0]),
- str.data(), str.data() + str.size(),
- out,
- split_option);
- else
- return split(multiple_char_delimiter_predicate(delimiters),
- str.data(), str.data() + str.size(),
- out,
- split_option);
- }
- template <typename OutputIterator>
- inline std::size_t split(const std::string::value_type delimiter,
- const std::string& str,
- OutputIterator out,
- const split_options::type& split_option = split_options::default_mode)
- {
- return split(single_delimiter_predicate<std::string::value_type>(delimiter),
- str.data(), str.data() + str.size(),
- out,
- split_option);
- }
- template <typename Allocator,
- template <typename,typename> class Sequence>
- inline std::size_t split(const char* delimiters,
- const std::string& str,
- Sequence<std::pair<const char*, const char*>,Allocator>& sequence,
- const split_options::type& split_option = split_options::default_mode)
- {
- if (1 == details::strnlength(delimiters,256))
- return split(single_delimiter_predicate<std::string::value_type>(delimiters[0]),
- str.data(), str.data() + str.size(),
- std::back_inserter(sequence),
- split_option);
- else
- return split(multiple_char_delimiter_predicate(delimiters),
- str.data(), str.data() + str.size(),
- std::back_inserter(sequence),
- split_option);
- }
- template <typename Allocator,
- template <typename,typename> class Sequence>
- inline std::size_t split(const std::string& delimiters,
- const std::string& str,
- Sequence<std::pair<const char*, const char*>,Allocator>& sequence,
- const split_options::type& split_option = split_options::default_mode)
- {
- if (1 == delimiters.size())
- return split(single_delimiter_predicate<std::string::value_type>(delimiters[0]),
- str.data(), str.data() + str.size(),
- std::back_inserter(sequence),
- split_option);
- else
- return split(multiple_char_delimiter_predicate(delimiters),
- str.data(), str.data() + str.size(),
- std::back_inserter(sequence),
- split_option);
- }
- template <typename DelimiterPredicate,
- typename OutputIterator>
- inline std::size_t split(const DelimiterPredicate& delimiter,
- const std::string& str,
- OutputIterator out,
- const split_options::type& split_option = split_options::default_mode)
- {
- return split(delimiter,
- str.data(), str.data() + str.size(),
- out,
- split_option);
- }
- template <typename DelimiterPredicate,
- typename Iterator,
- typename OutputIterator>
- inline std::size_t split_n(const DelimiterPredicate& delimiter,
- const Iterator begin,
- const Iterator end,
- const std::size_t& token_count,
- OutputIterator out,
- const split_options::type& split_option = split_options::default_mode)
- {
- if (0 == token_count) return 0;
- if (begin == end) return 0;
- std::size_t match_count = 0;
- std::pair<Iterator,Iterator> range(begin,begin);
- const bool compress_delimiters = split_options::perform_compress_delimiters(split_option);
- const bool include_1st_delimiter = split_options::perform_include_1st_delimiter(split_option);
- const bool include_all_delimiters = (!include_1st_delimiter) && split_options::perform_include_all_delimiters(split_option);
- const bool include_delimiters = include_1st_delimiter || include_all_delimiters;
- while (end != range.second)
- {
- if (delimiter(*range.second))
- {
- if (include_delimiters)
- {
- ++range.second;
- (*out) = range;
- ++out;
- if (++match_count >= token_count)
- return match_count;
- if (compress_delimiters)
- while ((end != range.second) && delimiter(*range.second)) ++range.second;
- }
- else
- {
- (*out) = range;
- ++out;
- if (++match_count >= token_count)
- return match_count;
- if (compress_delimiters)
- while ((end != (++range.second)) && delimiter(*range.second)) ;
- else
- ++range.second;
- }
- range.first = range.second;
- }
- else
- ++range.second;
- }
- if ((range.first != range.second) || delimiter(*(range.second - 1)))
- {
- (*out) = range;
- ++out;
- ++match_count;
- }
- return match_count;
- }
- template <typename OutputIterator>
- inline std::size_t split_n(const char* delimiters,
- const std::string& str,
- const std::size_t& token_count,
- OutputIterator out,
- const split_options::type& split_option = split_options::default_mode)
- {
- return split_n(multiple_char_delimiter_predicate(delimiters),
- str.data(), str.data() + str.size(),
- token_count,
- out,
- split_option);
- }
- template <typename OutputIterator>
- inline std::size_t split_n(const std::string& delimiters,
- const std::string& str,
- const std::size_t& token_count,
- OutputIterator out,
- const split_options::type& split_option = split_options::default_mode)
- {
- if (1 == delimiters.size())
- return split_n(single_delimiter_predicate<std::string::value_type>(delimiters[0]),
- str.data(), str.data() + str.size(),
- token_count,
- out,
- split_option);
- else
- return split_n(multiple_char_delimiter_predicate(delimiters),
- str.data(), str.data() + str.size(),
- token_count,
- out,
- split_option);
- }
- template <typename InputIterator, typename OutputIterator>
- inline std::size_t split_n(const std::string& delimiters,
- const InputIterator begin,
- const InputIterator end,
- const std::size_t& token_count,
- OutputIterator out,
- const split_options::type& split_option = split_options::default_mode)
- {
- typedef typename details::is_valid_iterator<InputIterator>::type itr_type;
- if (1 == delimiters.size())
- return split_n(single_delimiter_predicate<std::string::value_type>(delimiters[0]),
- begin,end,
- token_count,
- out,
- split_option);
- else
- return split_n(multiple_char_delimiter_predicate(delimiters),
- begin,end,
- token_count,
- out,
- split_option);
- }
- template <typename OutputIterator>
- inline std::size_t split_n(const std::string::value_type delimiter,
- const std::string& str,
- const std::size_t& token_count,
- OutputIterator out,
- const split_options::type& split_option = split_options::default_mode)
- {
- return split_n(single_delimiter_predicate<std::string::value_type>(delimiter),
- str.data(),str.data() + str.size(),
- token_count,
- out,
- split_option);
- }
- template <typename DelimiterPredicate,
- typename OutputIterator>
- inline std::size_t split_n(const DelimiterPredicate& delimiter,
- const std::string& str,
- const std::size_t& token_count,
- OutputIterator out,
- const split_options::type& split_option = split_options::default_mode)
- {
- return split_n(delimiter,
- str.data(),str.data() + str.size(),
- token_count,
- out,
- split_option);
- }
- #ifdef strtk_enable_regex
- static const std::string uri_expression ("((https?|ftp)\\://((\\[?(\\d{1,3}\\.){3}\\d{1,3}\\]?)|(([-a-zA-Z0-9]+\\.)+[a-zA-Z]{2,4}))(\\:\\d+)?(/[-a-zA-Z0-9._?,+&%$#=~\\\\]+)*/?)");
- static const std::string email_expression ("([\\w\\-\\.]+)@((\\[([0-9]{1,3}\\.){3}[0-9]{1,3}\\])|(([\\w\\-]+\\.)+)([a-zA-Z]{2,4}))");
- static const std::string ip_expression ("(([0-2]*[0-9]+[0-9]+)\\.([0-2]*[0-9]+[0-9]+)\\.([0-2]*[0-9]+[0-9]+)\\.([0-2]*[0-9]+[0-9]+))");
- static const std::string ieee754_expression ("([-+]?((\\.[0-9]+|[0-9]+\\.[0-9]+)([eE][-+][0-9]+)?|[0-9]+))");
- namespace regex_match_mode
- {
- enum type
- {
- match_all = 0,
- match_1 = 1,
- match_2 = 2,
- match_3 = 3,
- match_4 = 4,
- match_5 = 5,
- match_6 = 6,
- match_7 = 7,
- match_8 = 8,
- match_9 = 9
- };
- }
- template <typename InputIterator, typename OutputIterator>
- inline std::size_t split_regex(const boost::regex& delimiter_expression,
- const InputIterator begin,
- const InputIterator end,
- OutputIterator out,
- const regex_match_mode::type mode = regex_match_mode::match_all)
- {
- boost::regex_iterator<InputIterator> itr(begin,end,delimiter_expression);
- boost::regex_iterator<InputIterator> itr_end;
- std::pair<InputIterator,InputIterator> range(begin,begin);
- std::size_t match_count = 0;
- while (itr_end != itr)
- {
- range.first = (*itr)[mode].first;
- range.second = (*itr)[mode].second;
- (*out) = range;
- ++out;
- ++itr;
- ++match_count;
- }
- return match_count;
- }
- template <typename InputIterator, typename OutputIterator>
- inline std::size_t split_regex(const std::string& delimiter_expression,
- const InputIterator begin,
- const InputIterator end,
- OutputIterator out,
- const regex_match_mode::type mode = regex_match_mode::match_all)
- {
- const boost::regex regex_expression(delimiter_expression);
- return split_regex(regex_expression,
- begin,end,
- out,
- mode);
- }
- template <typename OutputIterator>
- inline std::size_t split_regex(const std::string& delimiter_expression,
- const std::string& text,
- OutputIterator out,
- const regex_match_mode::type mode = regex_match_mode::match_all)
- {
- return split_regex(delimiter_expression,
- text.begin(),text.end(),
- out,
- mode);
- }
- template <typename OutputIterator>
- inline std::size_t split_regex(const boost::regex& delimiter_expression,
- const std::string& text,
- OutputIterator out,
- const regex_match_mode::type mode = regex_match_mode::match_all)
- {
- return split_regex(delimiter_expression,
- text.begin(),text.end(),
- out,
- mode);
- }
- template <typename InputIterator, typename OutputIterator>
- inline std::size_t split_regex_n(const boost::regex& delimiter_expression,
- const InputIterator begin,
- const InputIterator end,
- const std::size_t& token_count,
- OutputIterator out,
- const regex_match_mode::type mode = regex_match_mode::match_all)
- {
- boost::sregex_iterator itr(begin,end,delimiter_expression);
- const boost::sregex_iterator itr_end;
- std::pair<InputIterator,InputIterator> range(begin,begin);
- std::size_t match_count = 0;
- while (itr_end != itr)
- {
- range.first = (*itr)[mode].first;
- range.second = (*itr)[mode].second;
- (*out) = range;
- ++out;
- ++itr;
- if (++match_count >= token_count)
- return match_count;
- }
- return match_count;
- }
- template <typename InputIterator, typename OutputIterator>
- inline std::size_t split_regex_n(const std::string& delimiter_expression,
- const InputIterator begin,
- const InputIterator end,
- const std::size_t& token_count,
- OutputIterator out,
- const regex_match_mode::type mode = regex_match_mode::match_all)
- {
- const boost::regex regex_expression(delimiter_expression);
- return split_regex_n(regex_expression,
- begin,end,
- token_count,
- out,
- mode);
- }
- template <typename OutputIterator>
- inline std::size_t split_regex_n(const std::string& delimiter_expression,
- const std::string& text,
- const std::size_t& token_count,
- OutputIterator out,
- const regex_match_mode::type mode = regex_match_mode::match_all)
- {
- return split_regex_n(delimiter_expression,
- text.begin(),text.end(),
- token_count,
- out,
- mode);
- }
- template <typename OutputIterator>
- inline std::size_t split_regex_n(const boost::regex& delimiter_expression,
- const std::string& text,
- const std::size_t& token_count,
- OutputIterator out,
- const regex_match_mode::type mode = regex_match_mode::match_all)
- {
- return split_regex_n(delimiter_expression,
- text.begin(),text.end(),
- token_count,
- out,
- mode);
- }
- #endif // strtk_enable_regex
- template <const std::size_t offset_list_size>
- class offset_predicate
- {
- public:
- offset_predicate(const int offset_list[], const bool rotate = false)
- : rotate_(rotate),
- current_index_(0)
- {
- std::copy(offset_list, offset_list + offset_list_size, offset_list_);
- offset_list_[offset_list_size] = 0;
- }
- inline bool operator!() const
- {
- return (0 == offset_list_size);
- }
- inline void reset() const
- {
- current_index_ = 0;
- }
- inline std::size_t size() const
- {
- return offset_list_size;
- }
- inline int next() const
- {
- int result = offset_list_[current_index_++];
- if (rotate_ && (current_index_ >= offset_list_size))
- {
- current_index_ = 0;
- }
- return result;
- }
- private:
- bool rotate_;
- mutable std::size_t current_index_;
- int offset_list_[offset_list_size + 1];
- };
- inline offset_predicate<12> offsets(const int& v1, const int& v2, const int& v3,
- const int& v4, const int& v5, const int& v6,
- const int& v7, const int& v8, const int& v9,
- const int& v10, const int& v11, const int& v12,
- const bool& rotate = false)
- {
- const int offset_list[12] = { v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12 };
- return offset_predicate<12>(offset_list,rotate);
- }
- inline offset_predicate<11> offsets(const int& v1, const int& v2, const int& v3,
- const int& v4, const int& v5, const int& v6,
- const int& v7, const int& v8, const int& v9,
- const int& v10, const int& v11,
- const bool& rotate = false)
- {
- const int offset_list[11] = { v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11 };
- return offset_predicate<11>(offset_list,rotate);
- }
- inline offset_predicate<10> offsets(const int& v1, const int& v2, const int& v3,
- const int& v4, const int& v5, const int& v6,
- const int& v7, const int& v8, const int& v9,
- const int& v10, const bool& rotate = false)
- {
- const int offset_list[10] = { v1, v2, v3, v4, v5, v6, v7, v8, v9, v10 };
- return offset_predicate<10>(offset_list,rotate);
- }
- inline offset_predicate<9> offsets(const int& v1, const int& v2, const int& v3,
- const int& v4, const int& v5, const int& v6,
- const int& v7, const int& v8, const int& v9,
- const bool& rotate = false)
- {
- const int offset_list[9] = { v1, v2, v3, v4, v5, v6, v7, v8, v9 };
- return offset_predicate<9>(offset_list,rotate);
- }
- inline offset_predicate<8> offsets(const int& v1, const int& v2, const int& v3,
- const int& v4, const int& v5, const int& v6,
- const int& v7, const int& v8, const bool& rotate = false)
- {
- const int offset_list[8] = { v1, v2, v3, v4, v5, v6, v7, v8 };
- return offset_predicate<8>(offset_list,rotate);
- }
- inline offset_predicate<7> offsets(const int& v1, const int& v2, const int& v3,
- const int& v4, const int& v5, const int& v6,
- const int& v7, const bool& rotate = false)
- {
- const int offset_list[7] = { v1, v2, v3, v4, v5, v6, v7 };
- return offset_predicate<7>(offset_list,rotate);
- }
- inline offset_predicate<6> offsets(const int& v1, const int& v2, const int& v3,
- const int& v4, const int& v5, const int& v6,
- const bool& rotate = false)
- {
- const int offset_list[6] = { v1, v2, v3, v4, v5, v6 };
- return offset_predicate<6>(offset_list,rotate);
- }
- inline offset_predicate<5> offsets(const int& v1, const int& v2, const int& v3,
- const int& v4, const int& v5, const bool& rotate = false)
- {
- const int offset_list[5] = { v1, v2, v3, v4, v5 };
- return offset_predicate<5>(offset_list,rotate);
- }
- inline offset_predicate<4> offsets(const int& v1, const int& v2, const int& v3,
- const int& v4, const bool& rotate = false)
- {
- const int offset_list[4] = { v1, v2, v3, v4 };
- return offset_predicate<4>(offset_list,rotate);
- }
- inline offset_predicate<3> offsets(const int& v1, const int& v2, const int& v3,
- const bool& rotate = false)
- {
- const int offset_list[3] = { v1, v2, v3 };
- return offset_predicate<3>(offset_list,rotate);
- }
- inline offset_predicate<2> offsets(const int& v1, const int& v2, const bool& rotate = false)
- {
- const int offset_list[2] = { v1, v2 };
- return offset_predicate<2>(offset_list,rotate);
- }
- inline offset_predicate<1> offsets(const int& v1,
- const bool& rotate = false)
- {
- const int offset_list[1] = { v1 };
- return offset_predicate<1>(offset_list,rotate);
- }
- template <typename OffsetPredicate,
- typename InputIterator,
- typename OutputIterator>
- inline std::size_t offset_splitter(const InputIterator begin,
- const InputIterator end,
- const OffsetPredicate& offset,
- OutputIterator out)
- {
- std::size_t length = 0;
- if (0 == (length = std::distance(begin,end))) return 0;
- std::pair<InputIterator,InputIterator> range(begin,begin);
- std::size_t match_count = 0;
- int offset_length = 0;
- std::size_t increment_amount = 0;
- while ((end != range.second) && (0 < (offset_length = offset.next())))
- {
- increment_amount = std::min<std::size_t>(length,offset_length);
- range.first = range.second;
- range.second += increment_amount;
- length -= increment_amount;
- (*out) = range;
- ++out;
- ++match_count;
- }
- return match_count;
- }
- template <typename OffsetPredicate,
- typename OutputIterator>
- inline std::size_t offset_splitter(const std::string& str,
- const OffsetPredicate& offset,
- OutputIterator out)
- {
- return offset_splitter(str.data(),str.data() + str.size(),offset,out);
- }
- template <typename InputIterator,
- typename Predicate,
- typename OutputPair>
- inline bool split_pair(const InputIterator begin,
- const InputIterator end,
- const Predicate& delimiter,
- OutputPair& v1,
- OutputPair& v2)
- {
- if (0 == std::distance(begin,end)) return false;
- InputIterator itr = begin;
- while (end != itr)
- {
- if (delimiter(*itr))
- {
- v1 = std::make_pair(begin,itr);
- ++itr;
- if (0 != std::distance(itr,end))
- {
- v2 = std::make_pair(itr,end);
- return true;
- }
- else
- return false;
- }
- else
- ++itr;
- }
- return false;
- }
- inline bool split_pair(const std::string::value_type delimiter,
- const std::string& str,
- std::pair<const char*,const char*>& v1,
- std::pair<const char*,const char*>& v2)
- {
- return split_pair(str.data(),
- str.data() + str.size(),
- single_delimiter_predicate<std::string::value_type>(delimiter),
- v1,
- v2);
- }
- template <typename DelimiterPredicate>
- inline bool split_pair(const DelimiterPredicate& delimiter,
- const std::string& str,
- std::pair<const char*,const char*>& v1,
- std::pair<const char*,const char*>& v2)
- {
- return split_pair(str.data(),
- str.data() + str.size(),
- delimiter,
- v1,
- v2);
- }
- template <typename Function>
- inline std::size_t for_each_token(const std::string& buffer,
- const std::string& delimiters,
- Function function)
- {
- return split(delimiters,
- buffer,
- strtk::functional_inserter<Function>(function));
- }
- template <typename Function>
- inline std::size_t for_each_token(const std::string& buffer,
- const char* delimiters,
- Function function)
- {
- return split(delimiters,
- buffer,
- strtk::functional_inserter<Function>(function));
- }
- template <typename InputIterator>
- inline std::size_t count_consecutive_duplicates(const InputIterator begin, const InputIterator end)
- {
- if (std::distance(begin,end) < 2) return 0;
- InputIterator prev = begin;
- InputIterator itr = begin;
- std::size_t count = 0;
- while (end != ++itr)
- {
- if ((*prev) == (*itr))
- ++count;
- else
- prev = itr;
- }
- return count;
- }
- template <typename T,
- typename Allocator,
- template <typename,typename> class Sequence>
- inline T min_of_cont(const Sequence<T,Allocator>& sequence)
- {
- return (*std::min_element(sequence.begin(),sequence.end()));
- }
- template <typename T,
- typename Comparator,
- typename Allocator>
- inline T min_of_cont(const std::set<T,Comparator,Allocator>& set)
- {
- return (*set.begin());
- }
- template <typename T,
- typename Comparator,
- typename Allocator>
- inline T min_of_cont(const std::multiset<T,Comparator,Allocator>& multiset)
- {
- return (*multiset.begin());
- }
- template <typename T,
- typename Allocator,
- template <typename,typename> class Sequence>
- inline T max_of_cont(const Sequence<T,Allocator>& sequence)
- {
- return (*std::max_element(sequence.begin(),sequence.end()));
- }
- template <typename T,
- typename Comparator,
- typename Allocator>
- inline T max_of_cont(const std::set<T,Comparator,Allocator>& set)
- {
- return (*set.rbegin());
- }
- template <typename T,
- typename Comparator,
- typename Allocator>
- inline T max_of_cont(const std::multiset<T,Comparator,Allocator>& multiset)
- {
- return (*multiset.rbegin());
- }
- template <typename InputIterator>
- inline void min_max_of_range(const InputIterator begin, const InputIterator end,
- typename std::iterator_traits<InputIterator>::value_type& min_value,
- typename std::iterator_traits<InputIterator>::value_type& max_value)
- {
- min_value = *begin;
- max_value = *begin;
- InputIterator itr = begin;
- while (end != ++itr)
- {
- if (*itr < min_value)
- min_value = (*itr);
- else if (*itr > max_value)
- max_value = (*itr);
- }
- }
- template <typename T,
- typename Allocator,
- template <typename,typename> class Sequence>
- inline void min_max_of_cont(const Sequence<T,Allocator>& sequence,
- T& min_value,
- T& max_value)
- {
- min_max_of_range(sequence.begin(),sequence.end(),
- min_value,
- max_value);
- }
- template <typename T,
- typename Comparator,
- typename Allocator>
- inline void min_max_of_cont(const std::set<T,Comparator,Allocator>& set,
- T& min_value,
- T& max_value)
- {
- min_value = (*set.begin());
- max_value = (*set.rbegin());
- }
- template <typename T,
- typename Comparator,
- typename Allocator>
- inline void min_max_of_cont(const std::multiset<T,Comparator,Allocator>& multiset,
- T& min_value,
- T& max_value)
- {
- min_value = (*multiset.begin());
- max_value = (*multiset.rbegin());
- }
- template <typename Iterator>
- inline void lexicographically_canonicalize(Iterator begin, Iterator end)
- {
- typedef typename std::iterator_traits<Iterator>::value_type type;
- typedef typename std::pair<Iterator,Iterator> iter_type;
- typedef typename std::list<iter_type> itr_list_type;
- itr_list_type itr_list;
- type smallest = (*std::min_element(begin,end));
- for (Iterator itr = begin; itr != end; ++itr)
- {
- if (*itr == smallest) itr_list.push_back(std::make_pair(itr,itr));
- }
- while (itr_list.size() > 1)
- {
- typename itr_list_type::iterator itr = itr_list.begin();
- while (itr_list.end() != itr)
- {
- ++(*itr).first;
- if (end == (*itr).first)
- itr = itr_list.erase(itr);
- else
- ++itr;
- }
- smallest = *(*itr_list.begin()).first;
- for (itr = (++itr_list.begin()); itr != itr_list.end(); ++itr)
- {
- if (*(*itr).first < smallest)
- {
- smallest = *(*itr).first;
- }
- }
- itr = itr_list.begin();
- while (itr_list.end() != itr)
- {
- if (*(*itr).first != smallest)
- itr = itr_list.erase(itr);
- else
- ++itr;
- }
- itr = itr_list.begin();
- while (itr_list.end() != itr)
- {
- if (end == (*itr).first)
- itr = itr_list.erase(itr);
- else
- ++itr;
- }
- }
- std::rotate(begin,(*itr_list.begin()).second,end);
- }
- inline void lexicographically_canonicalize(std::string& str)
- {
- lexicographically_canonicalize(const_cast<char*>(str.data()),
- const_cast<char*>(str.data() + str.size()));
- }
- template <typename T,
- typename Allocator,
- template <typename,typename> class Sequence>
- inline void lexicographically_canonicalize(Sequence<T,Allocator>& sequence)
- {
- lexicographically_canonicalize(sequence.begin(),sequence.end());
- }
- inline const char* first_non_repeated_char(const char* begin, const char* end)
- {
- static const std::size_t lut_size = 256;
- unsigned long long int lut[lut_size];
- std::fill_n(lut,lut_size,std::numeric_limits<unsigned long long int>::max());
- static const unsigned long long int not_yet_encountered = std::numeric_limits<unsigned long long int>::max();
- static const unsigned long long int repeated = not_yet_encountered - 1;
- const char* itr = begin;
- unsigned long long int position = 0;
- while (end != itr)
- {
- unsigned long long int& element = lut[static_cast<unsigned int>(*itr)];
- if (not_yet_encountered == element)
- {
- element = position;
- }
- else if (element < repeated)
- {
- element = repeated;
- }
- ++itr;
- ++position;
- }
- position = repeated;
- for (std::size_t i = 0; i < lut_size; ++i)
- {
- if (lut[i] < position)
- position = lut[i];
- }
- return (repeated != position) ? (begin + position) : end;
- }
- inline const unsigned char* first_non_repeated_char(const unsigned char* begin, const unsigned char* end)
- {
- char * b = reinterpret_cast<char*>(const_cast<unsigned char*>(begin));
- char * e = reinterpret_cast<char*>(const_cast<unsigned char*>(end));
- return const_cast<const unsigned char*>(reinterpret_cast<unsigned char*>(const_cast<char*>(first_non_repeated_char(b,e))));
- }
- inline std::size_t first_non_repeated_char(const std::string& str)
- {
- if (str.empty())
- return static_cast<std::size_t>(std::string::npos);
- const char* itr = first_non_repeated_char(str.data(),str.data() + str.size());
- if ((str.data() + str.size()) != itr)
- return static_cast<std::size_t>(itr - str.data());
- else
- return static_cast<std::size_t>(std::string::npos);
- }
- inline void convert_bin_to_hex(const unsigned char* begin, const unsigned char* end, unsigned char* out)
- {
- static const unsigned short hex_lut[] =
- {
- 0x3030, 0x3130, 0x3230, 0x3330, 0x3430, 0x3530, 0x3630, 0x3730,
- 0x3830, 0x3930, 0x4130, 0x4230, 0x4330, 0x4430, 0x4530, 0x4630,
- 0x3031, 0x3131, 0x3231, 0x3331, 0x3431, 0x3531, 0x3631, 0x3731,
- 0x3831, 0x3931, 0x4131, 0x4231, 0x4331, 0x4431, 0x4531, 0x4631,
- 0x3032, 0x3132, 0x3232, 0x3332, 0x3432, 0x3532, 0x3632, 0x3732,
- 0x3832, 0x3932, 0x4132, 0x4232, 0x4332, 0x4432, 0x4532, 0x4632,
- 0x3033, 0x3133, 0x3233, 0x3333, 0x3433, 0x3533, 0x3633, 0x3733,
- 0x3833, 0x3933, 0x4133, 0x4233, 0x4333, 0x4433, 0x4533, 0x4633,
- 0x3034, 0x3134, 0x3234, 0x3334, 0x3434, 0x3534, 0x3634, 0x3734,
- 0x3834, 0x3934, 0x4134, 0x4234, 0x4334, 0x4434, 0x4534, 0x4634,
- 0x3035, 0x3135, 0x3235, 0x3335, 0x3435, 0x3535, 0x3635, 0x3735,
- 0x3835, 0x3935, 0x4135, 0x4235, 0x4335, 0x4435, 0x4535, 0x4635,
- 0x3036, 0x3136, 0x3236, 0x3336, 0x3436, 0x3536, 0x3636, 0x3736,
- 0x3836, 0x3936, 0x4136, 0x4236, 0x4336, 0x4436, 0x4536, 0x4636,
- 0x3037, 0x3137, 0x3237, 0x3337, 0x3437, 0x3537, 0x3637, 0x3737,
- 0x3837, 0x3937, 0x4137, 0x4237, 0x4337, 0x4437, 0x4537, 0x4637,
- 0x3038, 0x3138, 0x3238, 0x3338, 0x3438, 0x3538, 0x3638, 0x3738,
- 0x3838, 0x3938, 0x4138, 0x4238, 0x4338, 0x4438, 0x4538, 0x4638,
- 0x3039, 0x3139, 0x3239, 0x3339, 0x3439, 0x3539, 0x3639, 0x3739,
- 0x3839, 0x3939, 0x4139, 0x4239, 0x4339, 0x4439, 0x4539, 0x4639,
- 0x3041, 0x3141, 0x3241, 0x3341, 0x3441, 0x3541, 0x3641, 0x3741,
- 0x3841, 0x3941, 0x4141, 0x4241, 0x4341, 0x4441, 0x4541, 0x4641,
- 0x3042, 0x3142, 0x3242, 0x3342, 0x3442, 0x3542, 0x3642, 0x3742,
- 0x3842, 0x3942, 0x4142, 0x4242, 0x4342, 0x4442, 0x4542, 0x4642,
- 0x3043, 0x3143, 0x3243, 0x3343, 0x3443, 0x3543, 0x3643, 0x3743,
- 0x3843, 0x3943, 0x4143, 0x4243, 0x4343, 0x4443, 0x4543, 0x4643,
- 0x3044, 0x3144, 0x3244, 0x3344, 0x3444, 0x3544, 0x3644, 0x3744,
- 0x3844, 0x3944, 0x4144, 0x4244, 0x4344, 0x4444, 0x4544, 0x4644,
- 0x3045, 0x3145, 0x3245, 0x3345, 0x3445, 0x3545, 0x3645, 0x3745,
- 0x3845, 0x3945, 0x4145, 0x4245, 0x4345, 0x4445, 0x4545, 0x4645,
- 0x3046, 0x3146, 0x3246, 0x3346, 0x3446, 0x3546, 0x3646, 0x3746,
- 0x3846, 0x3946, 0x4146, 0x4246, 0x4346, 0x4446, 0x4546, 0x4646
- };
- for (const unsigned char* itr = begin; end != itr; ++itr)
- {
- *reinterpret_cast<unsigned short*>(out) = hex_lut[(*itr)];
- out += sizeof(unsigned short);
- }
- }
- inline void convert_bin_to_hex(const char* begin, const char* end, char* out)
- {
- convert_bin_to_hex(reinterpret_cast<const unsigned char*>(begin),
- reinterpret_cast<const unsigned char*>(end),
- reinterpret_cast<unsigned char*>(out));
- }
- inline void convert_bin_to_hex(const std::pair<unsigned char*,unsigned char*>& r, unsigned char* out)
- {
- convert_bin_to_hex(r.first,r.second,out);
- }
- inline void convert_bin_to_hex(const std::pair<const unsigned char*,const unsigned char*>& r, unsigned char* out)
- {
- convert_bin_to_hex(r.first,r.second,out);
- }
- inline void convert_bin_to_hex(const std::pair<const char*,const char*>& r, char* out)
- {
- convert_bin_to_hex(r.first,r.second,out);
- }
- inline void convert_bin_to_hex(const std::string& binary_data, std::string& output)
- {
- output.resize(binary_data.size() * 2);
- convert_bin_to_hex(binary_data.data(),
- binary_data.data() + binary_data.size(),
- const_cast<char*>(output.data()));
- }
- inline std::string convert_bin_to_hex(const std::string& binary_data)
- {
- std::string output;
- convert_bin_to_hex(binary_data,output);
- return output;
- }
- inline bool convert_hex_to_bin(const unsigned char* begin, const unsigned char* end, unsigned char* out)
- {
- const std::size_t length = std::distance(begin,end);
- if (0 == length)
- return false;
- else if (1 == (length % 2))
- return false;
- static const unsigned char hex_to_bin[] =
- {
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x00 - 0x07
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x08 - 0x0F
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x10 - 0x17
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x18 - 0x1F
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x20 - 0x27
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x28 - 0x2F
- 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, // 0x30 - 0x37
- 0x08, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x38 - 0x3F
- 0x00, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x00, // 0x40 - 0x47
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x48 - 0x4F
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x50 - 0x57
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x58 - 0x5F
- 0x00, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x00, // 0x60 - 0x67
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x68 - 0x6F
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x70 - 0x77
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x78 - 0x7F
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x80 - 0x87
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x88 - 0x8F
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x90 - 0x97
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x98 - 0x9F
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xA0 - 0xA7
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xA8 - 0xAF
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xB0 - 0xB7
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xB8 - 0xBF
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xC0 - 0xC7
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xC8 - 0xCF
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xD0 - 0xD7
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xD8 - 0xDF
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xE0 - 0xE7
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xE8 - 0xEF
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xF0 - 0xF7
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // 0xF8 - 0xFF
- };
- const unsigned char* itr = begin;
- while (end != itr)
- {
- *reinterpret_cast<unsigned char*>(out) = static_cast<unsigned char>(hex_to_bin[itr[0]] << 4 | hex_to_bin[itr[1]]);
- ++out;
- itr += 2;
- }
- return true;
- }
- inline bool convert_hex_to_bin(const char* begin, const char* end, char* out)
- {
- return convert_hex_to_bin(reinterpret_cast<const unsigned char*>(begin),
- reinterpret_cast<const unsigned char*>(end),
- reinterpret_cast<unsigned char*>(out));
- }
- inline bool convert_hex_to_bin(const std::pair<unsigned char*,unsigned char*>& r, unsigned char* out)
- {
- return convert_hex_to_bin(r.first,r.second,out);
- }
- inline bool convert_hex_to_bin(const std::pair<const unsigned char*,const unsigned char*>& r, unsigned char* out)
- {
- return convert_hex_to_bin(r.first,r.second,out);
- }
- inline bool convert_hex_to_bin(const std::pair<char*,char*>& r, char* out)
- {
- return convert_hex_to_bin(r.first,r.second,out);
- }
- inline bool convert_hex_to_bin(const std::pair<const char*,const char*>& r, char* out)
- {
- return convert_hex_to_bin(r.first,r.second,out);
- }
- inline bool convert_hex_to_bin(const std::string& hex_data, std::string& output)
- {
- if (hex_data.empty() || (1 == (hex_data.size() % 2)))
- return false;
- output.resize(hex_data.size() >> 1);
- return convert_hex_to_bin(hex_data.data(),
- hex_data.data() + hex_data.size(),
- const_cast<char*>(output.data()));
- }
- inline std::size_t convert_bin_to_base64(const unsigned char* begin, const unsigned char* end, unsigned char* out)
- {
- static const unsigned char bin_to_base64 [] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
- const std::size_t length = std::distance(begin,end);
- std::size_t rounds = length / 3;
- const unsigned char* itr = begin;
- for (std::size_t i = 0; i < rounds; ++i)
- {
- unsigned int block = *(itr++) << 16;
- block |= *(itr++) << 8;
- block |= *(itr++) ;
- *(out++) = bin_to_base64[( block >> 18 ) & 0x3F];
- *(out++) = bin_to_base64[( block >> 12 ) & 0x3F];
- *(out++) = bin_to_base64[( block >> 6 ) & 0x3F];
- *(out++) = bin_to_base64[( block ) & 0x3F];
- }
- if ((rounds = (length % 3)) > 0)
- {
- switch (rounds)
- {
- case 1 : {
- unsigned int block = (unsigned char) (*itr) << 16;
- *(out++) = bin_to_base64[( block >> 18 ) & 0x3F];
- *(out++) = bin_to_base64[( block >> 12 ) & 0x3F];
- *(out++) = '=';
- *(out++) = '=';
- }
- break;
- case 2 : {
- unsigned int block = *(itr++) << 16;
- block |= *(itr++) << 8;
- *(out++) = bin_to_base64[( block >> 18 ) & 0x3F];
- *(out++) = bin_to_base64[( block >> 12 ) & 0x3F];
- *(out++) = bin_to_base64[( block >> 6 ) & 0x3F];
- *(out++) = '=';
- }
- break;
- }
- }
- return static_cast<std::size_t>((length / 3) * 4) + ((length % 3) > 0 ? 4 : 0);
- }
- inline std::size_t convert_bin_to_base64(const char* begin, const char* end, char* out)
- {
- return convert_bin_to_base64(reinterpret_cast<const unsigned char*>(begin),
- reinterpret_cast<const unsigned char*>(end),
- reinterpret_cast<unsigned char*>(out));
- }
- inline void convert_bin_to_base64(const std::string& binary_data, std::string& output)
- {
- output.resize(std::max<std::size_t>(4,binary_data.size() << 1));
- std::size_t resize = convert_bin_to_base64(binary_data.data(),
- binary_data.data() + binary_data.size(),
- const_cast<char*>(output.data()));
- output.resize(resize);
- }
- inline std::size_t convert_base64_to_bin(const unsigned char* begin, const unsigned char* end, unsigned char* out)
- {
- static const unsigned char base64_to_bin[] =
- {
- 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x00 - 0x07
- 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x08 - 0x0F
- 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x10 - 0x17
- 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x18 - 0x1F
- 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x20 - 0x27
- 0xFF, 0xFF, 0xFF, 0x3E, 0xFF, 0xFF, 0xFF, 0x3F, // 0x28 - 0x2F
- 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, // 0x30 - 0x37
- 0x3C, 0x3D, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x38 - 0x3F
- 0xFF, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, // 0x40 - 0x47
- 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, // 0x48 - 0x4F
- 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, // 0x50 - 0x57
- 0x17, 0x18, 0x19, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x58 - 0x5F
- 0xFF, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, // 0x60 - 0x67
- 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, // 0x68 - 0x6F
- 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, // 0x70 - 0x77
- 0x31, 0x32, 0x33, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x78 - 0x7F
- 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x80 - 0x87
- 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x88 - 0x8F
- 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x90 - 0x97
- 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0x98 - 0x9F
- 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0xA0 - 0xA7
- 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0xA8 - 0xAF
- 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0xB0 - 0xB7
- 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0xB8 - 0xBF
- 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0xC0 - 0xC7
- 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0xC8 - 0xCF
- 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0xD0 - 0xD7
- 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0xD8 - 0xDF
- 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0xE0 - 0xE7
- 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0xE8 - 0xEF
- 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // 0xF0 - 0xF7
- 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF // 0xF8 - 0xFF
- };
- const unsigned char* end_itr = end;
- if ('=' == *(end - 2))
- end_itr = end - 2;
- else if ('=' == *(end - 1))
- end_itr = end - 1;
- const std::size_t length = std::distance(begin,end_itr);
- const std::size_t rounds = length / 4;
- const unsigned char* itr = begin;
- for (std::size_t i = 0; i < rounds; ++i)
- {
- unsigned int block = base64_to_bin[*(itr++)] << 18;
- block |= base64_to_bin[*(itr++)] << 12;
- block |= base64_to_bin[*(itr++)] << 6;
- block |= base64_to_bin[*(itr++)];
- *(out++) = static_cast<unsigned char>(( block >> 16 ) & 0xFF);
- *(out++) = static_cast<unsigned char>(( block >> 8 ) & 0xFF);
- *(out++) = static_cast<unsigned char>(( block ) & 0xFF);
- }
- const std::size_t remainder = (length % 4);
- if (remainder > 0)
- {
- switch (remainder)
- {
- case 2 : {
- unsigned int block = base64_to_bin[*(itr++)] << 18;
- block |= base64_to_bin[*(itr++)] << 12;
- (*out) = static_cast<unsigned char>(( block >> 16 ) & 0xFF);
- }
- break;
- case 3 : {
- unsigned int block = base64_to_bin[*(itr++)] << 18;
- block |= base64_to_bin[*(itr++)] << 12;
- block |= base64_to_bin[*(itr++)] << 6;
- *(out++) = static_cast<unsigned char>(( block >> 16 ) & 0xFF);
- *(out ) = static_cast<unsigned char>(( block >> 8 ) & 0xFF);
- }
- break;
- }
- }
- return static_cast<std::size_t>((3 * length) / 4);
- }
- inline std::size_t convert_base64_to_bin(const char* begin, const char* end, char* out)
- {
- return convert_base64_to_bin(reinterpret_cast<const unsigned char*>(begin),
- reinterpret_cast<const unsigned char*>(end),
- reinterpret_cast<unsigned char*>(out));
- }
- inline void convert_base64_to_bin(const std::string& binary_data, std::string& output)
- {
- output.resize(binary_data.size());
- std::size_t resize = convert_base64_to_bin(binary_data.data(),
- binary_data.data() + binary_data.size(),
- const_cast<char*>(output.data()));
- output.resize(resize);
- }
- inline void convert_to_printable_chars(unsigned char* begin, unsigned char* end)
- {
- static const unsigned char printable_char_table[] =
- {
- 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, // 0x00 - 0x07
- 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, // 0x08 - 0x0F
- 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, // 0x10 - 0x17
- 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, // 0x18 - 0x1F
- 0x2E, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, // 0x20 - 0x27
- 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, // 0x28 - 0x2F
- 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, // 0x30 - 0x37
- 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, // 0x38 - 0x3F
- 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, // 0x40 - 0x47
- 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, // 0x48 - 0x4F
- 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, // 0x50 - 0x57
- 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, // 0x58 - 0x5F
- 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, // 0x60 - 0x67
- 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, // 0x68 - 0x6F
- 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, // 0x70 - 0x77
- 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x2E, // 0x78 - 0x7F
- 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, // 0x80 - 0x87
- 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, // 0x88 - 0x8F
- 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, // 0x90 - 0x97
- 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, // 0x98 - 0x9F
- 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, // 0xA0 - 0xA7
- 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, // 0xA8 - 0xAF
- 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, // 0xB0 - 0xB7
- 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, // 0xB8 - 0xBF
- 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, // 0xC0 - 0xC7
- 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, // 0xC8 - 0xCF
- 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, // 0xD0 - 0xD7
- 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, // 0xD8 - 0xDF
- 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, // 0xE0 - 0xE7
- 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, // 0xE8 - 0xEF
- 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, // 0xF0 - 0xF7
- 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E // 0xF8 - 0xFF
- };
- unsigned char* itr = begin;
- while (end != itr)
- {
- (*itr) = printable_char_table[static_cast<unsigned int>((*itr))];
- ++itr;
- }
- }
- inline void convert_to_printable_chars(char* begin, char* end)
- {
- convert_to_printable_chars(reinterpret_cast<unsigned char*>(begin),
- reinterpret_cast<unsigned char*>(end));
- }
- inline void convert_to_printable_chars(std::string& str)
- {
- convert_to_printable_chars(reinterpret_cast<unsigned char*>(const_cast<char*>(str.data())),
- reinterpret_cast<unsigned char*>(const_cast<char*>(str.data() + str.size())));
- }
- inline void convert_to_uppercase(unsigned char* begin, unsigned char* end)
- {
- std::transform(begin,end,begin,::toupper);
- /*
- unsigned char* itr = begin;
- while (end != itr)
- {
- //(*itr) = std::toupper((*itr), std::locale::classic());
- (*itr) = static_cast<unsigned char>(::toupper(static_cast<int>(*itr)));
- ++itr;
- }
- */
- }
- inline void convert_to_uppercase(char* begin, char* end)
- {
- convert_to_uppercase(reinterpret_cast<unsigned char*>(begin),
- reinterpret_cast<unsigned char*>(end));
- }
- inline void convert_to_uppercase(std::string& str)
- {
- convert_to_uppercase(reinterpret_cast<unsigned char*>(const_cast<char*>(str.data())),
- reinterpret_cast<unsigned char*>(const_cast<char*>(str.data() + str.size())));
- }
- inline void convert_to_lowercase(unsigned char* begin, unsigned char* end)
- {
- std::transform(begin,end,begin,::tolower);
- /*
- unsigned char* itr = begin;
- while (end != itr)
- {
- //(*itr) = std::tolower((*itr), std::locale::classic());
- (*itr) = static_cast<unsigned char>(::tolower(static_cast<int>(*itr)));
- ++itr;
- }
- */
- }
- inline void convert_to_lowercase(char* begin, char* end)
- {
- convert_to_lowercase(reinterpret_cast<unsigned char*>(begin),
- reinterpret_cast<unsigned char*>(end));
- }
- inline void convert_to_lowercase(const char* begin, const char* end)
- {
- convert_to_lowercase(const_cast<char*>(begin),const_cast<char*>(end));
- }
- inline void convert_to_lowercase(std::string& str)
- {
- convert_to_lowercase(reinterpret_cast<unsigned char*>(const_cast<char*>(str.data())),
- reinterpret_cast<unsigned char*>(const_cast<char*>(str.data() + str.size())));
- }
- inline std::string as_lowercase(const std::string& str)
- {
- std::string result = str;
- convert_to_lowercase(result);
- return result;
- }
- inline std::string as_uppercase(const std::string& str)
- {
- std::string result = str;
- convert_to_uppercase(result);
- return result;
- }
- inline bool twoway_bitwise_interleave(const unsigned char* begin1, const unsigned char* end1,
- const unsigned char* begin2, const unsigned char* end2,
- unsigned char* out)
- {
- if (std::distance(begin1,end1) != std::distance(begin2,end2))
- {
- return false;
- }
- static const std::size_t interleave_table_size = 256;
- static const unsigned short interleave_table[interleave_table_size] =
- {
- 0x0000, 0x0001, 0x0004, 0x0005, 0x0010, 0x0011, 0x0014, 0x0015, // 0x00 - 0x07
- 0x0040, 0x0041, 0x0044, 0x0045, 0x0050, 0x0051, 0x0054, 0x0055, // 0x08 - 0x0F
- 0x0100, 0x0101, 0x0104, 0x0105, 0x0110, 0x0111, 0x0114, 0x0115, // 0x10 - 0x17
- 0x0140, 0x0141, 0x0144, 0x0145, 0x0150, 0x0151, 0x0154, 0x0155, // 0x18 - 0x1F
- 0x0400, 0x0401, 0x0404, 0x0405, 0x0410, 0x0411, 0x0414, 0x0415, // 0x20 - 0x27
- 0x0440, 0x0441, 0x0444, 0x0445, 0x0450, 0x0451, 0x0454, 0x0455, // 0x28 - 0x2F
- 0x0500, 0x0501, 0x0504, 0x0505, 0x0510, 0x0511, 0x0514, 0x0515, // 0x30 - 0x37
- 0x0540, 0x0541, 0x0544, 0x0545, 0x0550, 0x0551, 0x0554, 0x0555, // 0x38 - 0x3F
- 0x1000, 0x1001, 0x1004, 0x1005, 0x1010, 0x1011, 0x1014, 0x1015, // 0x40 - 0x47
- 0x1040, 0x1041, 0x1044, 0x1045, 0x1050, 0x1051, 0x1054, 0x1055, // 0x48 - 0x4F
- 0x1100, 0x1101, 0x1104, 0x1105, 0x1110, 0x1111, 0x1114, 0x1115, // 0x50 - 0x57
- 0x1140, 0x1141, 0x1144, 0x1145, 0x1150, 0x1151, 0x1154, 0x1155, // 0x58 - 0x5F
- 0x1400, 0x1401, 0x1404, 0x1405, 0x1410, 0x1411, 0x1414, 0x1415, // 0x60 - 0x67
- 0x1440, 0x1441, 0x1444, 0x1445, 0x1450, 0x1451, 0x1454, 0x1455, // 0x68 - 0x6F
- 0x1500, 0x1501, 0x1504, 0x1505, 0x1510, 0x1511, 0x1514, 0x1515, // 0x70 - 0x77
- 0x1540, 0x1541, 0x1544, 0x1545, 0x1550, 0x1551, 0x1554, 0x1555, // 0x78 - 0x7F
- 0x4000, 0x4001, 0x4004, 0x4005, 0x4010, 0x4011, 0x4014, 0x4015, // 0x80 - 0x87
- 0x4040, 0x4041, 0x4044, 0x4045, 0x4050, 0x4051, 0x4054, 0x4055, // 0x88 - 0x8F
- 0x4100, 0x4101, 0x4104, 0x4105, 0x4110, 0x4111, 0x4114, 0x4115, // 0x90 - 0x97
- 0x4140, 0x4141, 0x4144, 0x4145, 0x4150, 0x4151, 0x4154, 0x4155, // 0x98 - 0x9F
- 0x4400, 0x4401, 0x4404, 0x4405, 0x4410, 0x4411, 0x4414, 0x4415, // 0xA0 - 0xA7
- 0x4440, 0x4441, 0x4444, 0x4445, 0x4450, 0x4451, 0x4454, 0x4455, // 0xA8 - 0xAF
- 0x4500, 0x4501, 0x4504, 0x4505, 0x4510, 0x4511, 0x4514, 0x4515, // 0xB0 - 0xB7
- 0x4540, 0x4541, 0x4544, 0x4545, 0x4550, 0x4551, 0x4554, 0x4555, // 0xB8 - 0xBF
- 0x5000, 0x5001, 0x5004, 0x5005, 0x5010, 0x5011, 0x5014, 0x5015, // 0xC0 - 0xC7
- 0x5040, 0x5041, 0x5044, 0x5045, 0x5050, 0x5051, 0x5054, 0x5055, // 0xC8 - 0xCF
- 0x5100, 0x5101, 0x5104, 0x5105, 0x5110, 0x5111, 0x5114, 0x5115, // 0xD0 - 0xD7
- 0x5140, 0x5141, 0x5144, 0x5145, 0x5150, 0x5151, 0x5154, 0x5155, // 0xD8 - 0xDF
- 0x5400, 0x5401, 0x5404, 0x5405, 0x5410, 0x5411, 0x5414, 0x5415, // 0xE0 - 0xE7
- 0x5440, 0x5441, 0x5444, 0x5445, 0x5450, 0x5451, 0x5454, 0x5455, // 0xE8 - 0xEF
- 0x5500, 0x5501, 0x5504, 0x5505, 0x5510, 0x5511, 0x5514, 0x5515, // 0xF0 - 0xF7
- 0x5540, 0x5541, 0x5544, 0x5545, 0x5550, 0x5551, 0x5554, 0x5555 // 0xF8 - 0xFF
- };
- const unsigned char* itr1 = begin1;
- const unsigned char* itr2 = begin2;
- while (end1 != itr1)
- {
- *(reinterpret_cast<unsigned short*>(out)) = (interleave_table[*(itr2++)] << 1);
- *(reinterpret_cast<unsigned short*>(out)) |= interleave_table[*(itr1++)];
- out += 2;
- }
- return true;
- }
- inline bool twoway_bitwise_interleave(const char* begin1, const char* end1,
- const char* begin2, const char* end2,
- char* out)
- {
- return twoway_bitwise_interleave(reinterpret_cast<const unsigned char*>(begin1),
- reinterpret_cast<const unsigned char*>(end1),
- reinterpret_cast<const unsigned char*>(begin2),
- reinterpret_cast<const unsigned char*>(end2),
- reinterpret_cast<unsigned char*>(out));
- }
- inline bool twoway_bitwise_interleave(const std::string& str1,
- const std::string& str2,
- std::string& out)
- {
- if (str1.size() != str2.size())
- {
- return false;
- }
- out.resize(str1.size());
- return twoway_bitwise_interleave(str1.data(),str1.data() + str1.size(),
- str2.data(),str2.data() + str2.size(),
- const_cast<char*>(out.data()));
- }
- template <std::size_t n>
- struct interleave_ary;
- template<> struct interleave_ary<sizeof(unsigned short)> { typedef unsigned short type; };
- template<> struct interleave_ary<sizeof(unsigned int )> { typedef unsigned int type; };
- template<> struct interleave_ary<sizeof(unsigned long long int)> { typedef unsigned long long int type; };
- template <std::size_t n>
- inline void create_nway_interleave_table(typename interleave_ary<n>::type table[256])
- {
- typedef typename interleave_ary<n>::type type;
- const type diff = static_cast<type>(n - 1);
- for (type i = static_cast<type>(0); i < static_cast<type>(256); ++i)
- {
- table[i] = 0x00;
- for (type j = static_cast<type>(0); j < static_cast<type>(8); ++j)
- {
- table[i] |= (i & (1 << j)) << (j * diff);
- }
- }
- }
- namespace bitwise_operation { enum type { eAND, eOR, eXOR }; }
- inline void bitwise_transform(const bitwise_operation::type& operation,
- const unsigned char* begin1, const unsigned char* end1,
- const unsigned char* begin2,
- unsigned char* out)
- {
- const unsigned char* itr1 = begin1;
- const unsigned char* itr2 = begin2;
- switch (operation)
- {
- case bitwise_operation::eAND : while (itr1 != end1) { *(out++) = *(itr1++) & *(itr2++); } return;
- case bitwise_operation::eOR : while (itr1 != end1) { *(out++) = *(itr1++) | *(itr2++); } return;
- case bitwise_operation::eXOR : while (itr1 != end1) { *(out++) = *(itr1++) ^ *(itr2++); } return;
- }
- }
- inline void bitwise_transform(const bitwise_operation::type& operation,
- const char* begin1, const char* end1,
- const char* begin2,
- char* out)
- {
- bitwise_transform(operation,
- reinterpret_cast<const unsigned char*>(begin1),
- reinterpret_cast<const unsigned char*>(end1),
- reinterpret_cast<const unsigned char*>(begin2),
- reinterpret_cast<unsigned char*>(out));
- }
- inline void bitwise_transform(const bitwise_operation::type& operation,
- const std::string& str1,
- const std::string& str2,
- std::string& out)
- {
- if (str1.size() != str2.size()) return;
- out.resize(str1.size());
- bitwise_transform(operation,
- str1.data(),str1.data() + str1.size(),
- str2.data(),
- const_cast<char*>(out.data()));
- }
- inline std::size_t high_bit_count(const unsigned char c)
- {
- static const std::size_t high_bits_in_char[256] =
- {
- 0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,
- 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
- 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
- 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
- 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
- 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
- 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
- 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
- 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
- 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
- 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
- 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
- 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
- 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
- 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
- 4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8
- };
- return high_bits_in_char[c];
- }
- inline std::size_t high_bit_count(const unsigned short& s)
- {
- const unsigned char* ptr = reinterpret_cast<const unsigned char*>(&s);
- return high_bit_count(*(ptr + 0)) + high_bit_count(*(ptr + 1));
- }
- inline std::size_t high_bit_count(const unsigned int& i)
- {
- const unsigned char* ptr = reinterpret_cast<const unsigned char*>(&i);
- return high_bit_count(*(ptr + 0)) + high_bit_count(*(ptr + 1)) +
- high_bit_count(*(ptr + 2)) + high_bit_count(*(ptr + 3));
- }
- inline std::size_t high_bit_count(const long long int& ll)
- {
- const unsigned char* ptr = reinterpret_cast<const unsigned char*>(&ll);
- return high_bit_count(*(ptr + 0)) + high_bit_count(*(ptr + 1)) +
- high_bit_count(*(ptr + 2)) + high_bit_count(*(ptr + 3)) +
- high_bit_count(*(ptr + 4)) + high_bit_count(*(ptr + 5)) +
- high_bit_count(*(ptr + 6)) + high_bit_count(*(ptr + 7));
- }
- inline std::size_t high_bit_count(const unsigned char* begin, const unsigned char* end)
- {
- std::size_t count = 0;
- const unsigned char* itr = begin;
- while (end != itr)
- {
- count += high_bit_count(*itr++);
- }
- return count;
- }
- inline std::size_t high_bit_count(const char* begin, const char* end)
- {
- return high_bit_count(reinterpret_cast<const unsigned char*>(begin),
- reinterpret_cast<const unsigned char*>(end));
- }
- inline std::size_t high_bit_count(const std::string& str)
- {
- return high_bit_count(str.data(),str.data() + str.size());
- }
- inline bool bit_state(const std::size_t& index, const unsigned char* ptr)
- {
- static const unsigned char bit_mask[] =
- {
- 0x01, //00000001
- 0x02, //00000010
- 0x04, //00000100
- 0x08, //00001000
- 0x10, //00010000
- 0x20, //00100000
- 0x40, //01000000
- 0x80 //10000000
- };
- return (0 != (ptr[(index >> 3)] & bit_mask[index & 7]));
- }
- inline void set_bit_high(const std::size_t& index, unsigned char* const ptr)
- {
- static const unsigned char bit_mask[] =
- {
- 0x01, //00000001
- 0x02, //00000010
- 0x04, //00000100
- 0x08, //00001000
- 0x10, //00010000
- 0x20, //00100000
- 0x40, //01000000
- 0x80 //10000000
- };
- ptr[(index >> 3)] |= bit_mask[index & 7];
- }
- inline void set_bit_low(const std::size_t& index, unsigned char* const ptr)
- {
- static const unsigned char bit_mask[] =
- {
- 0xFE, //11111110
- 0xFD, //11111101
- 0xFB, //11111011
- 0xF7, //11110111
- 0xEF, //11101111
- 0xDF, //11011111
- 0xBF, //10111111
- 0x7F //01111111
- };
- ptr[(index >> 3)] &= bit_mask[index & 7];
- }
- inline std::size_t hamming_distance(const unsigned char* begin1, const unsigned char* end1,
- const unsigned char* begin2, const unsigned char* end2)
- {
- if (std::distance(begin1,end1) != std::distance(begin2,end2))
- {
- return std::numeric_limits<std::size_t>::max();
- }
- std::size_t distance = 0;
- const unsigned char* itr1 = begin1;
- const unsigned char* itr2 = begin2;
- while (end1 != itr1)
- {
- distance += high_bit_count(static_cast<unsigned char>(((*itr1++) ^ (*itr2++)) & 0xFF));
- }
- return distance;
- }
- inline std::size_t hamming_distance(const char* begin1, const char* end1,
- const char* begin2, const char* end2)
- {
- return hamming_distance(reinterpret_cast<const unsigned char*>(begin1),
- reinterpret_cast<const unsigned char*>(end1),
- reinterpret_cast<const unsigned char*>(begin2),
- reinterpret_cast<const unsigned char*>(end2));
- }
- inline std::size_t hamming_distance(const std::string& str1, const std::string& str2)
- {
- return hamming_distance(str1.data(), str1.data() + str1.size(),
- str2.data(), str2.data() + str2.size());
- }
- template <typename Iterator>
- inline std::size_t hamming_distance_elementwise(const Iterator begin1, const Iterator end1,
- const Iterator begin2, const Iterator end2)
- {
- if (std::distance(begin1,end1) != std::distance(begin2,end2))
- {
- return std::numeric_limits<std::size_t>::max();
- }
- std::size_t distance = 0;
- Iterator itr1 = begin1;
- Iterator itr2 = begin2;
- while (end1 != itr1)
- {
- if ((*itr1) != (*itr2))
- ++distance;
- }
- return distance;
- }
- inline std::size_t hamming_distance_elementwise(const std::string& str1, const std::string& str2)
- {
- return hamming_distance_elementwise(str1.data(), str1.data() + str1.size(),
- str2.data(), str2.data() + str2.size());
- }
- class token_grid
- {
- public:
- typedef const unsigned char* iterator_t;
- typedef unsigned int index_t;
- typedef std::pair<iterator_t,iterator_t> range_t;
- typedef std::deque<range_t> token_list_t;
- typedef std::pair<index_t,index_t> row_index_range_t;
- typedef std::deque<row_index_range_t> row_index_t;
- typedef std::pair<index_t,index_t> row_range_t;
- typedef std::pair<index_t,index_t> col_range_t;
- private:
- struct store
- {
- store()
- : max_column(0)
- {}
- token_list_t token_list;
- row_index_t row_index;
- std::size_t max_column;
- inline void clear()
- {
- token_list.clear();
- row_index.clear();
- }
- inline range_t operator()(const std::size_t& col, const std::size_t& row) const
- {
- if (row < row_index.size())
- {
- const row_index_range_t& r = row_index[row];
- if (col < (r.second - r.first + 1))
- return *(token_list.begin() + (r.first + col));
- else
- return null_range();
- }
- else
- return null_range();
- }
- inline bool remove_row(const std::size_t& row)
- {
- if (row >= row_index.size()) return false;
- row_index_range_t& r = row_index[row];
- std::size_t number_of_tokens = r.second - r.first + 1;
- token_list_t::iterator remove_begin = token_list.begin() + r.first;
- token_list_t::iterator remove_end = token_list.begin() + r.first + number_of_tokens;
- token_list.erase(remove_begin,remove_end);
- row_index.erase(row_index.begin() + row);
- for (std::size_t i = row; i < row_index.size(); ++i)
- {
- row_index_range_t& r = row_index[i];
- r.first -= static_cast<unsigned int>(number_of_tokens);
- r.second -= static_cast<unsigned int>(number_of_tokens);
- }
- return true;
- }
- inline std::size_t token_count(const row_index_range_t& r) const
- {
- return (r.second - r.first + 1);
- }
- inline std::size_t token_count(const std::size_t& index) const
- {
- return token_count(row_index[index]);
- }
- inline bool remove_row_range(const std::size_t& r0, const std::size_t& r1)
- {
- if (r0 > r1)
- return false;
- else if (r0 >= row_index.size())
- return false;
- else if (r1 >= row_index.size())
- return false;
- std::size_t number_of_tokens = 0;
- for (std::size_t i = r0; i <= r1; ++i)
- {
- row_index_range_t& r = row_index[i];
- number_of_tokens += token_count(r);
- }
- row_index_range_t rr0 = row_index[r0];
- token_list_t::iterator remove_begin = token_list.begin() + rr0.first;
- token_list_t::iterator remove_end = token_list.begin() + rr0.first + number_of_tokens;
- token_list.erase(remove_begin,remove_end);
- row_index.erase(row_index.begin() + r0,row_index.begin() + r0 + (r1 - r0 + 1));
- for (std::size_t i = r0; i < row_index.size(); ++i)
- {
- row_index_range_t& r = row_index[i];
- r.first -= static_cast<unsigned int>(number_of_tokens);
- r.second -= static_cast<unsigned int>(number_of_tokens);
- }
- return true;
- }
- struct remove_column_impl
- {
- std::size_t column;
- std::size_t counter;
- std::size_t remainder;
- std::size_t current_row;
- inline void update(store& idx)
- {
- current_row++;
- while (current_row < idx.row_index.size())
- {
- std::size_t number_of_tokens = idx.token_count(current_row);
- if (number_of_tokens > column)
- break;
- counter += number_of_tokens;
- ++current_row;
- }
- if (current_row < idx.row_index.size())
- {
- counter += column + remainder;
- row_index_range_t& r = idx.row_index[current_row];
- remainder = (r.second - r.first) - column;
- }
- else
- counter = std::numeric_limits<std::size_t>::max();
- }
- inline void process(store& idx)
- {
- token_list_t::iterator itr1 = idx.token_list.begin();
- token_list_t::iterator itr2 = idx.token_list.begin();
- token_list_t::iterator end = idx.token_list.end();
- counter = 0;
- remainder = 0;
- current_row = static_cast<std::size_t>(-1);
- update(idx);
- while (end != itr1)
- {
- while ((end != itr1) && (0 != counter))
- {
- if (itr1 != itr2)
- {
- (*itr2) = (*itr1);
- }
- ++itr1;
- ++itr2;
- --counter;
- }
- if (0 == counter)
- {
- update(idx);
- ++itr1;
- }
- }
- std::size_t remove_count = 0;
- idx.max_column = std::numeric_limits<std::size_t>::min();
- for (std::size_t i = 0; i < idx.row_index.size(); ++i)
- {
- row_index_range_t& r = idx.row_index[i];
- std::size_t token_count = (r.second - r.first + 1);
- r.first -= static_cast<unsigned int>(remove_count);
- if (token_count > column)
- {
- ++remove_count;
- }
- r.second -= static_cast<unsigned int>(remove_count);
- token_count = (r.second - r.first + 1);
- if (token_count > idx.max_column)
- idx.max_column = token_count;
- }
- idx.token_list.resize(idx.token_list.size() - remove_count);
- }
- };
- inline bool remove_column(const std::size_t& column)
- {
- if (column >= max_column) return false;
- remove_column_impl rc;
- rc.column = column;
- rc.process(*this);
- return true;
- }
- inline static range_t null_range()
- {
- static const range_t null_range_ = range_t(reinterpret_cast<const unsigned char*>(0),
- reinterpret_cast<const unsigned char*>(0));
- return null_range_;
- }
- };
- template <typename DelimiterPredicate = multiple_char_delimiter_predicate>
- struct row_processor
- {
- row_processor(store& idx,
- DelimiterPredicate& tp,
- const split_options::type split_mode = split_options::compress_delimiters)
- : idx_(idx),
- row_start_index_(0),
- row_end_index_(0),
- token_predicate_(tp),
- split_mode_(split_mode)
- {
- idx_.max_column = std::numeric_limits<std::size_t>::min();
- }
- inline void operator()(const range_t& range)
- {
- if (0 == std::distance(range.first,range.second))
- return;
- row_start_index_ = idx_.token_list.size();
- std::size_t token_count = split(token_predicate_,
- range.first,range.second,
- std::back_inserter(idx_.token_list),
- split_mode_);
- row_end_index_ = row_start_index_ + token_count - 1;
- idx_.row_index.push_back(std::make_pair(row_start_index_,row_end_index_));
- if (token_count > idx_.max_column)
- idx_.max_column = token_count;
- }
- row_processor<DelimiterPredicate> operator=(const row_processor<DelimiterPredicate>&);
- store& idx_;
- std::size_t row_start_index_;
- std::size_t row_end_index_;
- DelimiterPredicate& token_predicate_;
- split_options::type split_mode_;
- };
- public:
- inline row_range_t range(std::size_t lower_bound,
- std::size_t upper_bound = std::numeric_limits<std::size_t>::max()) const
- {
- if (upper_bound == std::numeric_limits<std::size_t>::max())
- {
- upper_bound = dsv_index_.token_list.size();
- }
- else if (upper_bound > dsv_index_.token_list.size())
- {
- return row_range_t(std::numeric_limits<std::size_t>::max(),std::numeric_limits<std::size_t>::max());
- }
- else if (lower_bound > upper_bound)
- {
- return row_range_t(std::numeric_limits<std::size_t>::max(),std::numeric_limits<std::size_t>::max());
- }
- return row_range_t(lower_bound,upper_bound);
- }
- struct options
- {
- options()
- : row_split_option(split_options::compress_delimiters),
- column_split_option(split_options::compress_delimiters),
- row_delimiters("\n\r"),
- column_delimiters(",|;\t "),
- support_dquotes(false)
- {}
- options(split_options::type sro,
- split_options::type sco,
- const std::string& rd,
- const std::string& cd,
- const bool support_dq = false)
- : row_split_option(sro),
- column_split_option(sco),
- row_delimiters(rd),
- column_delimiters(cd),
- support_dquotes(support_dq)
- {}
- inline options& set_column_split_option(const split_options::type& option)
- {
- column_split_option = option;
- return *this;
- }
- inline options& set_row_split_option(const split_options::type& option)
- {
- row_split_option = option;
- return *this;
- }
- inline options& set_column_delimiters(const std::string& delimiters)
- {
- column_delimiters = delimiters;
- return *this;
- }
- inline options& set_row_delimiters(const std::string& delimiters)
- {
- row_delimiters = delimiters;
- return *this;
- }
- split_options::type row_split_option;
- split_options::type column_split_option;
- std::string row_delimiters;
- std::string column_delimiters;
- bool support_dquotes;
- };
- class row_type
- {
- private:
- typedef std::pair<bool,row_type*> row_pair_type;
- public:
- row_type()
- : index_(std::numeric_limits<std::size_t>::max()),
- size_(0)
- {}
- row_type(const std::size_t& index,
- const store& dsv_index)
- : index_(index),
- size_ (dsv_index.token_count(index)),
- begin_(dsv_index.token_list.begin() + dsv_index.row_index[index].first)
- {}
- template <typename T>
- inline T operator[](const std::size_t& index) const
- {
- const range_t& range = *(begin_ + index);
- return string_to_type_converter<T>(range.first,range.second);
- }
- template <typename T>
- inline T get(const std::size_t& index) const
- {
- return operator[]<T>(index);
- }
- inline col_range_t all_columns() const
- {
- return col_range_t(0,static_cast<index_t>(size()));
- }
- inline range_t range() const
- {
- return range_t((*begin_).first,(*(begin_ + (size_ - 1))).second);
- }
- inline range_t token(const std::size_t& index) const
- {
- return *(begin_ + index);
- }
- inline std::size_t index() const
- {
- return index_;
- }
- inline std::size_t size() const
- {
- return size_;
- }
- inline std::size_t raw_length() const
- {
- std::size_t result = 0;
- token_list_t::const_iterator itr = begin_;
- for (std::size_t i = 0; i < size_; ++i, ++itr)
- {
- const range_t& range = (*itr);
- result += std::distance(range.first,range.second);
- }
- return result;
- }
- inline std::size_t raw_length(const std::size_t& column_index) const
- {
- const range_t& range = *(begin_ + column_index);
- return std::distance(range.first,range.second);
- }
- inline std::string as_string() const
- {
- std::string result;
- result.reserve(std::distance(begin_->first,(begin_ + (size_ - 1))->second));
- token_list_t::const_iterator itr = begin_;
- for (std::size_t i = 0; i < size_; ++i, ++itr)
- {
- const range_t& range = (*itr);
- result.append(range.first,range.second);
- }
- return result;
- }
- inline void as_string(std::string& out) const
- {
- out = as_string();
- }
- template <typename T0, typename T1, typename T2, typename T3,
- typename T4, typename T5, typename T6, typename T7,
- typename T8, typename T9>
- inline bool parse_with_index(const std::size_t& col0, const std::size_t& col1,
- const std::size_t& col2, const std::size_t& col3,
- const std::size_t& col4, const std::size_t& col5,
- const std::size_t& col6, const std::size_t& col7,
- const std::size_t& col8, const std::size_t& col9,
- T0& t0, T1& t1, T2& t2, T3& t3, T4& t4, T5& t5,
- T6& t6, T7& t7, T8& t8, T9& t9) const
- {
- if (!process(*(begin_ + col0),t0)) return false;
- if (!process(*(begin_ + col1),t1)) return false;
- if (!process(*(begin_ + col2),t2)) return false;
- if (!process(*(begin_ + col3),t3)) return false;
- if (!process(*(begin_ + col4),t4)) return false;
- if (!process(*(begin_ + col5),t5)) return false;
- if (!process(*(begin_ + col6),t6)) return false;
- if (!process(*(begin_ + col7),t7)) return false;
- if (!process(*(begin_ + col8),t8)) return false;
- if (!process(*(begin_ + col9),t9)) return false;
- return true;
- }
- template <typename T0, typename T1, typename T2, typename T3,
- typename T4, typename T5, typename T6, typename T7,
- typename T8>
- inline bool parse_with_index(const std::size_t& col0, const std::size_t& col1,
- const std::size_t& col2, const std::size_t& col3,
- const std::size_t& col4, const std::size_t& col5,
- const std::size_t& col6, const std::size_t& col7,
- const std::size_t& col8,
- T0& t0, T1& t1, T2& t2, T3& t3, T4& t4, T5& t5,
- T6& t6, T7& t7, T8& t8) const
- {
- if (!process(*(begin_ + col0),t0)) return false;
- if (!process(*(begin_ + col1),t1)) return false;
- if (!process(*(begin_ + col2),t2)) return false;
- if (!process(*(begin_ + col3),t3)) return false;
- if (!process(*(begin_ + col4),t4)) return false;
- if (!process(*(begin_ + col5),t5)) return false;
- if (!process(*(begin_ + col6),t6)) return false;
- if (!process(*(begin_ + col7),t7)) return false;
- if (!process(*(begin_ + col8),t8)) return false;
- return true;
- }
- template <typename T0, typename T1, typename T2, typename T3,
- typename T4, typename T5, typename T6, typename T7>
- inline bool parse_with_index(const std::size_t& col0, const std::size_t& col1,
- const std::size_t& col2, const std::size_t& col3,
- const std::size_t& col4, const std::size_t& col5,
- const std::size_t& col6, const std::size_t& col7,
- T0& t0, T1& t1, T2& t2, T3& t3, T4& t4, T5& t5,
- T6& t6, T7& t7) const
- {
- if (!process(*(begin_ + col0),t0)) return false;
- if (!process(*(begin_ + col1),t1)) return false;
- if (!process(*(begin_ + col2),t2)) return false;
- if (!process(*(begin_ + col3),t3)) return false;
- if (!process(*(begin_ + col4),t4)) return false;
- if (!process(*(begin_ + col5),t5)) return false;
- if (!process(*(begin_ + col6),t6)) return false;
- if (!process(*(begin_ + col7),t7)) return false;
- return true;
- }
- template <typename T0, typename T1, typename T2, typename T3,
- typename T4, typename T5, typename T6>
- inline bool parse_with_index(const std::size_t& col0, const std::size_t& col1,
- const std::size_t& col2, const std::size_t& col3,
- const std::size_t& col4, const std::size_t& col5,
- const std::size_t& col6,
- T0& t0, T1& t1, T2& t2, T3& t3, T4& t4, T5& t5,
- T6& t6) const
- {
- if (!process(*(begin_ + col0),t0)) return false;
- if (!process(*(begin_ + col1),t1)) return false;
- if (!process(*(begin_ + col2),t2)) return false;
- if (!process(*(begin_ + col3),t3)) return false;
- if (!process(*(begin_ + col4),t4)) return false;
- if (!process(*(begin_ + col5),t5)) return false;
- if (!process(*(begin_ + col6),t6)) return false;
- return true;
- }
- template <typename T0, typename T1, typename T2, typename T3,
- typename T4, typename T5>
- inline bool parse_with_index(const std::size_t& col0, const std::size_t& col1,
- const std::size_t& col2, const std::size_t& col3,
- const std::size_t& col4, const std::size_t& col5,
- T0& t0, T1& t1, T2& t2, T3& t3, T4& t4, T5& t5) const
- {
- if (!process(*(begin_ + col0),t0)) return false;
- if (!process(*(begin_ + col1),t1)) return false;
- if (!process(*(begin_ + col2),t2)) return false;
- if (!process(*(begin_ + col3),t3)) return false;
- if (!process(*(begin_ + col4),t4)) return false;
- if (!process(*(begin_ + col5),t5)) return false;
- return true;
- }
- template <typename T0, typename T1, typename T2, typename T3, typename T4>
- inline bool parse_with_index(const std::size_t& col0, const std::size_t& col1,
- const std::size_t& col2, const std::size_t& col3,
- const std::size_t& col4,
- T0& t0, T1& t1, T2& t2, T3& t3, T4& t4) const
- {
- if (!process(*(begin_ + col0),t0)) return false;
- if (!process(*(begin_ + col1),t1)) return false;
- if (!process(*(begin_ + col2),t2)) return false;
- if (!process(*(begin_ + col3),t3)) return false;
- if (!process(*(begin_ + col4),t4)) return false;
- return true;
- }
- template <typename T0, typename T1, typename T2, typename T3>
- inline bool parse_with_index(const std::size_t& col0, const std::size_t& col1,
- const std::size_t& col2, const std::size_t& col3,
- T0& t0, T1& t1, T2& t2, T3& t3) const
- {
- if (!process(*(begin_ + col0),t0)) return false;
- if (!process(*(begin_ + col1),t1)) return false;
- if (!process(*(begin_ + col2),t2)) return false;
- if (!process(*(begin_ + col3),t3)) return false;
- return true;
- }
- template <typename T0, typename T1, typename T2>
- inline bool parse_with_index(const std::size_t& col0, const std::size_t& col1,
- const std::size_t& col2,
- T0& t0, T1& t1, T2& t2) const
- {
- if (!process(*(begin_ + col0),t0)) return false;
- if (!process(*(begin_ + col1),t1)) return false;
- if (!process(*(begin_ + col2),t2)) return false;
- return true;
- }
- template <typename T0, typename T1>
- inline bool parse_with_index(const std::size_t& col0, const std::size_t& col1,
- T0& t0, T1& t1) const
- {
- if (!process(*(begin_ + col0),t0)) return false;
- if (!process(*(begin_ + col1),t1)) return false;
- return true;
- }
- template <typename T>
- inline bool parse_with_index(const std::size_t& col, T& t) const
- {
- return process(*(begin_ + col),t);
- }
- template <typename T0, typename T1, typename T2, typename T3,
- typename T4, typename T5, typename T6, typename T7,
- typename T8, typename T9 >
- inline bool parse(T0& t0, T1& t1, T2& t2, T3& t3,
- T4& t4, T5& t5, T6& t6, T7& t7,
- T8& t8, T9& t9) const
- {
- if (!process(*(begin_ + 0),t0)) return false;
- if (!process(*(begin_ + 1),t1)) return false;
- if (!process(*(begin_ + 2),t2)) return false;
- if (!process(*(begin_ + 3),t3)) return false;
- if (!process(*(begin_ + 4),t4)) return false;
- if (!process(*(begin_ + 5),t5)) return false;
- if (!process(*(begin_ + 6),t6)) return false;
- if (!process(*(begin_ + 7),t7)) return false;
- if (!process(*(begin_ + 8),t8)) return false;
- if (!process(*(begin_ + 9),t9)) return false;
- return true;
- }
- template <typename T0, typename T1, typename T2, typename T3,
- typename T4, typename T5, typename T6, typename T7,
- typename T8>
- inline bool parse(T0& t0, T1& t1, T2& t2, T3& t3,
- T4& t4, T5& t5, T6& t6, T7& t7,
- T8& t8) const
- {
- if (!process(*(begin_ + 0),t0)) return false;
- if (!process(*(begin_ + 1),t1)) return false;
- if (!process(*(begin_ + 2),t2)) return false;
- if (!process(*(begin_ + 3),t3)) return false;
- if (!process(*(begin_ + 4),t4)) return false;
- if (!process(*(begin_ + 5),t5)) return false;
- if (!process(*(begin_ + 6),t6)) return false;
- if (!process(*(begin_ + 7),t7)) return false;
- if (!process(*(begin_ + 8),t8)) return false;
- return true;
- }
- template <typename T0, typename T1, typename T2, typename T3,
- typename T4, typename T5, typename T6, typename T7>
- inline bool parse(T0& t0, T1& t1, T2& t2, T3& t3,
- T4& t4, T5& t5, T6& t6, T7& t7) const
- {
- if (!process(*(begin_ + 0),t0)) return false;
- if (!process(*(begin_ + 1),t1)) return false;
- if (!process(*(begin_ + 2),t2)) return false;
- if (!process(*(begin_ + 3),t3)) return false;
- if (!process(*(begin_ + 4),t4)) return false;
- if (!process(*(begin_ + 5),t5)) return false;
- if (!process(*(begin_ + 6),t6)) return false;
- if (!process(*(begin_ + 7),t7)) return false;
- return true;
- }
- template <typename T0, typename T1, typename T2, typename T3,
- typename T4, typename T5, typename T6>
- inline bool parse(T0& t0, T1& t1, T2& t2, T3& t3,
- T4& t4, T5& t5, T6& t6) const
- {
- if (!process(*(begin_ + 0),t0)) return false;
- if (!process(*(begin_ + 1),t1)) return false;
- if (!process(*(begin_ + 2),t2)) return false;
- if (!process(*(begin_ + 3),t3)) return false;
- if (!process(*(begin_ + 4),t4)) return false;
- if (!process(*(begin_ + 5),t5)) return false;
- if (!process(*(begin_ + 6),t6)) return false;
- return true;
- }
- template <typename T0, typename T1, typename T2, typename T3,
- typename T4, typename T5>
- inline bool parse(T0& t0, T1& t1, T2& t2, T3& t3,
- T4& t4, T5& t5) const
- {
- if (!process(*(begin_ + 0),t0)) return false;
- if (!process(*(begin_ + 1),t1)) return false;
- if (!process(*(begin_ + 2),t2)) return false;
- if (!process(*(begin_ + 3),t3)) return false;
- if (!process(*(begin_ + 4),t4)) return false;
- if (!process(*(begin_ + 5),t5)) return false;
- return true;
- }
- template <typename T0, typename T1,
- typename T2, typename T3,typename T4>
- inline bool parse(T0& t0, T1& t1, T2& t2, T3& t3, T4& t4) const
- {
- if (!process(*(begin_ + 0),t0)) return false;
- if (!process(*(begin_ + 1),t1)) return false;
- if (!process(*(begin_ + 2),t2)) return false;
- if (!process(*(begin_ + 3),t3)) return false;
- if (!process(*(begin_ + 4),t4)) return false;
- return true;
- }
- template <typename T0, typename T1,
- typename T2, typename T3>
- inline bool parse(T0& t0, T1& t1, T2& t2, T3& t3) const
- {
- if (!process(*(begin_ + 0),t0)) return false;
- if (!process(*(begin_ + 1),t1)) return false;
- if (!process(*(begin_ + 2),t2)) return false;
- if (!process(*(begin_ + 3),t3)) return false;
- return true;
- }
- template <typename T0, typename T1, typename T2>
- inline bool parse(T0& t0, T1& t1, T2& t2) const
- {
- if (!process(*(begin_ + 0),t0)) return false;
- if (!process(*(begin_ + 1),t1)) return false;
- if (!process(*(begin_ + 2),t2)) return false;
- return true;
- }
- template <typename T0, typename T1>
- inline bool parse(T0& t0, T1& t1) const
- {
- if (!process(*(begin_ + 0),t0)) return false;
- if (!process(*(begin_ + 1),t1)) return false;
- return true;
- }
- template <typename T0>
- inline bool parse(T0& t) const
- {
- return process(*begin_,t);
- }
- template <typename T, typename OutputIterator>
- inline void parse(OutputIterator out) const
- {
- token_list_t::const_iterator itr = begin_;
- const token_list_t::const_iterator end = begin_ + size_;
- while (end != itr)
- {
- const range_t& range = (*itr);
- *(out++) = string_to_type_converter<T>(range.first,range.second);
- ++itr;
- }
- }
- bool validate_column_range(const col_range_t& range) const
- {
- if ((range.first > size()) || (range.second > size()))
- return false;
- else if (range.first > range.second)
- return false;
- else
- return true;
- }
- col_range_t range(const std::size_t& lower_bound,
- const std::size_t& upper_bound = std::numeric_limits<std::size_t>::max()) const
- {
- if (std::numeric_limits<std::size_t>::max() != upper_bound)
- return col_range_t(lower_bound,upper_bound);
- else
- return col_range_t(lower_bound,static_cast<index_t>(size()));
- }
- template <typename T,
- typename Allocator,
- template <typename,typename> class Sequence>
- inline bool parse(const col_range_t& range,
- Sequence<T,Allocator>& sequence) const
- {
- if (!validate_column_range(range))
- return false;
- token_list_t::const_iterator itr = (begin_ + range.first);
- token_list_t::const_iterator end = (begin_ + range.second);
- T t;
- while (end != itr)
- {
- const range_t& range = (*itr);
- if (string_to_type_converter(range.first,range.second,t))
- sequence.push_back(t);
- else
- return false;
- ++itr;
- }
- return true;
- }
- template <typename T,
- typename Comparator,
- typename Allocator>
- inline bool parse(const col_range_t& range,
- std::set<T,Comparator,Allocator>& set) const
- {
- if (!validate_column_range(range))
- return false;
- token_list_t::const_iterator itr = (begin_ + range.first);
- token_list_t::const_iterator end = (begin_ + range.second);
- T t;
- while (end != itr)
- {
- const range_t& range = (*itr);
- if (string_to_type_converter(range.first,range.second,t))
- set.insert(t);
- else
- return false;
- ++itr;
- }
- return true;
- }
- template <typename T,
- typename Comparator,
- typename Allocator>
- inline bool parse(const col_range_t& range,
- std::multiset<T,Comparator,Allocator>& multiset) const
- {
- if (!validate_column_range(range))
- return false;
- token_list_t::const_iterator itr = (begin_ + range.first);
- token_list_t::const_iterator end = (begin_ + range.second);
- T t;
- while (end != itr)
- {
- const range_t& range = (*itr);
- if (string_to_type_converter(range.first,range.second,t))
- multiset.insert(t);
- else
- return false;
- ++itr;
- }
- return true;
- }
- template <typename T, typename Container>
- inline bool parse(const col_range_t& range,
- std::queue<T,Container>& queue) const
- {
- if (!validate_column_range(range))
- return false;
- token_list_t::const_iterator itr = (begin_ + range.first);
- token_list_t::const_iterator end = (begin_ + range.second);
- T t;
- while (end != itr)
- {
- const range_t& range = (*itr);
- if (string_to_type_converter(range.first,range.second,t))
- queue.push(t);
- else
- return false;
- ++itr;
- }
- return true;
- }
- template <typename T, typename Container>
- inline bool parse(const col_range_t& range,
- std::stack<T,Container>& stack) const
- {
- if (!validate_column_range(range))
- return false;
- token_list_t::const_iterator itr = (begin_ + range.first);
- token_list_t::const_iterator end = (begin_ + range.second);
- T t;
- while (end != itr)
- {
- const range_t& range = (*itr);
- if (string_to_type_converter(range.first,range.second,t))
- stack.push(t);
- else
- return false;
- ++itr;
- }
- return true;
- }
- template <typename T,
- typename Container,
- typename Comparator>
- inline bool parse(const col_range_t& range,
- std::priority_queue<T,Container,Comparator>& priority_queue) const
- {
- if (!validate_column_range(range))
- return false;
- token_list_t::const_iterator itr = (begin_ + range.first);
- token_list_t::const_iterator end = (begin_ + range.second);
- T t;
- while (end != itr)
- {
- const range_t& range = (*itr);
- if (string_to_type_converter(range.first,range.second,t))
- priority_queue.push(t);
- else
- return false;
- ++itr;
- }
- return true;
- }
- template <typename T,
- typename Allocator,
- template <typename,typename> class Sequence>
- inline bool parse(Sequence<T,Allocator>& sequence) const
- {
- return parse(range(0),sequence);
- }
- template <typename T,
- typename Comparator,
- typename Allocator>
- inline bool parse(std::set<T,Comparator,Allocator>& set) const
- {
- return parse(range(0),set);
- }
- template <typename T,
- typename Comparator,
- typename Allocator>
- inline bool parse(std::multiset<T,Comparator,Allocator>& multiset) const
- {
- return parse(range(0),multiset);
- }
- template <typename T, typename Container>
- inline bool parse(std::queue<T,Container>& queue) const
- {
- return parse(range(0),queue);
- }
- template <typename T, typename Container>
- inline bool parse(std::stack<T,Container>& stack) const
- {
- return parse(range(0),stack);
- }
- template <typename T,
- typename Container,
- typename Comparator>
- inline bool parse(std::priority_queue<T,Container,Comparator>& priority_queue) const
- {
- return parse(range(0),priority_queue);
- }
- template <typename T,
- typename Allocator,
- template <typename,typename> class Sequence>
- inline std::size_t parse_n(const std::size_t& n, Sequence<T,Allocator>& sequence) const
- {
- if (0 == n) return 0;
- T t;
- std::size_t count = 0;
- token_list_t::const_iterator itr = begin_;
- const token_list_t::const_iterator end = begin_ + size_;
- while (end != itr)
- {
- const range_t& range = (*itr);
- if (!string_to_type_converter(range.first,range.second,t))
- return false;
- else
- sequence.push_back(t);
- if (n == (++count))
- break;
- ++itr;
- }
- return count;
- }
- template <typename T, typename OutputIterator>
- inline void parse_checked(OutputIterator out) const
- {
- T value;
- token_list_t::const_iterator itr = begin_;
- const token_list_t::const_iterator end = begin_ + size_;
- while (end != itr)
- {
- const range_t& range = (*itr);
- if (string_to_type_converter(range.first,range.second,value))
- {
- *(out++) = value;
- }
- ++itr;
- }
- }
- template <typename T,
- typename Allocator,
- template <typename,typename> class Sequence>
- inline void parse_checked(Sequence<T,Allocator>& sequence) const
- {
- parse_checked<T>(std::back_inserter(sequence));
- }
- template <typename T,
- typename Comparator,
- typename Allocator>
- inline void parse_checked(std::set<T,Comparator,Allocator>& set) const
- {
- parse_checked<T>(std::inserter(set,set.end()));
- }
- template <typename T,
- typename Comparator,
- typename Allocator>
- inline void parse_checked(std::multiset<T,Comparator,Allocator>& multiset) const
- {
- parse_checked<T>(std::inserter(multiset,multiset.end()));
- }
- template <typename T, typename Container>
- inline void parse_checked(std::queue<T,Container>& queue) const
- {
- parse_checked<T>(push_inserter(queue));
- }
- template <typename T, typename Container>
- inline void parse_checked(std::stack<T,Container>& stack) const
- {
- parse_checked<T>(push_inserter(stack));
- }
- template <typename T,
- typename Container,
- typename Comparator>
- inline void parse_checked(std::priority_queue<T,Container,Comparator>& priority_queue) const
- {
- parse_checked<T>(push_inserter(priority_queue));
- }
- template <typename Function>
- inline std::size_t for_each_column(const col_range_t& range, Function f) const
- {
- if (!validate_column_range(range))
- return false;
- token_list_t::const_iterator itr = begin_ + range.first;
- token_list_t::const_iterator end = begin_ + range.second;
- std::size_t col_count = 0;
- while (end != itr)
- {
- const range_t& range = (*itr);
- f(range);
- ++itr;
- ++col_count;
- }
- return col_count;
- }
- template <typename Function>
- inline std::size_t for_each_column(Function f) const
- {
- return for_each_column(all_columns(),f);
- }
- private:
- template <typename T>
- inline bool process(const range_t& range, T& t) const
- {
- return string_to_type_converter(range.first,range.second,t);
- }
- private:
- std::size_t index_;
- std::size_t size_;
- token_list_t::const_iterator begin_;
- };
- token_grid()
- : file_name_(""),
- buffer_(0),
- buffer_size_(0),
- min_column_count_(0),
- max_column_count_(0),
- load_from_file_(false),
- state_(false)
- {}
- token_grid(const std::string& file_name,
- const token_grid::options& options)
- : file_name_(file_name),
- buffer_(0),
- buffer_size_(0),
- min_column_count_(0),
- max_column_count_(0),
- options_(options),
- load_from_file_(true),
- state_(load())
- {}
- token_grid(const unsigned char* input_buffer,
- const std::size_t& input_buffer_size,
- const token_grid::options& options)
- : file_name_(""),
- buffer_(const_cast<unsigned char*>(input_buffer)),
- buffer_size_(input_buffer_size),
- min_column_count_(0),
- max_column_count_(0),
- options_(options),
- load_from_file_(false),
- state_(load())
- {}
- token_grid(const char* input_buffer,
- const std::size_t& input_buffer_size,
- const token_grid::options& options)
- : file_name_(""),
- buffer_(reinterpret_cast<unsigned char*>(const_cast<char*>(input_buffer))),
- buffer_size_(input_buffer_size),
- min_column_count_(0),
- max_column_count_(0),
- options_(options),
- load_from_file_(false),
- state_(load())
- {}
- token_grid(const std::string& input_buffer,
- const std::size_t& input_buffer_size,
- const token_grid::options& options)
- : file_name_(""),
- buffer_(reinterpret_cast<unsigned char*>(const_cast<char*>(input_buffer.data()))),
- buffer_size_(input_buffer_size),
- min_column_count_(0),
- max_column_count_(0),
- options_(options),
- load_from_file_(false),
- state_(load())
- {}
- token_grid(const std::string& file_name,
- const std::string& column_delimiters = ",|;\t",
- const std::string& row_delimiters = "\n\r")
- : file_name_(file_name),
- buffer_(0),
- buffer_size_(0),
- min_column_count_(0),
- max_column_count_(0),
- options_(split_options::compress_delimiters,
- split_options::compress_delimiters,
- row_delimiters,
- column_delimiters),
- load_from_file_(true),
- state_(load())
- {}
- token_grid(const unsigned char* input_buffer,
- const std::size_t& input_buffer_size,
- const std::string& column_delimiters = ",|;\t",
- const std::string& row_delimiters = "\n\r")
- : file_name_(""),
- buffer_(const_cast<unsigned char*>(input_buffer)),
- buffer_size_(input_buffer_size),
- min_column_count_(0),
- max_column_count_(0),
- options_(split_options::compress_delimiters,
- split_options::compress_delimiters,
- row_delimiters,
- column_delimiters),
- load_from_file_(false),
- state_(load())
- {}
- token_grid(const char* input_buffer,
- const std::size_t& input_buffer_size,
- const std::string& column_delimiters = ",|;\t",
- const std::string& row_delimiters = "\n\r")
- : file_name_(""),
- buffer_(reinterpret_cast<unsigned char*>(const_cast<char*>(input_buffer))),
- buffer_size_(input_buffer_size),
- min_column_count_(0),
- max_column_count_(0),
- options_(split_options::compress_delimiters,
- split_options::compress_delimiters,
- row_delimiters,
- column_delimiters),
- load_from_file_(false),
- state_(load())
- {}
- token_grid(const std::string& input_buffer,
- const std::size_t& input_buffer_size,
- const std::string& column_delimiters = ",;|\t ",
- const std::string& row_delimiters = "\n\r")
- : file_name_(""),
- buffer_(reinterpret_cast<unsigned char*>(const_cast<char*>(input_buffer.data()))),
- buffer_size_(input_buffer_size),
- min_column_count_(0),
- max_column_count_(0),
- options_(split_options::compress_delimiters,
- split_options::compress_delimiters,
- row_delimiters,
- column_delimiters),
- load_from_file_(false),
- state_(load())
- {}
- ~token_grid()
- {
- if ((load_from_file_) && (0 != buffer_))
- {
- delete [] buffer_;
- buffer_ = 0;
- }
- }
- inline bool operator!() const
- {
- return !state_;
- }
- inline std::string source_file() const
- {
- return file_name_;
- }
- inline std::size_t row_count() const
- {
- return dsv_index_.row_index.size();
- }
- inline std::size_t min_column_count() const
- {
- return min_column_count_;
- }
- inline std::size_t max_column_count() const
- {
- return max_column_count_;
- }
- inline range_t token(const unsigned int& row, const std::size_t& col) const
- {
- return dsv_index_(col,row);
- }
- template <typename T>
- inline T get(const unsigned int& row, const std::size_t& col)
- {
- range_t r = token(row,col);
- return string_to_type_converter<T>(r.first,r.second);
- }
- inline row_type row(const unsigned int& row_index) const
- {
- return row_type(row_index,dsv_index_);
- }
- inline row_range_t all_rows() const
- {
- return row_range_t(0,static_cast<index_t>(dsv_index_.row_index.size()));
- }
- template <typename OutputIterator>
- inline bool extract_column_checked(const row_range_t& row_range,
- const std::size_t& index,
- OutputIterator out) const
- {
- if (index > max_column_count_)
- return false;
- else if (row_range_invalid(row_range))
- return false;
- for (std::size_t i = row_range.first; i < row_range.second; ++i)
- {
- const row_index_range_t& row = dsv_index_.row_index[i];
- if (index < dsv_index_.token_count(row))
- {
- dsv_index_.token_list.begin() + (row.first + index);
- process_token_checked(*(dsv_index_.token_list.begin() + (row.first + index)),out);
- }
- }
- return true;
- }
- template <typename OutputIterator>
- inline bool extract_column_checked(const std::size_t& index,
- OutputIterator out) const
- {
- return extract_column_checked(all_rows(),index,out);
- }
- template <typename T,
- typename Allocator,
- template <typename,typename> class Sequence>
- inline void extract_column_checked(const std::size_t& index,
- Sequence<T,Allocator>& sequence) const
- {
- extract_column_checked(index,back_inserter_with_valuetype(sequence));
- }
- template <typename T,
- typename Comparator,
- typename Allocator>
- inline void extract_column_checked(const std::size_t& index,
- std::set<T,Comparator,Allocator>& set) const
- {
- extract_column_checked(index,inserter_with_valuetype(set));
- }
- template <typename T,
- typename Comparator,
- typename Allocator>
- inline void extract_column_checked(const std::size_t& index,
- std::multiset<T,Comparator,Allocator>& multiset) const
- {
- extract_column_checked(index,inserter_with_valuetype(multiset));
- }
- template <typename OutputIterator>
- inline bool extract_column(const row_range_t& row_range,
- const std::size_t& index,
- OutputIterator out) const
- {
- if (index > max_column_count_)
- return false;
- else if (row_range_invalid(row_range))
- return false;
- for (std::size_t i = row_range.first; i < row_range.second; ++i)
- {
- const row_index_range_t& row = dsv_index_.row_index[i];
- if (index < dsv_index_.token_count(row))
- {
- process_token(*(dsv_index_.token_list.begin() + (row.first + index)),out);
- }
- }
- return true;
- }
- template <typename OutputIterator>
- inline bool extract_column(const std::size_t& index,
- OutputIterator out) const
- {
- return extract_column(all_rows(),index,out);
- }
- template <typename OutputIterator0, typename OutputIterator1>
- inline bool extract_column(const row_range_t& row_range,
- const std::size_t& index0,
- const std::size_t& index1,
- OutputIterator0 out0,
- OutputIterator1 out1) const
- {
- if ((index0 > max_column_count_) ||
- (index1 > max_column_count_))
- return false;
- else if (row_range_invalid(row_range))
- return false;
- std::size_t max_index = std::max(index0,index1);
- for (std::size_t i = row_range.first; i < row_range.second; ++i)
- {
- const row_index_range_t& row = dsv_index_.row_index[i];
- if (max_index < dsv_index_.token_count(row))
- {
- process_token(*(dsv_index_.token_list.begin() + (row.first + index0)),out0);
- process_token(*(dsv_index_.token_list.begin() + (row.first + index1)),out1);
- }
- }
- return true;
- }
- template <typename OutputIterator0, typename OutputIterator1, typename OutputIterator2>
- inline bool extract_column(const row_range_t& row_range,
- const std::size_t& index0,
- const std::size_t& index1,
- const std::size_t& index2,
- OutputIterator0 out0,
- OutputIterator1 out1,
- OutputIterator2 out2) const
- {
- if ((index0 > max_column_count_) ||
- (index1 > max_column_count_) ||
- (index2 > max_column_count_))
- return false;
- else if (row_range_invalid(row_range))
- return false;
- std::size_t max_index = std::max(index0,std::max(index1,index2));
- for (std::size_t i = row_range.first; i < row_range.second; ++i)
- {
- const row_index_range_t& row = dsv_index_.row_index[i];
- if (max_index < dsv_index_.token_count(row))
- {
- process_token(*(dsv_index_.token_list.begin() + (row.first + index0)),out0);
- process_token(*(dsv_index_.token_list.begin() + (row.first + index1)),out1);
- process_token(*(dsv_index_.token_list.begin() + (row.first + index2)),out2);
- }
- }
- return true;
- }
- template <typename OutputIterator0, typename OutputIterator1,
- typename OutputIterator2, typename OutputIterator3>
- inline bool extract_column(const row_range_t& row_range,
- const std::size_t& index0,
- const std::size_t& index1,
- const std::size_t& index2,
- const std::size_t& index3,
- OutputIterator0 out0,
- OutputIterator1 out1,
- OutputIterator2 out2,
- OutputIterator3 out3) const
- {
- if ((index0 > max_column_count_) ||
- (index1 > max_column_count_) ||
- (index2 > max_column_count_) ||
- (index3 > max_column_count_))
- return false;
- else if (row_range_invalid(row_range))
- return false;
- std::size_t max_index = std::max(std::max(index0,index1),std::max(index2,index3));
- for (std::size_t i = row_range.first; i < row_range.second; ++i)
- {
- const row_index_range_t& row = dsv_index_.row_index[i];
- if (max_index < dsv_index_.token_count(row))
- {
- process_token(*(dsv_index_.token_list.begin() + (row.first + index0)),out0);
- process_token(*(dsv_index_.token_list.begin() + (row.first + index1)),out1);
- process_token(*(dsv_index_.token_list.begin() + (row.first + index2)),out2);
- process_token(*(dsv_index_.token_list.begin() + (row.first + index3)),out3);
- }
- }
- return true;
- }
- template <typename OutputIterator0, typename OutputIterator1,
- typename OutputIterator2, typename OutputIterator3,
- typename OutputIterator4>
- inline bool extract_column(const row_range_t& row_range,
- const std::size_t& index0,
- const std::size_t& index1,
- const std::size_t& index2,
- const std::size_t& index3,
- const std::size_t& index4,
- OutputIterator0 out0,
- OutputIterator1 out1,
- OutputIterator2 out2,
- OutputIterator3 out3,
- OutputIterator4 out4) const
- {
- if ((index0 > max_column_count_) ||
- (index1 > max_column_count_) ||
- (index2 > max_column_count_) ||
- (index3 > max_column_count_) ||
- (index4 > max_column_count_))
- return false;
- else if (row_range_invalid(row_range))
- return false;
- std::size_t max_index = std::max(index4,std::max(std::max(index0,index1),std::max(index2,index3)));
- for (std::size_t i = row_range.first; i < row_range.second; ++i)
- {
- const row_index_range_t& row = dsv_index_.row_index[i];
- if (max_index < dsv_index_.token_count(row))
- {
- process_token(*(dsv_index_.token_list.begin() + (row.first + index0)),out0);
- process_token(*(dsv_index_.token_list.begin() + (row.first + index1)),out1);
- process_token(*(dsv_index_.token_list.begin() + (row.first + index2)),out2);
- process_token(*(dsv_index_.token_list.begin() + (row.first + index3)),out3);
- process_token(*(dsv_index_.token_list.begin() + (row.first + index4)),out4);
- }
- }
- return true;
- }
- inline void remove_row(const std::size_t& index)
- {
- if (index < dsv_index_.row_index.size())
- {
- dsv_index_.remove_row(index);
- }
- }
- template <typename Predicate>
- inline bool remove_row_if(const row_range_t& row_range, Predicate predicate)
- {
- if (row_range_invalid(row_range))
- return false;
- std::size_t removed_token_count = 0;
- std::deque<std::size_t> remove_token_list;
- std::deque<std::size_t> remove_row_list;
- for (std::size_t i = row_range.first; i < row_range.second; ++i)
- {
- row_index_range_t& r = dsv_index_.row_index[i];
- std::size_t temp_r_first = r.first - removed_token_count;
- row_type row(i,dsv_index_);
- if (predicate(row))
- {
- remove_row_list.push_back(i);
- for (std::size_t j = r.first; j <= r.second; ++j)
- {
- remove_token_list.push_back(j);
- }
- removed_token_count += row.size();
- }
- r.first = static_cast<unsigned int>(temp_r_first);
- r.second -= static_cast<unsigned int>(removed_token_count);
- }
- for (std::size_t i = row_range.second; i < dsv_index_.row_index.size(); ++i)
- {
- row_index_range_t& r = dsv_index_.row_index[i];
- r.first -= static_cast<unsigned int>(removed_token_count);
- r.second -= static_cast<unsigned int>(removed_token_count);
- }
- if (!remove_row_list.empty())
- {
- remove_inplace(index_remover(remove_row_list),dsv_index_.row_index);
- }
- if (!remove_token_list.empty())
- {
- remove_inplace(index_remover(remove_token_list),dsv_index_.token_list);
- }
- return true;
- }
- template <typename Predicate>
- inline bool remove_row_if(Predicate predicate)
- {
- return remove_row_if(all_rows(),predicate);
- }
- template <typename Predicate>
- inline std::size_t remove_token_if(const row_range_t& row_range, Predicate predicate)
- {
- if (row_range_invalid(row_range))
- return 0;
- std::size_t removed_token_count = 0;
- std::deque<std::size_t> remove_token_list;
- std::deque<std::size_t> remove_row_list;
- for (std::size_t i = row_range.first; i < row_range.second; ++i)
- {
- row_index_range_t& r = dsv_index_.row_index[i];
- std::size_t temp_r_first = r.first - removed_token_count;
- row_type row(i,dsv_index_);
- for (std::size_t j = 0; j < row.size(); ++j)
- {
- if (predicate(row.token(j)))
- {
- remove_token_list.push_back(r.first + j);
- ++removed_token_count;
- }
- }
- r.first = static_cast<unsigned int>(temp_r_first);
- r.second -= static_cast<unsigned int>(removed_token_count);
- if (0 == dsv_index_.token_count(r))
- {
- remove_row_list.push_back(i);
- }
- }
- for (std::size_t i = row_range.second; i < dsv_index_.row_index.size(); ++i)
- {
- row_index_range_t& r = dsv_index_.row_index[i];
- r.first -= static_cast<unsigned int>(removed_token_count);
- r.second -= static_cast<unsigned int>(removed_token_count);
- }
- if (!remove_row_list.empty())
- {
- remove_inplace(index_remover(remove_row_list),dsv_index_.row_index);
- }
- if (!remove_token_list.empty())
- {
- remove_inplace(index_remover(remove_token_list),dsv_index_.token_list);
- }
- if (!remove_token_list.empty())
- {
- update_minmax_columns();
- }
- return remove_token_list.size();
- }
- inline std::size_t remove_empty_tokens(const row_range_t& range)
- {
- return remove_token_if(range,is_empty_token());
- }
- inline std::size_t remove_empty_tokens()
- {
- return remove_empty_tokens(all_rows());
- }
- inline void enforce_column_count(const row_range_t& row_range,
- const std::size_t& column_count)
- {
- if (row_range_invalid(row_range))
- return;
- remove_row_if(insufficient_number_of_columns(column_count));
- min_column_count_ = column_count;
- max_column_count_ = column_count;
- }
- inline void enforce_column_count(const std::size_t& column_count)
- {
- enforce_column_count(all_rows(),column_count);
- }
- inline void enforce_min_max_column_count(const row_range_t& row_range,
- const std::size_t& min_column_count,
- const std::size_t& max_column_count)
- {
- if (row_range_invalid(row_range))
- return;
- remove_row_if(insufficient_number_of_minmax_columns(min_column_count,max_column_count));
- min_column_count_ = min_column_count;
- max_column_count_ = max_column_count;
- }
- inline void enforce_min_max_column_count(const std::size_t& min_column_count,
- const std::size_t& max_column_count)
- {
- enforce_min_max_column_count(all_rows(),min_column_count,max_column_count);
- }
- inline void clear(const bool force_delete_buffer = false)
- {
- if (load_from_file_ || force_delete_buffer)
- delete[] buffer_;
- buffer_ = 0;
- buffer_size_ = 0;
- dsv_index_.clear();
- min_column_count_ = 0;
- max_column_count_ = 0;
- state_ = false;
- file_name_ = "";
- }
- template <typename T>
- inline std::size_t accumulate_row(const std::size_t& row, T& result) const
- {
- if (row >= dsv_index_.row_index.size())
- return 0;
- const row_index_range_t& r = dsv_index_.row_index[row];
- token_list_t::const_iterator itr = dsv_index_.token_list.begin() + r.first;
- token_list_t::const_iterator end = dsv_index_.token_list.begin() + r.second + 1;
- std::size_t process_count = 0;
- T current_value = T();
- while (end != itr)
- {
- if (string_to_type_converter((*itr).first,(*itr).second,current_value))
- {
- result += current_value;
- ++process_count;
- }
- else
- return 0;
- ++itr;
- }
- return process_count;
- }
- template <typename T>
- inline std::size_t accumulate_column(const std::size_t& col,
- const row_range_t& row_range,
- T& result) const
- {
- if (col > max_column_count_)
- return 0;
- else if (row_range_invalid(row_range))
- return 0;
- std::size_t process_count = 0;
- T current_value = T();
- for (std::size_t i = row_range.first; i < row_range.second; ++i)
- {
- const row_index_range_t& r = dsv_index_.row_index[i];
- if (col < dsv_index_.token_count(r))
- {
- const range_t& range = *(dsv_index_.token_list.begin() + r.first + col);
- if (string_to_type_converter(range.first,range.second,current_value))
- result += current_value;
- else
- return 0;
- }
- ++process_count;
- }
- return process_count;
- }
- template <typename T>
- inline std::size_t accumulate_column(const std::size_t& col, T& result) const
- {
- return accumulate_column(col,all_rows(),result);
- }
- template <typename T, typename Predicate>
- inline std::size_t accumulate_column(const std::size_t& col,
- const row_range_t& row_range,
- Predicate p,
- T& result) const
- {
- if (col > max_column_count_)
- return 0;
- else if (row_range_invalid(row_range))
- return 0;
- std::size_t process_count = 0;
- T current_value = T();
- for (std::size_t i = row_range.first; i < row_range.second; ++i)
- {
- const row_index_range_t& r = dsv_index_.row_index[i];
- if (col < dsv_index_.token_count(r))
- {
- row_type row = row_type(i,dsv_index_);
- if (p(row))
- {
- const range_t& range = row.token(col);
- if (string_to_type_converter(range.first,range.second,current_value))
- {
- result += current_value;
- ++process_count;
- }
- else
- return 0;
- }
- }
- }
- return process_count;
- }
- template <typename T, typename Predicate>
- inline std::size_t accumulate_column(const std::size_t& col,
- Predicate p,
- T& result) const
- {
- return accumulate_column(col,all_rows(),p,result);
- }
- inline bool join_row(const std::size_t& row,
- const std::string& delimiter,
- std::string& result)
- {
- if (row >= dsv_index_.row_index.size())
- return false;
- const row_index_range_t& r = dsv_index_.row_index[row];
- token_list_t::const_iterator itr = dsv_index_.token_list.begin() + r.first;
- token_list_t::const_iterator end = dsv_index_.token_list.begin() + r.second + (row < (dsv_index_.row_index.size() - 1) ? 1 : 0);
- result.reserve(delimiter.size() * dsv_index_.token_count(r) + std::distance(itr->first,end->second));
- bool appended = false;
- while (end != itr)
- {
- if (!delimiter.empty() && appended)
- result.append(delimiter);
- appended = false;
- if ((*itr).first != (*itr).second)
- {
- result.append((*itr).first,(*itr).second);
- appended = true;
- }
- ++itr;
- }
- return true;
- }
- template <typename Predicate>
- inline bool join_row(const std::size_t& row,
- Predicate predicate,
- const std::string& delimiter,
- std::string& result)
- {
- if (row >= dsv_index_.row_index.size())
- return false;
- const row_index_range_t& r = dsv_index_.row_index[row];
- token_list_t::const_iterator itr = (dsv_index_.token_list.begin() + r.first);
- token_list_t::const_iterator end = dsv_index_.token_list.begin() + r.second + (row < (dsv_index_.row_index.size() - 1) ? 1 : 0);
- result.reserve(delimiter.size() * dsv_index_.token_count(r) + std::distance(itr->first,end->second));
- bool appended = false;
- while (end != itr)
- {
- if (!delimiter.empty() && appended)
- result.append(delimiter);
- appended = false;
- if ((*itr).first != (*itr).second)
- {
- if (predicate(*itr))
- {
- result.append((*itr).first,(*itr).second);
- appended = true;
- }
- }
- ++itr;
- }
- return true;
- }
- template <typename Predicate>
- inline bool join_row(const std::size_t& row,
- Predicate predicate,
- const char* delimiter,
- std::string& result)
- {
- return join_row(row,predicate,std::string(delimiter),result);
- }
- inline bool join_column(const std::size_t& col,
- const row_range_t& row_range,
- const std::string& delimiter,
- std::string& result) const
- {
- if (col > max_column_count_)
- return false;
- else if (row_range_invalid(row_range))
- return false;
- bool appended = false;
- for (std::size_t i = row_range.first; i < row_range.second; ++i)
- {
- const row_index_range_t& r = dsv_index_.row_index[i];
- if (col < dsv_index_.token_count(r))
- {
- row_type row = row_type(i,dsv_index_);
- const range_t& range = row.token(col);
- if (!delimiter.empty() && appended)
- result.append(delimiter);
- appended = false;
- if (range.first != range.second)
- {
- result.append(range.first,range.second);
- appended = true;
- }
- }
- }
- return true;
- }
- inline bool join_column(const std::size_t& col,
- const std::string& delimiter,
- std::string& result) const
- {
- return join_column(col,all_rows(),delimiter,result);
- }
- template <typename Predicate>
- inline bool join_column(const std::size_t& col,
- const row_range_t& row_range,
- Predicate predicate,
- const std::string& delimiter,
- std::string& result) const
- {
- if (col > max_column_count_)
- return false;
- else if (row_range_invalid(row_range))
- return false;
- bool appended = false;
- const std::size_t pre_end_index = row_range.second - 1;
- for (std::size_t i = row_range.first; i < row_range.second; ++i)
- {
- const row_index_range_t& r = dsv_index_.row_index[i];
- if (col < dsv_index_.token_count(r))
- {
- row_type row = row_type(i,dsv_index_);
- const range_t& range = row.token(col);
- if (!delimiter.empty() && appended && (pre_end_index != i))
- result.append(delimiter);
- appended = false;
- if (range.first != range.second)
- {
- if (predicate(row))
- {
- result.append(range.first,range.second);
- appended = true;
- }
- }
- }
- }
- return true;
- }
- template <typename Predicate>
- inline bool join_column(const std::size_t& col,
- Predicate p,
- const std::string& delimiter,
- std::string& result) const
- {
- return join_column(col,all_rows(),p,delimiter,result);
- }
- template <typename TransitionPredicate, typename Function>
- inline bool sequential_partition(const row_range_t& row_range,
- TransitionPredicate p,
- Function f)
- {
- if (row_range_invalid(row_range))
- return false;
- row_range_t r(row_range.first,row_range.first);
- for (std::size_t i = row_range.first; i < row_range.second; ++i)
- {
- if (p(row_type(i,dsv_index_)))
- {
- if (r.first != r.second)
- {
- r.second = i;
- if (!f(*this,r))
- return false;
- }
- r.first = r.second;
- }
- else
- r.second = i;
- }
- if (r.first != row_range.second)
- {
- r.second = row_range.second;
- if (!f(*this,r))
- return false;
- }
- return true;
- }
- template <typename TransitionPredicate, typename Function>
- inline bool sequential_partition(TransitionPredicate p, Function f)
- {
- return sequential_partition(all_rows(),p,f);
- }
- static inline token_grid::options default_options()
- {
- return options();
- }
- template <typename Function>
- inline std::size_t for_each_row(const row_range_t& row_range, Function f) const
- {
- if (row_range_invalid(row_range))
- return 0;
- std::size_t row_count = 0;
- for (std::size_t i = row_range.first; i < row_range.second; ++i)
- {
- f(row_type(i,dsv_index_));
- ++row_count;
- }
- return row_count;
- }
- template <typename Function>
- inline std::size_t for_each_row(Function f) const
- {
- return for_each_row(all_rows(),f);
- }
- bool load(const std::string& file_name,
- const token_grid::options& options)
- {
- file_name_ = file_name;
- if ((load_from_file_) && (0 != buffer_))
- {
- delete [] buffer_;
- buffer_ = 0;
- }
- buffer_size_ = 0;
- min_column_count_ = 0;
- max_column_count_ = 0;
- options_ = options;
- load_from_file_ = true;
- state_ = load();
- if (state_)
- return true;
- else
- {
- file_name_ = "";
- if ((load_from_file_) && (0 != buffer_))
- {
- delete [] buffer_;
- buffer_ = 0;
- }
- return false;
- }
- }
- bool load(unsigned char* buffer,
- const std::size_t buffer_size,
- const token_grid::options& options)
- {
- file_name_ = "";
- if ((load_from_file_) && (0 != buffer_))
- {
- delete [] buffer_;
- buffer_ = 0;
- }
- min_column_count_ = 0;
- max_column_count_ = 0;
- options_ = options;
- load_from_file_ = false;
- buffer_ = buffer;
- buffer_size_ = buffer_size;
- state_ = load();
- if (state_)
- return true;
- else
- {
- file_name_ = "";
- if ((load_from_file_) && (0 != buffer_))
- {
- delete [] buffer_;
- buffer_ = 0;
- }
- return false;
- }
- }
- private:
- token_grid(const token_grid& tg);
- token_grid& operator=(const token_grid& tg);
- struct is_empty_token
- {
- inline bool operator()(const range_t& r) const
- {
- return r.first == r.second;
- }
- };
- struct insufficient_number_of_columns
- {
- insufficient_number_of_columns(const std::size_t& noc)
- : num_of_cols(noc)
- {}
- inline bool operator()(const row_type& row) const
- {
- return (num_of_cols != row.size());
- }
- std::size_t num_of_cols;
- };
- struct insufficient_number_of_minmax_columns
- {
- insufficient_number_of_minmax_columns(const std::size_t& min_col, const std::size_t& max_col)
- : min_column_count(min_col),
- max_column_count(max_col)
- {}
- inline bool operator()(const row_type& row) const
- {
- return (row.size() < min_column_count) || (max_column_count < row.size());
- }
- std::size_t min_column_count;
- std::size_t max_column_count;
- };
- class double_quotes_predicate
- {
- public:
- double_quotes_predicate(const std::string& delimiters)
- : in_bracket_range_(false),
- mdp_(delimiters)
- {}
- inline bool operator()(const unsigned char c) const
- {
- if ('"' == c)
- {
- in_bracket_range_ = !in_bracket_range_;
- return true;
- }
- else if (in_bracket_range_)
- return false;
- else
- return mdp_(c);
- }
- inline void reset()
- {
- in_bracket_range_ = false;
- }
- private:
- mutable bool in_bracket_range_;
- mutable strtk::multiple_char_delimiter_predicate mdp_;
- };
- inline bool load()
- {
- if (load_from_file_ && !load_buffer_from_file())
- return false;
- dsv_index_.token_list.clear();
- dsv_index_.row_index.clear();
- multiple_char_delimiter_predicate text_newline_predicate(options_.row_delimiters);
- if (!options_.support_dquotes)
- {
- multiple_char_delimiter_predicate token_predicate(options_.column_delimiters);
- strtk::split(text_newline_predicate,
- buffer_, buffer_ + buffer_size_,
- strtk::functional_inserter(
- row_processor<multiple_char_delimiter_predicate>(dsv_index_,token_predicate,options_.column_split_option)),
- strtk::split_options::compress_delimiters);
- }
- else
- {
- double_quotes_predicate token_predicate_dblq(options_.column_delimiters);
- strtk::split(text_newline_predicate,
- buffer_, buffer_ + buffer_size_,
- strtk::functional_inserter(
- row_processor<double_quotes_predicate>(dsv_index_,token_predicate_dblq,options_.column_split_option)),
- strtk::split_options::compress_delimiters);
- }
- update_minmax_columns();
- return true;
- }
- inline bool load_buffer_from_file()
- {
- std::ifstream stream(file_name_.c_str(),std::ios::binary);
- if (!stream)
- return false;
- stream.seekg (0,std::ios::end);
- buffer_size_ = static_cast<std::size_t>(stream.tellg());
- if (0 == buffer_size_)
- return false;
- stream.seekg (0,std::ios::beg);
- buffer_ = new unsigned char[buffer_size_];
- stream.read(reinterpret_cast<char*>(buffer_),static_cast<std::streamsize>(buffer_size_));
- stream.close();
- return true;
- }
- template <typename OutputIterator>
- inline void process_token(const range_t& range, OutputIterator out) const
- {
- typedef typename std::iterator_traits<OutputIterator>::value_type output_type;
- (*out) = string_to_type_converter<output_type>(range.first,range.second);
- ++out;
- }
- template <typename OutputIterator>
- inline void process_token_checked(const range_t& range, OutputIterator out) const
- {
- typedef typename std::iterator_traits<OutputIterator>::value_type output_type;
- output_type value;
- if (string_to_type_converter(range.first,range.second,value))
- {
- (*out) = value;
- ++out;
- }
- }
- inline bool row_range_invalid(const row_range_t& row_range) const
- {
- if (row_range.first > dsv_index_.row_index.size())
- return true;
- else if (row_range.second > dsv_index_.row_index.size())
- return true;
- else if (row_range.first > row_range.second)
- return true;
- else
- return false;
- }
- inline void update_minmax_columns()
- {
- min_column_count_ = std::numeric_limits<std::size_t>::max();
- max_column_count_ = std::numeric_limits<std::size_t>::min();
- for (std::size_t i = 0; i < dsv_index_.row_index.size(); ++i)
- {
- const row_index_range_t& r = dsv_index_.row_index[i];
- const std::size_t number_of_tokens = dsv_index_.token_count(r);
- if (number_of_tokens > max_column_count_)
- max_column_count_ = number_of_tokens;
- if (number_of_tokens < min_column_count_)
- min_column_count_ = number_of_tokens;
- }
- }
- private:
- store dsv_index_;
- std::string file_name_;
- unsigned char* buffer_;
- std::size_t buffer_size_;
- std::size_t min_column_count_;
- std::size_t max_column_count_;
- options options_;
- bool load_from_file_;
- bool state_;
- };
- template <typename T>
- inline bool convert_string_range(const std::pair<std::string::const_iterator,std::string::const_iterator>& range, T& t)
- {
- if (range.first == range.second) return false;
- t = string_to_type_converter<T>(std::string(range.first,range.second));
- return true;
- }
- struct empty_range
- {
- public:
- template <typename InputIterator>
- inline bool operator()(const InputIterator begin, const InputIterator end)
- {
- return (0 == std::distance(begin,end));
- }
- };
- struct nonempty_range
- {
- public:
- template <typename InputIterator>
- inline bool operator()(const InputIterator begin, const InputIterator end)
- {
- return (0 != std::distance(begin,end));
- }
- };
- template <typename OutputIterator>
- struct filter_non_empty_range
- {
- public:
- filter_non_empty_range(OutputIterator out)
- : out_(out)
- {}
- template <typename Iterator>
- inline void operator() (const std::pair<Iterator,Iterator>& range)
- {
- if (range.first != range.second)
- {
- *out_++ = range;
- }
- }
- private:
- OutputIterator out_;
- };
- template <typename OutputPredicate>
- struct filter_on_wildcard_match
- {
- public:
- filter_on_wildcard_match(const std::string& match_pattern,
- OutputPredicate& predicate,
- bool allow_through_on_match = true)
- : allow_through_on_match_(allow_through_on_match),
- match_pattern_(match_pattern),
- predicate_(predicate)
- {}
- template <typename Iterator>
- inline void operator() (const std::pair<Iterator,Iterator>& range) const
- {
- if (match(match_pattern_.begin(),match_pattern_.end(),range.first,range.second,'*','?') ^ allow_through_on_match_)
- {
- predicate_(range);
- }
- }
- inline void operator() (const std::string& s) const
- {
- if (match(match_pattern_,s) ^ allow_through_on_match_)
- {
- predicate_(s);
- }
- }
- private:
- filter_on_wildcard_match(const filter_on_wildcard_match& fom);
- filter_on_wildcard_match& operator=(const filter_on_wildcard_match& fom);
- bool allow_through_on_match_;
- std::string match_pattern_;
- OutputPredicate& predicate_;
- };
- template <typename OutputPredicate>
- struct filter_on_match
- {
- public:
- filter_on_match(const std::string* begin, const std::string* end,
- OutputPredicate predicate,
- bool case_insensitive,
- bool allow_through_on_match = true)
- : case_insensitive_(case_insensitive),
- allow_through_on_match_(allow_through_on_match),
- begin_(begin),
- end_(end),
- predicate_(predicate)
- {}
- template <typename Iterator>
- inline void operator() (const std::pair<Iterator,Iterator>& range) const
- {
- for (const std::string* itr = begin_; end_ != itr; ++itr)
- {
- if ((case_insensitive_ &&
- (imatch((*itr).data(),(*itr).data() + (*itr).size(),range.first,range.second))) ||
- (!case_insensitive_ && std::equal((*itr).begin(),(*itr).end(),range.first)))
- {
- if (allow_through_on_match_)
- {
- predicate_(range);
- }
- return;
- }
- }
- if (!allow_through_on_match_)
- {
- predicate_(range);
- return;
- }
- }
- inline void operator() (const std::string& s) const
- {
- for (const std::string* itr = begin_; end_ != itr; ++itr)
- {
- if ((case_insensitive_ &&
- (imatch((*itr).begin(),(*itr).end(),s.begin(),s.end()))) ||
- (!case_insensitive_ && std::equal((*itr).begin(),(*itr).end(),s.begin())))
- {
- if (allow_through_on_match_)
- {
- predicate_(s);
- return;
- }
- }
- }
- if (!allow_through_on_match_)
- {
- predicate_(s);
- return;
- }
- }
- private:
- filter_on_match& operator=(const filter_on_match& fom);
- private:
- bool case_insensitive_;
- bool allow_through_on_match_;
- const std::string* begin_;
- const std::string* end_;
- OutputPredicate predicate_;
- };
- template <typename Iterator, typename MatchPredicate>
- inline void skip_while_matching(Iterator& itr,
- const Iterator& end,
- const MatchPredicate& predicate)
- {
- while (end != itr)
- {
- if (predicate(*itr))
- ++itr;
- else
- break;
- }
- }
- template <std::size_t length>
- struct size_equal_to
- {
- template <typename Iterator>
- inline bool operator()(const Iterator begin, const Iterator end) const
- {
- return length == std::distance(begin,end);
- }
- template <typename Iterator>
- inline bool operator()(const std::pair<Iterator,Iterator>& range) const
- {
- return length == std::distance(range.first,range.second);
- }
- template <typename T,
- typename Allocator,
- template <typename,typename> class Sequence>
- inline bool operator()(const Sequence<T,Allocator>& sequence) const
- {
- return length == sequence.size();
- }
- template <typename T,
- typename Comparator,
- typename Allocator>
- inline bool operator()(const std::set<T,Comparator,Allocator>& set) const
- {
- return length == set.size();
- }
- template <typename T,
- typename Comparator,
- typename Allocator>
- inline bool operator()(const std::multiset<T,Comparator,Allocator>& multiset) const
- {
- return length == multiset.size();
- }
- inline bool operator()(const std::string& str) const
- {
- return length == str.size();
- }
- };
- template <std::size_t length>
- struct size_less_than
- {
- template <typename Iterator>
- inline bool operator()(const Iterator begin, const Iterator end) const
- {
- return std::distance(begin,end) < static_cast<typename std::iterator_traits<Iterator>::difference_type>(length);
- }
- template <typename Iterator>
- inline bool operator()(const std::pair<Iterator,Iterator>& range) const
- {
- return std::distance(range.first,range.second) < static_cast<typename std::iterator_traits<Iterator>::difference_type>(length);
- }
- template <typename T,
- typename Allocator,
- template <typename,typename> class Sequence>
- inline bool operator()(const Sequence<T,Allocator>& sequence) const
- {
- return sequence.size() < length;
- }
- template <typename T,
- typename Comparator,
- typename Allocator>
- inline bool operator()(const std::set<T,Comparator,Allocator>& set) const
- {
- return set.size() < length;
- }
- template <typename T,
- typename Comparator,
- typename Allocator>
- inline bool operator()(const std::multiset<T,Comparator,Allocator>& multiset) const
- {
- return multiset.size() < length;
- }
- inline bool operator()(const std::string& str) const
- {
- return str.size() < length;
- }
- };
- template <std::size_t length>
- struct size_greater_than
- {
- template <typename Iterator>
- inline bool operator()(const Iterator begin, const Iterator end) const
- {
- return std::distance(begin,end) > static_cast<typename std::iterator_traits<Iterator>::difference_type>(length);
- }
- template <typename Iterator>
- inline bool operator()(const std::pair<Iterator,Iterator>& range) const
- {
- return std::distance(range.first,range.second) > static_cast<typename std::iterator_traits<Iterator>::difference_type>(length);
- }
- template <typename T,
- typename Allocator,
- template <typename,typename> class Sequence>
- inline bool operator()(const Sequence<T,Allocator>& sequence) const
- {
- return sequence.size() > length;
- }
- template <typename T,
- typename Comparator,
- typename Allocator>
- inline bool operator()(const std::set<T,Comparator,Allocator>& set) const
- {
- return set.size() > length;
- }
- template <typename T,
- typename Comparator,
- typename Allocator>
- inline bool operator()(const std::multiset<T,Comparator,Allocator>& multiset) const
- {
- return multiset.size() > length;
- }
- inline bool operator()(const std::string& str) const
- {
- return str.size() > length;
- }
- };
- struct size_is_even
- {
- template <typename Iterator>
- inline bool operator()(const Iterator begin, const Iterator end) const
- {
- return 0 == (std::distance(begin,end) % 2);
- }
- template <typename Iterator>
- inline bool operator()(const std::pair<Iterator,Iterator>& range) const
- {
- return 0 == (std::distance(range.first,range.second) % 2);
- }
- template <typename T,
- typename Allocator,
- template <typename,typename> class Sequence>
- inline bool operator()(const Sequence<T,Allocator>& sequence) const
- {
- return 0 == (sequence.size() % 2);
- }
- template <typename T,
- typename Comparator,
- typename Allocator>
- inline bool operator()(const std::set<T,Comparator,Allocator>& set) const
- {
- return 0 == (set.size() % 2);
- }
- template <typename T,
- typename Comparator,
- typename Allocator>
- inline bool operator()(const std::multiset<T,Comparator,Allocator>& multiset) const
- {
- return 0 == (multiset.size() % 2);
- }
- inline bool operator()(const std::string& str) const
- {
- return 0 == (str.size() % 2);
- }
- };
- struct size_is_odd
- {
- template <typename Iterator>
- inline bool operator()(const Iterator begin, const Iterator end) const
- {
- return 0 != (std::distance(begin,end) % 2);
- }
- template <typename Iterator>
- inline bool operator()(const std::pair<Iterator,Iterator>& range) const
- {
- return 0 != (std::distance(range.first,range.second) % 2);
- }
- template <typename T,
- typename Allocator,
- template <typename,typename> class Sequence>
- inline bool operator()(const Sequence<T,Allocator>& sequence) const
- {
- return 0 != (sequence.size() % 2);
- }
- template <typename T,
- typename Comparator,
- typename Allocator>
- inline bool operator()(const std::set<T,Comparator,Allocator>& set) const
- {
- return 0 != (set.size() % 2);
- }
- template <typename T,
- typename Comparator,
- typename Allocator>
- inline bool operator()(const std::multiset<T,Comparator,Allocator>& multiset) const
- {
- return 0 != (multiset.size() % 2);
- }
- inline bool operator()(const std::string& str) const
- {
- return 0 != (str.size() % 2);
- }
- };
- template <typename InputIterator,
- typename T1, typename T2, typename T3, typename T4,
- typename T5, typename T6, typename T7, typename T8,
- typename T9, typename T10, typename T11, typename T12>
- inline bool parse(const InputIterator begin,
- const InputIterator end,
- const std::string& delimiters,
- T1& t1, T2& t2, T3& t3, T4& t4,
- T5& t5, T6& t6, T7& t7, T8& t8,
- T9& t9, T10& t10, T11& t11, T12& t12)
- {
- typedef typename details::is_valid_iterator<InputIterator>::type itr_type;
- static const std::size_t token_count = 12;
- typedef std::pair<InputIterator,InputIterator> iterator_type;
- typedef iterator_type* iterator_type_ptr;
- iterator_type token_list[token_count];
- const std::size_t parsed_token_count = split_n(delimiters,
- begin,end,
- token_count,
- token_list,
- split_options::compress_delimiters);
- if (token_count > parsed_token_count)
- return false;
- iterator_type_ptr itr = token_list;
- if (!string_to_type_converter((*itr).first,(*itr).second, t1)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second, t2)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second, t3)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second, t4)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second, t5)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second, t6)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second, t7)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second, t8)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second, t9)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t10)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t11)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t12)) return false;
- return true;
- }
- template <typename InputIterator,
- typename T1, typename T2, typename T3, typename T4,
- typename T5, typename T6, typename T7, typename T8,
- typename T9, typename T10, typename T11>
- inline bool parse(const InputIterator begin,
- const InputIterator end,
- const std::string& delimiters,
- T1& t1, T2& t2, T3& t3, T4& t4,
- T5& t5, T6& t6, T7& t7, T8& t8,
- T9& t9, T10& t10, T11& t11)
- {
- typedef typename details::is_valid_iterator<InputIterator>::type itr_type;
- static const std::size_t token_count = 11;
- typedef std::pair<InputIterator,InputIterator> iterator_type;
- typedef iterator_type* iterator_type_ptr;
- iterator_type token_list[token_count];
- const std::size_t parsed_token_count = split_n(delimiters,
- begin,end,
- token_count,
- token_list,
- split_options::compress_delimiters);
- if (token_count > parsed_token_count)
- return false;
- iterator_type_ptr itr = token_list;
- if (!string_to_type_converter((*itr).first,(*itr).second, t1)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second, t2)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second, t3)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second, t4)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second, t5)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second, t6)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second, t7)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second, t8)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second, t9)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t10)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t11)) return false;
- return true;
- }
- template <typename InputIterator,
- typename T1, typename T2, typename T3, typename T4,
- typename T5, typename T6, typename T7, typename T8,
- typename T9, typename T10>
- inline bool parse(const InputIterator begin,
- const InputIterator end,
- const std::string& delimiters,
- T1& t1, T2& t2, T3& t3, T4& t4,
- T5& t5, T6& t6, T7& t7, T8& t8,
- T9& t9, T10& t10)
- {
- typedef typename details::is_valid_iterator<InputIterator>::type itr_type;
- static const std::size_t token_count = 10;
- typedef std::pair<InputIterator,InputIterator> iterator_type;
- typedef iterator_type* iterator_type_ptr;
- iterator_type token_list[token_count];
- const std::size_t parsed_token_count = split_n(delimiters,
- begin,end,
- token_count,
- token_list,
- split_options::compress_delimiters);
- if (token_count > parsed_token_count)
- return false;
- iterator_type_ptr itr = token_list;
- if (!string_to_type_converter((*itr).first,(*itr).second, t1)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second, t2)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second, t3)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second, t4)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second, t5)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second, t6)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second, t7)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second, t8)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second, t9)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t10)) return false;
- return true;
- }
- template <typename InputIterator,
- typename T1, typename T2, typename T3, typename T4,
- typename T5, typename T6, typename T7, typename T8,
- typename T9>
- inline bool parse(const InputIterator begin,
- const InputIterator end,
- const std::string& delimiters,
- T1& t1, T2& t2, T3& t3, T4& t4,
- T5& t5, T6& t6, T7& t7, T8& t8,
- T9& t9)
- {
- typedef typename details::is_valid_iterator<InputIterator>::type itr_type;
- static const std::size_t token_count = 9;
- typedef std::pair<InputIterator,InputIterator> iterator_type;
- typedef iterator_type* iterator_type_ptr;
- iterator_type token_list[token_count];
- const std::size_t parsed_token_count = split_n(delimiters,
- begin,end,
- token_count,
- token_list,
- split_options::compress_delimiters);
- if (token_count > parsed_token_count)
- return false;
- iterator_type_ptr itr = token_list;
- if (!string_to_type_converter((*itr).first,(*itr).second,t1)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t2)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t3)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t4)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t5)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t6)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t7)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t8)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t9)) return false;
- return true;
- }
- template <typename InputIterator,
- typename T1, typename T2, typename T3, typename T4,
- typename T5, typename T6, typename T7, typename T8>
- inline bool parse(const InputIterator begin,
- const InputIterator end,
- const std::string& delimiters,
- T1& t1, T2& t2, T3& t3, T4& t4,
- T5& t5, T6& t6, T7& t7, T8& t8)
- {
- typedef typename details::is_valid_iterator<InputIterator>::type itr_type;
- static const std::size_t token_count = 8;
- typedef std::pair<InputIterator,InputIterator> iterator_type;
- typedef iterator_type* iterator_type_ptr;
- iterator_type token_list[token_count];
- const std::size_t parsed_token_count = split_n(delimiters,
- begin,end,
- token_count,
- token_list,
- split_options::compress_delimiters);
- if (token_count > parsed_token_count)
- return false;
- iterator_type_ptr itr = token_list;
- if (!string_to_type_converter((*itr).first,(*itr).second,t1)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t2)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t3)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t4)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t5)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t6)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t7)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t8)) return false;
- return true;
- }
- template <typename InputIterator,
- typename T1, typename T2, typename T3, typename T4,
- typename T5, typename T6, typename T7>
- inline bool parse(const InputIterator begin,
- const InputIterator end,
- const std::string& delimiters,
- T1& t1, T2& t2, T3& t3, T4& t4,
- T5& t5, T6& t6, T7& t7)
- {
- typedef typename details::is_valid_iterator<InputIterator>::type itr_type;
- static const std::size_t token_count = 7;
- typedef std::pair<InputIterator,InputIterator> iterator_type;
- typedef iterator_type* iterator_type_ptr;
- iterator_type token_list[token_count];
- const std::size_t parsed_token_count = split_n(delimiters,
- begin,end,
- token_count,
- token_list,
- split_options::compress_delimiters);
- if (token_count > parsed_token_count)
- return false;
- iterator_type_ptr itr = token_list;
- if (!string_to_type_converter((*itr).first,(*itr).second,t1)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t2)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t3)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t4)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t5)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t6)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t7)) return false;
- return true;
- }
- template <typename InputIterator,
- typename T1, typename T2, typename T3, typename T4,
- typename T5, typename T6>
- inline bool parse(const InputIterator begin,
- const InputIterator end,
- const std::string& delimiters,
- T1& t1, T2& t2, T3& t3, T4& t4,
- T5& t5, T6& t6)
- {
- typedef typename details::is_valid_iterator<InputIterator>::type itr_type;
- static const std::size_t token_count = 6;
- typedef std::pair<InputIterator,InputIterator> iterator_type;
- typedef iterator_type* iterator_type_ptr;
- iterator_type token_list[token_count];
- const std::size_t parsed_token_count = split_n(delimiters,
- begin,end,
- token_count,
- token_list,
- split_options::compress_delimiters);
- if (token_count > parsed_token_count)
- return false;
- iterator_type_ptr itr = token_list;
- if (!string_to_type_converter((*itr).first,(*itr).second,t1)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t2)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t3)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t4)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t5)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t6)) return false;
- return true;
- }
- template <typename InputIterator,
- typename T1, typename T2, typename T3, typename T4,
- typename T5>
- inline bool parse(const InputIterator begin,
- const InputIterator end,
- const std::string& delimiters,
- T1& t1, T2& t2, T3& t3, T4& t4,
- T5& t5)
- {
- typedef typename details::is_valid_iterator<InputIterator>::type itr_type;
- static const std::size_t token_count = 5;
- typedef std::pair<InputIterator,InputIterator> iterator_type;
- typedef iterator_type* iterator_type_ptr;
- iterator_type token_list[token_count];
- const std::size_t parsed_token_count = split_n(delimiters,
- begin,end,
- token_count,
- token_list,
- split_options::compress_delimiters);
- if (token_count > parsed_token_count)
- return false;
- iterator_type_ptr itr = token_list;
- if (!string_to_type_converter((*itr).first,(*itr).second,t1)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t2)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t3)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t4)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t5)) return false;
- return true;
- }
- template <typename InputIterator,
- typename T1, typename T2, typename T3, typename T4>
- inline bool parse(const InputIterator begin,
- const InputIterator end,
- const std::string& delimiters,
- T1& t1, T2& t2, T3& t3, T4& t4)
- {
- typedef typename details::is_valid_iterator<InputIterator>::type itr_type;
- static const std::size_t token_count = 4;
- typedef std::pair<InputIterator,InputIterator> iterator_type;
- typedef iterator_type* iterator_type_ptr;
- iterator_type token_list[token_count];
- const std::size_t parsed_token_count = split_n(delimiters,
- begin,end,
- token_count,
- token_list,
- split_options::compress_delimiters);
- if (token_count > parsed_token_count)
- return false;
- iterator_type_ptr itr = token_list;
- if (!string_to_type_converter((*itr).first,(*itr).second,t1)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t2)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t3)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t4)) return false;
- return true;
- }
- template <typename InputIterator,
- typename T1, typename T2, typename T3>
- inline bool parse(const InputIterator begin,
- const InputIterator end,
- const std::string& delimiters,
- T1& t1, T2& t2, T3& t3)
- {
- typedef typename details::is_valid_iterator<InputIterator>::type itr_type;
- static const std::size_t token_count = 3;
- typedef std::pair<InputIterator,InputIterator> iterator_type;
- typedef iterator_type* iterator_type_ptr;
- iterator_type token_list[token_count];
- const std::size_t parsed_token_count = split_n(delimiters,
- begin,end,
- token_count,
- token_list,
- split_options::compress_delimiters);
- if (token_count > parsed_token_count)
- return false;
- iterator_type_ptr itr = token_list;
- if (!string_to_type_converter((*itr).first,(*itr).second,t1)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t2)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t3)) return false;
- return true;
- }
- template <typename InputIterator, typename T1, typename T2>
- inline bool parse(const InputIterator begin,
- const InputIterator end,
- const std::string& delimiters,
- T1& t1, T2& t2)
- {
- typedef typename details::is_valid_iterator<InputIterator>::type itr_type;
- static const std::size_t token_count = 2;
- typedef std::pair<InputIterator,InputIterator> iterator_type;
- typedef iterator_type* iterator_type_ptr;
- iterator_type token_list[token_count];
- const std::size_t parsed_token_count = split_n(delimiters,
- begin,end,
- token_count,
- token_list,
- split_options::compress_delimiters);
- if (token_count > parsed_token_count)
- return false;
- iterator_type_ptr itr = token_list;
- if (!string_to_type_converter((*itr).first,(*itr).second,t1)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t2)) return false;
- return true;
- }
- template <typename InputIterator, typename T>
- inline bool parse(const InputIterator begin,
- const InputIterator end,
- const std::string& delimiters,
- T& t)
- {
- typedef typename details::is_valid_iterator<InputIterator>::type itr_type;
- static const std::size_t token_count = 1;
- typedef std::pair<InputIterator,InputIterator> iterator_type;
- typedef iterator_type* iterator_type_ptr;
- iterator_type token_list[token_count];
- const std::size_t parsed_token_count = split_n(delimiters,
- begin,end,
- token_count,
- token_list,
- split_options::compress_delimiters);
- if (token_count > parsed_token_count)
- return false;
- iterator_type_ptr itr = token_list;
- return string_to_type_converter((*itr).first,(*itr).second,t);
- }
- template <typename InputIterator,
- typename T,
- typename Allocator,
- template <typename,typename> class Sequence>
- inline std::size_t parse(const InputIterator begin,
- const InputIterator end,
- const std::string& delimiters,
- Sequence<T,Allocator>& sequence,
- const split_options::type& split_option = split_options::compress_delimiters)
- {
- typedef typename details::is_valid_iterator<InputIterator>::type itr_type;
- if (1 == delimiters.size())
- return split(single_delimiter_predicate<std::string::value_type>(delimiters[0]),
- begin,end,
- range_to_type_back_inserter(sequence),
- split_option);
- else
- return split(multiple_char_delimiter_predicate(delimiters),
- begin, end,
- range_to_type_back_inserter(sequence),
- split_option);
- }
- template <typename InputIterator,
- typename T,
- typename Comparator,
- typename Allocator>
- inline std::size_t parse(const InputIterator begin,
- const InputIterator end,
- const std::string& delimiters,
- std::set<T,Comparator,Allocator>& set,
- const split_options::type& split_option = split_options::compress_delimiters)
- {
- typedef typename details::is_valid_iterator<InputIterator>::type itr_type;
- if (1 == delimiters.size())
- return split(single_delimiter_predicate<std::string::value_type>(delimiters[0]),
- begin,end,
- range_to_type_inserter(set),
- split_option);
- else
- return split(multiple_char_delimiter_predicate(delimiters),
- begin,end,
- range_to_type_inserter(set),
- split_option);
- }
- template <typename InputIterator,
- typename T,
- typename Comparator,
- typename Allocator>
- inline std::size_t parse(const InputIterator begin,
- const InputIterator end,
- const std::string& delimiters,
- std::multiset<T,Comparator,Allocator>& multiset,
- const split_options::type& split_option = split_options::compress_delimiters)
- {
- typedef typename details::is_valid_iterator<InputIterator>::type itr_type;
- if (1 == delimiters.size())
- return split(single_delimiter_predicate<std::string::value_type>(delimiters[0]),
- begin,end,
- range_to_type_inserter(multiset),
- split_option);
- else
- return split(multiple_char_delimiter_predicate(delimiters),
- begin,end,
- range_to_type_inserter(multiset),
- split_option);
- }
- template <typename InputIterator,
- typename T,
- typename Container>
- inline std::size_t parse(const InputIterator begin,
- const InputIterator end,
- const std::string& delimiters,
- std::queue<T,Container>& queue,
- const split_options::type& split_option = split_options::compress_delimiters)
- {
- typedef typename details::is_valid_iterator<InputIterator>::type itr_type;
- if (1 == delimiters.size())
- return split(single_delimiter_predicate<std::string::value_type>(delimiters[0]),
- begin,end,
- range_to_type_push_inserter(queue),
- split_option);
- else
- return split(multiple_char_delimiter_predicate(delimiters),
- begin,end,
- range_to_type_push_inserter(queue),
- split_option);
- }
- template <typename InputIterator,
- typename T,
- typename Container>
- inline std::size_t parse(const InputIterator begin,
- const InputIterator end,
- const std::string& delimiters,
- std::stack<T,Container>& stack,
- const split_options::type& split_option = split_options::compress_delimiters)
- {
- typedef typename details::is_valid_iterator<InputIterator>::type itr_type;
- if (1 == delimiters.size())
- return split(single_delimiter_predicate<std::string::value_type>(delimiters[0]),
- begin,end,
- range_to_type_push_inserter(stack),
- split_option);
- else
- return split(multiple_char_delimiter_predicate(delimiters),
- begin, end,
- range_to_type_push_inserter(stack),
- split_option);
- }
- template <typename InputIterator,
- typename T,
- typename Container,
- typename Comparator>
- inline std::size_t parse(const InputIterator begin,
- const InputIterator end,
- const std::string& delimiters,
- std::priority_queue<T,Container,Comparator>& priority_queue,
- const split_options::type& split_option = split_options::compress_delimiters)
- {
- typedef typename details::is_valid_iterator<InputIterator>::type itr_type;
- if (1 == delimiters.size())
- return split(single_delimiter_predicate<std::string::value_type>(delimiters[0]),
- begin,end,
- range_to_type_push_inserter(priority_queue),
- split_option);
- else
- return split(multiple_char_delimiter_predicate(delimiters),
- begin,end,
- range_to_type_push_inserter(priority_queue),
- split_option);
- }
- template <typename InputIterator,
- typename T,
- typename Allocator,
- template <typename,typename> class Sequence>
- inline std::size_t parse(const std::pair<InputIterator,InputIterator>& range,
- const std::string& delimiters,
- Sequence<T,Allocator>& sequence,
- const split_options::type& split_option = split_options::compress_delimiters)
- {
- typedef typename details::is_valid_iterator<InputIterator>::type itr_type;
- if (1 == delimiters.size())
- return split(single_delimiter_predicate<std::string::value_type>(delimiters[0]),
- range.first,range.second,
- range_to_type_back_inserter(sequence),
- split_option);
- else
- return split(multiple_char_delimiter_predicate(delimiters),
- range.first,range.second,
- range_to_type_back_inserter(sequence),
- split_option);
- }
- template <typename InputIterator,
- typename T,
- typename Comparator,
- typename Allocator>
- inline std::size_t parse(const std::pair<InputIterator,InputIterator>& range,
- const std::string& delimiters,
- std::set<T,Comparator,Allocator>& set,
- const split_options::type& split_option = split_options::compress_delimiters)
- {
- typedef typename details::is_valid_iterator<InputIterator>::type itr_type;
- if (1 == delimiters.size())
- return split(single_delimiter_predicate<std::string::value_type>(delimiters[0]),
- range.first,range.second,
- range_to_type_inserter(set),
- split_option);
- else
- return split(multiple_char_delimiter_predicate(delimiters),
- range.first,range.second,
- range_to_type_inserter(set),
- split_option);
- }
- template <typename InputIterator,
- typename T,
- typename Comparator,
- typename Allocator>
- inline std::size_t parse(const std::pair<InputIterator,InputIterator>& range,
- const std::string& delimiters,
- std::multiset<T,Comparator,Allocator>& multiset,
- const split_options::type& split_option = split_options::compress_delimiters)
- {
- typedef typename details::is_valid_iterator<InputIterator>::type itr_type;
- if (1 == delimiters.size())
- return split(single_delimiter_predicate<std::string::value_type>(delimiters[0]),
- range.first,range.second,
- range_to_type_inserter(multiset),
- split_option);
- else
- return split(multiple_char_delimiter_predicate(delimiters),
- range.first,range.second,
- range_to_type_inserter(multiset),
- split_option);
- }
- template <typename InputIterator,
- typename T,
- typename Container>
- inline std::size_t parse(const std::pair<InputIterator,InputIterator>& range,
- const std::string& delimiters,
- std::queue<T,Container>& queue,
- const split_options::type& split_option = split_options::compress_delimiters)
- {
- typedef typename details::is_valid_iterator<InputIterator>::type itr_type;
- if (1 == delimiters.size())
- return split(single_delimiter_predicate<std::string::value_type>(delimiters[0]),
- range.first,range.second,
- range_to_type_push_inserter(queue),
- split_option);
- else
- return split(multiple_char_delimiter_predicate(delimiters),
- range.first,range.second,
- range_to_type_push_inserter(queue),
- split_option);
- }
- template <typename InputIterator,
- typename T,
- typename Container>
- inline std::size_t parse(const std::pair<InputIterator,InputIterator>& range,
- const std::string& delimiters,
- std::stack<T,Container>& stack,
- const split_options::type& split_option = split_options::compress_delimiters)
- {
- typedef typename details::is_valid_iterator<InputIterator>::type itr_type;
- if (1 == delimiters.size())
- return split(single_delimiter_predicate<std::string::value_type>(delimiters[0]),
- range.first,range.second,
- range_to_type_push_inserter(stack),
- split_option);
- else
- return split(multiple_char_delimiter_predicate(delimiters),
- range.first,range.second,
- range_to_type_push_inserter(stack),
- split_option);
- }
- template <typename InputIterator,
- typename T,
- typename Container,
- typename Comparator>
- inline std::size_t parse(const std::pair<InputIterator,InputIterator>& range,
- const std::string& delimiters,
- std::priority_queue<T,Container,Comparator>& priority_queue,
- const split_options::type& split_option = split_options::compress_delimiters)
- {
- typedef typename details::is_valid_iterator<InputIterator>::type itr_type;
- if (1 == delimiters.size())
- return split(single_delimiter_predicate<std::string::value_type>(delimiters[0]),
- range.first,range.second,
- range_to_type_push_inserter(priority_queue),
- split_option);
- else
- return split(multiple_char_delimiter_predicate(delimiters),
- range.first,range.second,
- range_to_type_push_inserter(priority_queue),
- split_option);
- }
- namespace details
- {
- class container_adder
- {
- private:
- class container_adder_base
- {
- public:
- typedef const char* itr_type;
- virtual ~container_adder_base(){}
- template <typename InputIterator>
- inline bool add(const InputIterator begin, const InputIterator end) const
- {
- return add_impl(begin,end);
- }
- template <typename InputIterator>
- inline bool add(const std::pair<InputIterator,InputIterator>& range) const
- {
- return add(range.first,range.second);
- }
- protected:
- virtual bool add_impl(const itr_type begin, const itr_type end) const = 0;
- };
- template <typename T,
- typename Allocator,
- template <typename,typename> class Sequence>
- class sequence_adder_impl : public container_adder_base
- {
- public:
- typedef Sequence<T,Allocator> sequence_t;
- sequence_adder_impl(sequence_t& seq)
- : sequence_(seq)
- {}
- protected:
- bool add_impl(const itr_type begin, const itr_type end) const
- {
- T t;
- if (!string_to_type_converter(begin,end,t)) return false;
- sequence_.push_back(t);
- return true;
- }
- private:
- sequence_adder_impl operator=(const sequence_adder_impl&);
- sequence_t& sequence_;
- };
- template <typename T,
- typename Comparator,
- typename Allocator,
- template <typename,typename,typename> class Set>
- class set_adder_impl : public container_adder_base
- {
- public:
- set_adder_impl(Set<T,Comparator,Allocator>& set)
- : set_(set)
- {}
- bool add_impl(const itr_type begin, const itr_type end) const
- {
- T t;
- if (!string_to_type_converter(begin,end,t)) return false;
- set_.insert(t);
- return true;
- }
- private:
- set_adder_impl operator=(const set_adder_impl&);
- Set<T,Comparator,Allocator>& set_;
- };
- template <typename T,
- typename Container,
- typename Comparator>
- class pq_adder_impl : public container_adder_base
- {
- public:
- pq_adder_impl(std::priority_queue<T,Container,Comparator>& pq)
- : pq_(pq)
- {}
- bool add_impl(const itr_type begin, const itr_type end) const
- {
- T t;
- if (!string_to_type_converter(begin,end,t)) return false;
- pq_.push(t);
- return true;
- }
- private:
- pq_adder_impl operator=(const pq_adder_impl&);
- std::priority_queue<T,Container,Comparator>& pq_;
- };
- template <typename T,
- typename Container,
- template <typename,typename> class SContainer>
- class stack_queue_adder_impl : public container_adder_base
- {
- public:
- stack_queue_adder_impl(SContainer<T,Container>& container)
- : container_(container)
- {}
- bool add_impl(const itr_type begin, const itr_type end) const
- {
- T t;
- if (!string_to_type_converter(begin,end,t)) return false;
- container_.push(t);
- return true;
- }
- private:
- stack_queue_adder_impl operator=(const stack_queue_adder_impl&);
- SContainer<T,Container>& container_;
- };
- public:
- template <typename T, typename Allocator>
- container_adder(std::vector<T,Allocator>& vec)
- : container_adder_base_(new(buffer)sequence_adder_impl<T,Allocator,std::vector>(vec))
- {}
- template <typename T, typename Allocator>
- container_adder(std::deque<T,Allocator>& deq)
- : container_adder_base_(new(buffer)sequence_adder_impl<T,Allocator,std::deque>(deq))
- {}
- template <typename T, typename Allocator>
- container_adder(std::list<T,Allocator>& list)
- : container_adder_base_(new(buffer)sequence_adder_impl<T,Allocator,std::list>(list))
- {}
- template <typename T, typename Comparator, typename Allocator>
- container_adder(std::set<T,Comparator,Allocator>& set)
- : container_adder_base_(new(buffer)set_adder_impl<T,Comparator,Allocator,std::set>(set))
- {}
- template <typename T, typename Comparator, typename Allocator>
- container_adder(std::multiset<T,Comparator,Allocator>& multiset)
- : container_adder_base_(new(buffer)set_adder_impl<T,Comparator,Allocator,std::multiset>(multiset))
- {}
- template <typename T, typename Container, typename Comparator>
- container_adder(std::priority_queue<T,Container,Comparator>& pq)
- : container_adder_base_(new(buffer)pq_adder_impl<T,Container,Comparator>(pq))
- {}
- template <typename T, typename Container>
- container_adder(std::queue<T,Container>& queue)
- : container_adder_base_(new(buffer)stack_queue_adder_impl<T,Container,std::queue>(queue))
- {}
- template <typename T, typename Container>
- container_adder(std::stack<T,Container>& stack)
- : container_adder_base_(new(buffer)stack_queue_adder_impl<T,Container,std::stack>(stack))
- {}
- template <typename InputIterator>
- inline bool add(InputIterator begin, InputIterator end) const
- {
- return container_adder_base_->add(begin,end);
- }
- template <typename InputIterator>
- inline bool add(std::pair<InputIterator,InputIterator>& range) const
- {
- return add(range.first,range.second);
- }
- template <typename InputIterator>
- inline bool operator()(const InputIterator begin, const InputIterator end)
- {
- InputIterator itr = begin;
- while (end != itr)
- {
- if (!add(*itr)) return false;
- ++itr;
- }
- return true;
- }
- private:
- mutable container_adder_base* container_adder_base_;
- unsigned char buffer[64];
- };
- template <typename T,typename is_stl_container_result>
- struct ca_type { typedef T& type; };
- template <typename T>
- struct ca_type <T,details::yes_t> { typedef details::container_adder type; };
- }
- template <typename InputIterator,
- typename T1, typename T2, typename T3,
- typename T4, typename T5, typename T6,
- typename T7, typename T8, typename T9,
- typename T10, typename T11>
- inline bool parse(const InputIterator begin, const InputIterator end,
- const std::string& delimiters,
- T1& t1, T2& t2, T3& t3, T4& t4, T5& t5, T6& t6, T7& t7,
- T8& t8, T9& t9, T10& t10, T11& t11,
- details::container_adder ca)
- {
- typedef typename details::is_valid_iterator<InputIterator>::type itr_type;
- typedef std::pair<InputIterator,InputIterator> iterator_type;
- typedef typename std::deque<iterator_type>::iterator iterator_type_ptr;
- std::deque<iterator_type> token_list;
- if (1 == delimiters.size())
- split(single_delimiter_predicate<std::string::value_type>(delimiters[0]),
- begin,end,
- std::back_inserter(token_list),
- split_options::compress_delimiters);
- else
- split(multiple_char_delimiter_predicate(delimiters),
- begin,end,
- std::back_inserter(token_list),
- split_options::compress_delimiters);
- if (token_list.size() < 12) return false;
- iterator_type_ptr itr = token_list.begin();
- if (!string_to_type_converter((*itr).first,(*itr).second, t1)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second, t2)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second, t3)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second, t4)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second, t5)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second, t6)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second, t7)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second, t8)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second, t9)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t10)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t11)) return false; ++itr;
- return ca(itr,token_list.end());
- }
- template <typename InputIterator,
- typename T1, typename T2, typename T3,
- typename T4, typename T5, typename T6,
- typename T7, typename T8, typename T9,
- typename T10>
- inline bool parse(const InputIterator begin, const InputIterator end,
- const std::string& delimiters,
- T1& t1, T2& t2, T3& t3, T4& t4, T5& t5, T6& t6, T7& t7, T8& t8, T9& t9, T10& t10,
- details::container_adder ca)
- {
- typedef typename details::is_valid_iterator<InputIterator>::type itr_type;
- typedef std::pair<InputIterator,InputIterator> iterator_type;
- typedef typename std::deque<iterator_type>::iterator iterator_type_ptr;
- std::deque<iterator_type> token_list;
- if (1 == delimiters.size())
- split(single_delimiter_predicate<std::string::value_type>(delimiters[0]),
- begin,end,
- std::back_inserter(token_list),
- split_options::compress_delimiters);
- else
- split(multiple_char_delimiter_predicate(delimiters),
- begin,end,
- std::back_inserter(token_list),
- split_options::compress_delimiters);
- if (token_list.size() < 11) return false;
- iterator_type_ptr itr = token_list.begin();
- if (!string_to_type_converter((*itr).first,(*itr).second, t1)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second, t2)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second, t3)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second, t4)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second, t5)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second, t6)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second, t7)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second, t8)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second, t9)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t10)) return false; ++itr;
- return ca(itr,token_list.end());
- }
- template <typename InputIterator,
- typename T1, typename T2, typename T3,
- typename T4, typename T5, typename T6,
- typename T7, typename T8, typename T9>
- inline bool parse(const InputIterator begin, const InputIterator end,
- const std::string& delimiters,
- T1& t1, T2& t2, T3& t3, T4& t4, T5& t5, T6& t6, T7& t7, T8& t8, T9& t9,
- details::container_adder ca)
- {
- typedef typename details::is_valid_iterator<InputIterator>::type itr_type;
- typedef std::pair<InputIterator,InputIterator> iterator_type;
- typedef typename std::deque<iterator_type>::iterator iterator_type_ptr;
- std::deque<iterator_type> token_list;
- if (1 == delimiters.size())
- split(single_delimiter_predicate<std::string::value_type>(delimiters[0]),
- begin,end,
- std::back_inserter(token_list),
- split_options::compress_delimiters);
- else
- split(multiple_char_delimiter_predicate(delimiters),
- begin,end,
- std::back_inserter(token_list),
- split_options::compress_delimiters);
- if (token_list.size() < 10) return false;
- iterator_type_ptr itr = token_list.begin();
- if (!string_to_type_converter((*itr).first,(*itr).second,t1)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t2)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t3)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t4)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t5)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t6)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t7)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t8)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t9)) return false; ++itr;
- return ca(itr,token_list.end());
- }
- template <typename InputIterator,
- typename T1, typename T2, typename T3,
- typename T4, typename T5, typename T6,
- typename T7, typename T8>
- inline bool parse(const InputIterator begin, const InputIterator end,
- const std::string& delimiters,
- T1& t1, T2& t2, T3& t3, T4& t4, T5& t5, T6& t6, T7& t7, T8& t8,
- details::container_adder ca)
- {
- typedef typename details::is_valid_iterator<InputIterator>::type itr_type;
- typedef std::pair<InputIterator,InputIterator> iterator_type;
- typedef typename std::deque<iterator_type>::iterator iterator_type_ptr;
- std::deque<iterator_type> token_list;
- if (1 == delimiters.size())
- split(single_delimiter_predicate<std::string::value_type>(delimiters[0]),
- begin,end,
- std::back_inserter(token_list),
- split_options::compress_delimiters);
- else
- split(multiple_char_delimiter_predicate(delimiters),
- begin,end,
- std::back_inserter(token_list),
- split_options::compress_delimiters);
- if (token_list.size() < 9) return false;
- iterator_type_ptr itr = token_list.begin();
- if (!string_to_type_converter((*itr).first,(*itr).second,t1)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t2)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t3)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t4)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t5)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t6)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t7)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t8)) return false; ++itr;
- return ca(itr,token_list.end());
- }
- template <typename InputIterator,
- typename T1, typename T2, typename T3,
- typename T4, typename T5, typename T6, typename T7>
- inline bool parse(const InputIterator begin, const InputIterator end,
- const std::string& delimiters,
- T1& t1, T2& t2, T3& t3, T4& t4, T5& t5, T6& t6, T7& t7,
- details::container_adder ca)
- {
- typedef typename details::is_valid_iterator<InputIterator>::type itr_type;
- typedef std::pair<InputIterator,InputIterator> iterator_type;
- typedef typename std::deque<iterator_type>::iterator iterator_type_ptr;
- std::deque<iterator_type> token_list;
- if (1 == delimiters.size())
- split(single_delimiter_predicate<std::string::value_type>(delimiters[0]),
- begin,end,
- std::back_inserter(token_list),
- split_options::compress_delimiters);
- else
- split(multiple_char_delimiter_predicate(delimiters),
- begin,end,
- std::back_inserter(token_list),
- split_options::compress_delimiters);
- if (token_list.size() < 8) return false;
- iterator_type_ptr itr = token_list.begin();
- if (!string_to_type_converter((*itr).first,(*itr).second,t1)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t2)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t3)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t4)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t5)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t6)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t7)) return false; ++itr;
- return ca(itr,token_list.end());
- }
- template <typename InputIterator,
- typename T1, typename T2, typename T3,
- typename T4, typename T5, typename T6>
- inline bool parse(const InputIterator begin, const InputIterator end,
- const std::string& delimiters,
- T1& t1, T2& t2, T3& t3, T4& t4, T5& t5, T6& t6,
- details::container_adder ca)
- {
- typedef typename details::is_valid_iterator<InputIterator>::type itr_type;
- typedef std::pair<InputIterator,InputIterator> iterator_type;
- typedef typename std::deque<iterator_type>::iterator iterator_type_ptr;
- std::deque<iterator_type> token_list;
- if (1 == delimiters.size())
- split(single_delimiter_predicate<std::string::value_type>(delimiters[0]),
- begin,end,
- std::back_inserter(token_list),
- split_options::compress_delimiters);
- else
- split(multiple_char_delimiter_predicate(delimiters),
- begin,end,
- std::back_inserter(token_list),
- split_options::compress_delimiters);
- if (token_list.size() < 7) return false;
- iterator_type_ptr itr = token_list.begin();
- if (!string_to_type_converter((*itr).first,(*itr).second,t1)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t2)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t3)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t4)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t5)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t6)) return false; ++itr;
- return ca(itr,token_list.end());
- }
- template <typename InputIterator,
- typename T1, typename T2, typename T3,
- typename T4, typename T5>
- inline bool parse(const InputIterator begin, const InputIterator end,
- const std::string& delimiters,
- T1& t1, T2& t2, T3& t3, T4& t4, T5& t5,
- details::container_adder ca)
- {
- typedef typename details::is_valid_iterator<InputIterator>::type itr_type;
- typedef std::pair<InputIterator,InputIterator> iterator_type;
- typedef typename std::deque<iterator_type>::iterator iterator_type_ptr;
- std::deque<iterator_type> token_list;
- if (1 == delimiters.size())
- split(single_delimiter_predicate<std::string::value_type>(delimiters[0]),
- begin,end,
- std::back_inserter(token_list),
- split_options::compress_delimiters);
- else
- split(multiple_char_delimiter_predicate(delimiters),
- begin,end,
- std::back_inserter(token_list),
- split_options::compress_delimiters);
- if (token_list.size() < 6) return false;
- iterator_type_ptr itr = token_list.begin();
- if (!string_to_type_converter((*itr).first,(*itr).second,t1)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t2)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t3)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t4)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t5)) return false; ++itr;
- return ca(itr,token_list.end());
- }
- template <typename InputIterator,
- typename T1, typename T2, typename T3, typename T4>
- inline bool parse(const InputIterator begin, const InputIterator end,
- const std::string& delimiters,
- T1& t1, T2& t2, T3& t3, T4& t4,
- details::container_adder ca)
- {
- typedef typename details::is_valid_iterator<InputIterator>::type itr_type;
- typedef std::pair<InputIterator,InputIterator> iterator_type;
- typedef typename std::deque<iterator_type>::iterator iterator_type_ptr;
- std::deque<iterator_type> token_list;
- if (1 == delimiters.size())
- split(single_delimiter_predicate<std::string::value_type>(delimiters[0]),
- begin,end,
- std::back_inserter(token_list),
- split_options::compress_delimiters);
- else
- split(multiple_char_delimiter_predicate(delimiters),
- begin,end,
- std::back_inserter(token_list),
- split_options::compress_delimiters);
- if (token_list.size() < 5) return false;
- iterator_type_ptr itr = token_list.begin();
- if (!string_to_type_converter((*itr).first,(*itr).second,t1)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t2)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t3)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t4)) return false; ++itr;
- return ca(itr,token_list.end());
- }
- template <typename InputIterator,
- typename T1, typename T2, typename T3>
- inline bool parse(const InputIterator begin, const InputIterator end,
- const std::string& delimiters,
- T1& t1, T2& t2, T3& t3,
- details::container_adder ca)
- {
- typedef typename details::is_valid_iterator<InputIterator>::type itr_type;
- typedef std::pair<InputIterator,InputIterator> iterator_type;
- typedef typename std::deque<iterator_type>::iterator iterator_type_ptr;
- std::deque<iterator_type> token_list;
- if (1 == delimiters.size())
- split(single_delimiter_predicate<std::string::value_type>(delimiters[0]),
- begin,end,
- std::back_inserter(token_list),
- split_options::compress_delimiters);
- else
- split(multiple_char_delimiter_predicate(delimiters),
- begin,end,
- std::back_inserter(token_list),
- split_options::compress_delimiters);
- if (token_list.size() < 4) return false;
- iterator_type_ptr itr = token_list.begin();
- if (!string_to_type_converter((*itr).first,(*itr).second,t1)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t2)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t3)) return false; ++itr;
- return ca(itr,token_list.end());
- }
- template <typename InputIterator,
- typename T1, typename T2>
- inline bool parse(const InputIterator begin, const InputIterator end,
- const std::string& delimiters,
- T1& t1, T2& t2,
- details::container_adder ca)
- {
- typedef typename details::is_valid_iterator<InputIterator>::type itr_type;
- typedef std::pair<InputIterator,InputIterator> iterator_type;
- typedef typename std::deque<iterator_type>::iterator iterator_type_ptr;
- std::deque<iterator_type> token_list;
- if (1 == delimiters.size())
- split(single_delimiter_predicate<std::string::value_type>(delimiters[0]),
- begin,end,
- std::back_inserter(token_list),
- split_options::compress_delimiters);
- else
- split(multiple_char_delimiter_predicate(delimiters),
- begin,end,
- std::back_inserter(token_list),
- split_options::compress_delimiters);
- if (token_list.size() < 3) return false;
- iterator_type_ptr itr = token_list.begin();
- if (!string_to_type_converter((*itr).first,(*itr).second,t1)) return false; ++itr;
- if (!string_to_type_converter((*itr).first,(*itr).second,t2)) return false; ++itr;
- return ca(itr,token_list.end());
- }
- template <typename InputIterator, typename T1>
- inline bool parse(const InputIterator begin, const InputIterator end,
- const std::string& delimiters,
- T1& t1,
- details::container_adder ca)
- {
- typedef typename details::is_valid_iterator<InputIterator>::type itr_type;
- typedef std::pair<InputIterator,InputIterator> iterator_type;
- typedef typename std::deque<iterator_type>::iterator iterator_type_ptr;
- std::deque<iterator_type> token_list;
- if (1 == delimiters.size())
- split(single_delimiter_predicate<std::string::value_type>(delimiters[0]),
- begin,end,
- std::back_inserter(token_list),
- split_options::compress_delimiters);
- else
- split(multiple_char_delimiter_predicate(delimiters),
- begin,end,
- std::back_inserter(token_list),
- split_options::compress_delimiters);
- if (token_list.size() < 2) return false;
- iterator_type_ptr itr = token_list.begin();
- if (!string_to_type_converter((*itr).first,(*itr).second,t1)) return false; ++itr;
- return ca(itr,token_list.end());
- }
- template <typename InputIterator,
- typename T,
- typename Allocator,
- template <typename,typename> class Sequence>
- inline std::size_t parse_n(const InputIterator begin,
- const InputIterator end,
- const std::string& delimiters,
- const std::size_t& n,
- Sequence<T,Allocator>& sequence,
- const split_options::type& split_option = split_options::compress_delimiters)
- {
- typedef typename details::is_valid_iterator<InputIterator>::type itr_type;
- const std::size_t original_size = sequence.size();
- if (1 == delimiters.size())
- split_n(single_delimiter_predicate<std::string::value_type>(delimiters[0]),
- begin,end,
- n,
- range_to_type_back_inserter(sequence),
- split_option);
- else
- split_n(multiple_char_delimiter_predicate(delimiters),
- begin,end,
- n,
- range_to_type_back_inserter(sequence),
- split_option);
- return sequence.size() - original_size;
- }
- template <typename InputIterator,
- typename T,
- typename Comparator,
- typename Allocator>
- inline std::size_t parse_n(const InputIterator begin,
- const InputIterator end,
- const std::string& delimiters,
- const std::size_t& n,
- std::set<T,Comparator,Allocator>& set,
- const split_options::type& split_option = split_options::compress_delimiters)
- {
- typedef typename details::is_valid_iterator<InputIterator>::type itr_type;
- const std::size_t original_size = set.size();
- if (1 == delimiters.size())
- split_n(single_delimiter_predicate<std::string::value_type>(delimiters[0]),
- begin,end,
- n,
- range_to_type_inserter(set),
- split_option);
- else
- split_n(multiple_char_delimiter_predicate(delimiters),
- begin,end,
- n,
- range_to_type_inserter(set),
- split_option);
- return set.size() - original_size;
- }
- template <typename InputIterator,
- typename T,
- typename Comparator,
- typename Allocator>
- inline std::size_t parse_n(const InputIterator begin,
- const InputIterator end,
- const std::string& delimiters,
- const std::size_t& n,
- std::multiset<T,Comparator,Allocator>& multiset,
- const split_options::type& split_option = split_options::compress_delimiters)
- {
- typedef typename details::is_valid_iterator<InputIterator>::type itr_type;
- const std::size_t original_size = multiset.size();
- if (1 == delimiters.size())
- split_n(single_delimiter_predicate<std::string::value_type>(delimiters[0]),
- begin,end,
- n,
- range_to_type_inserter(multiset),
- split_option);
- else
- split_n(multiple_char_delimiter_predicate(delimiters),
- begin,end,
- n,
- range_to_type_inserter(multiset),
- split_option);
- return multiset.size() - original_size;
- }
- template <typename InputIterator,
- typename T,
- typename Container>
- inline std::size_t parse_n(const InputIterator begin,
- const InputIterator end,
- const std::string& delimiters,
- const std::size_t& n,
- std::queue<T,Container>& queue,
- const split_options::type& split_option = split_options::compress_delimiters)
- {
- typedef typename details::is_valid_iterator<InputIterator>::type itr_type;
- const std::size_t original_size = queue.size();
- if (1 == delimiters.size())
- split_n(single_delimiter_predicate<std::string::value_type>(delimiters[0]),
- begin,end,
- n,
- range_to_type_push_inserter(queue),
- split_option);
- else
- split_n(multiple_char_delimiter_predicate(delimiters),
- begin,end,
- n,
- range_to_type_push_inserter(queue),
- split_option);
- return queue.size() - original_size;
- }
- template <typename InputIterator,
- typename T,
- typename Container>
- inline std::size_t parse_n(const InputIterator begin,
- const InputIterator end,
- const std::string& delimiters,
- const std::size_t& n,
- std::stack<T,Container>& stack,
- const split_options::type& split_option = split_options::compress_delimiters)
- {
- typedef typename details::is_valid_iterator<InputIterator>::type itr_type;
- const std::size_t original_size = stack.size();
- if (1 == delimiters.size())
- split_n(single_delimiter_predicate<std::string::value_type>(delimiters[0]),
- begin,end,
- n,
- range_to_type_push_inserter(stack),
- split_option);
- else
- split_n(multiple_char_delimiter_predicate(delimiters),
- begin,end,
- n,
- range_to_type_push_inserter(stack),
- split_option);
- return stack.size() - original_size;
- }
- template <typename InputIterator,
- typename T,
- typename Container,
- typename Comparator>
- inline std::size_t parse_n(const InputIterator begin,
- const InputIterator end,
- const std::string& delimiters,
- const std::size_t& n,
- std::priority_queue<T,Container,Comparator>& priority_queue,
- const split_options::type& split_option = split_options::compress_delimiters)
- {
- typedef typename details::is_valid_iterator<InputIterator>::type itr_type;
- const std::size_t original_size = priority_queue.size();
- if (1 == delimiters.size())
- split_n(single_delimiter_predicate<std::string::value_type>(delimiters[0]),
- begin,end,
- n,
- range_to_type_push_inserter(priority_queue),
- split_option);
- else
- split_n(multiple_char_delimiter_predicate(delimiters),
- begin,end,
- n,
- range_to_type_push_inserter(priority_queue),
- split_option);
- return priority_queue.size() - original_size;
- }
- template <typename InputIterator, typename T>
- inline std::size_t parse_n(const InputIterator begin,
- const InputIterator end,
- const std::string& delimiters,
- const std::size_t& n,
- T* out,
- const split_options::type& split_option = split_options::compress_delimiters)
- {
- typedef typename details::is_valid_iterator<InputIterator>::type itr_type;
- std::size_t insert_count = 0;
- if (1 == delimiters.size())
- split_n(single_delimiter_predicate<std::string::value_type>(delimiters[0]),
- begin,end,
- n,
- range_to_ptr_type(out,insert_count),
- split_option);
- else
- split_n(multiple_char_delimiter_predicate(delimiters),
- begin,end,
- n,
- range_to_ptr_type(out,insert_count),
- split_option);
- return insert_count;
- }
- template <typename InputIterator,
- typename T,
- typename Allocator,
- template <typename,typename> class Sequence>
- inline std::size_t parse_n(const std::pair<InputIterator,InputIterator>& range,
- const std::string& delimiters,
- const std::size_t& n,
- Sequence<T,Allocator>& sequence,
- const split_options::type& split_option = split_options::compress_delimiters)
- {
- return parse(range.first,range.second,delimiters,n,sequence,split_option);
- }
- template <typename InputIterator,
- typename T,
- typename Comparator,
- typename Allocator>
- inline std::size_t parse_n(const std::pair<InputIterator,InputIterator>& range,
- const std::string& delimiters,
- const std::size_t& n,
- std::set<T,Comparator,Allocator>& set,
- const split_options::type& split_option = split_options::compress_delimiters)
- {
- return parse(range.first,range.second,delimiters,n,set,split_option);
- }
- template <typename InputIterator,
- typename T,
- typename Comparator,
- typename Allocator>
- inline std::size_t parse_n(const std::pair<InputIterator,InputIterator>& range,
- const std::string& delimiters,
- const std::size_t& n,
- std::multiset<T,Comparator,Allocator>& multiset,
- const split_options::type& split_option = split_options::compress_delimiters)
- {
- return parse(range.first,range.second,delimiters,n,multiset,split_option);
- }
- template <typename InputIterator,
- typename T,
- typename Container>
- inline std::size_t parse_n(const std::pair<InputIterator,InputIterator>& range,
- const std::string& delimiters,
- const std::size_t& n,
- std::queue<T,Container>& queue,
- const split_options::type& split_option = split_options::compress_delimiters)
- {
- return parse(range.first,range.second,delimiters,n,queue,split_option);
- }
- template <typename InputIterator,
- typename T,
- typename Container>
- inline std::size_t parse_n(const std::pair<InputIterator,InputIterator>& range,
- const std::string& delimiters,
- const std::size_t& n,
- std::stack<T,Container>& stack,
- const split_options::type& split_option = split_options::compress_delimiters)
- {
- return parse(range.first,range.second,delimiters,n,stack,split_option);
- }
- template <typename InputIterator,
- typename T,
- typename Container,
- typename Comparator>
- inline std::size_t parse_n(const std::pair<InputIterator,InputIterator>& range,
- const std::string& delimiters,
- const std::size_t& n,
- std::priority_queue<T,Container,Comparator>& priority_queue,
- const split_options::type& split_option = split_options::compress_delimiters)
- {
- return parse(range.first,range.second,delimiters,n,priority_queue,split_option);
- }
- template <typename T1, typename T2, typename T3, typename T4,
- typename T5, typename T6, typename T7, typename T8,
- typename T9, typename T10, typename T11, typename T12>
- inline bool parse(const std::string& data,
- const std::string& delimiters,
- T1& t1, T2& t2, T3& t3, T4& t4,
- T5& t5, T6& t6, T7& t7, T8& t8,
- T9& t9, T10& t10, T11& t11, T12& t12)
- {
- return parse(data.data(),
- data.data() + data.size(),
- delimiters,
- t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,
- typename details::ca_type<T12, typename details::is_stl_container<T12>::result_t>::type(t12));
- }
- template <typename T1, typename T2, typename T3, typename T4,
- typename T5, typename T6, typename T7, typename T8,
- typename T9, typename T10, typename T11>
- inline bool parse(const std::string& data,
- const std::string& delimiters,
- T1& t1, T2& t2, T3& t3, T4& t4,
- T5& t5, T6& t6, T7& t7, T8& t8,
- T9& t9, T10& t10, T11& t11)
- {
- return parse(data.data(),
- data.data() + data.size(),
- delimiters,
- t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,
- typename details::ca_type<T11, typename details::is_stl_container<T11>::result_t>::type(t11));
- }
- template <typename T1, typename T2, typename T3, typename T4,
- typename T5, typename T6, typename T7, typename T8,
- typename T9, typename T10>
- inline bool parse(const std::string& data,
- const std::string& delimiters,
- T1& t1, T2& t2, T3& t3, T4& t4,
- T5& t5, T6& t6, T7& t7, T8& t8,
- T9& t9, T10& t10)
- {
- return parse(data.data(),
- data.data() + data.size(),
- delimiters,
- t1,t2,t3,t4,t5,t6,t7,t8,t9,
- typename details::ca_type<T10, typename details::is_stl_container<T10>::result_t>::type(t10));
- }
- template <typename T1, typename T2, typename T3, typename T4,
- typename T5, typename T6, typename T7, typename T8,
- typename T9>
- inline bool parse(const std::string& data,
- const std::string& delimiters,
- T1& t1, T2& t2, T3& t3, T4& t4,
- T5& t5, T6& t6, T7& t7, T8& t8,
- T9& t9)
- {
- return parse(data.data(),
- data.data() + data.size(),
- delimiters,
- t1,t2,t3,t4,t5,t6,t7,t8,
- typename details::ca_type<T9, typename details::is_stl_container<T9>::result_t>::type(t9));
- }
- template <typename T1, typename T2, typename T3, typename T4,
- typename T5, typename T6, typename T7, typename T8>
- inline bool parse(const std::string& data,
- const std::string& delimiters,
- T1& t1, T2& t2, T3& t3, T4& t4,
- T5& t5, T6& t6, T7& t7, T8& t8)
- {
- return parse(data.data(),
- data.data() + data.size(),
- delimiters,
- t1,t2,t3,t4,t5,t6,t7,
- typename details::ca_type<T8, typename details::is_stl_container<T8>::result_t>::type(t8));
- }
- template <typename T1, typename T2, typename T3, typename T4,
- typename T5, typename T6, typename T7>
- inline bool parse(const std::string& data,
- const std::string& delimiters,
- T1& t1, T2& t2, T3& t3, T4& t4,
- T5& t5, T6& t6, T7& t7)
- {
- return parse(data.data(),
- data.data() + data.size(),
- delimiters,
- t1,t2,t3,t4,t5,t6,
- typename details::ca_type<T7, typename details::is_stl_container<T7>::result_t>::type(t7));
- }
- template <typename T1, typename T2, typename T3, typename T4,
- typename T5, typename T6>
- inline bool parse(const std::string& data,
- const std::string& delimiters,
- T1& t1, T2& t2, T3& t3, T4& t4,
- T5& t5, T6& t6)
- {
- return parse(data.data(),
- data.data() + data.size(),
- delimiters,
- t1,t2,t3,t4,t5,
- typename details::ca_type<T6,typename details::is_stl_container<T6>::result_t>::type(t6));
- }
- template <typename T1, typename T2, typename T3, typename T4,
- typename T5>
- inline bool parse(const std::string& data,
- const std::string& delimiters,
- T1& t1, T2& t2, T3& t3, T4& t4,
- T5& t5)
- {
- return parse(data.data(),
- data.data() + data.size(),
- delimiters,
- t1,t2,t3,t4,
- typename details::ca_type<T5, typename details::is_stl_container<T5>::result_t>::type(t5));
- }
- template <typename T1, typename T2, typename T3, typename T4>
- inline bool parse(const std::string& data,
- const std::string& delimiters,
- T1& t1, T2& t2, T3& t3, T4& t4)
- {
- return parse(data.data(),
- data.data() + data.size(),
- delimiters,
- t1,t2,t3,
- typename details::ca_type<T4, typename details::is_stl_container<T4>::result_t>::type(t4));
- }
- template <typename T1, typename T2, typename T3>
- inline bool parse(const std::string& data,
- const std::string& delimiters,
- T1& t1, T2& t2, T3& t3)
- {
- return parse(data.data(),
- data.data() + data.size(),
- delimiters,
- t1,t2,
- typename details::ca_type<T3, typename details::is_stl_container<T3>::result_t>::type(t3));
- }
- template <typename T1, typename T2>
- inline bool parse(const std::string& data,
- const std::string& delimiters,
- T1& t1, T2& t2)
- {
- return parse(data.data(),
- data.data() + data.size(),
- delimiters,
- t1,
- typename details::ca_type<T2, typename details::is_stl_container<T2>::result_t>::type(t2));
- }
- template <typename T>
- inline bool parse(const std::string& data,
- const std::string& delimiters,
- T& t)
- {
- return parse(data.data(),
- data.data() + data.size(),
- delimiters,
- typename details::ca_type<T,typename details::is_stl_container<T>::result_t>::type(t));
- }
- template <typename T,
- typename Allocator,
- template <typename,typename> class Sequence>
- inline std::size_t parse(const std::string& data,
- const std::string& delimiters,
- Sequence<T,Allocator>& sequence,
- const split_options::type& split_option = split_options::compress_delimiters)
- {
- return parse(data.data(),
- data.data() + data.size(),
- delimiters,
- sequence,
- split_option);
- }
- template <typename T,
- typename Comparator,
- typename Allocator>
- inline std::size_t parse(const std::string& data,
- const std::string& delimiters,
- std::set<T,Comparator,Allocator>& set,
- const split_options::type& split_option = split_options::compress_delimiters)
- {
- return parse(data.data(),
- data.data() + data.size(),
- delimiters,
- set,
- split_option);
- }
- template <typename T,
- typename Comparator,
- typename Allocator>
- inline std::size_t parse(const std::string& data,
- const std::string& delimiters,
- std::multiset<T,Comparator,Allocator>& multiset,
- const split_options::type& split_option = split_options::compress_delimiters)
- {
- return parse(data.data(),
- data.data() + data.size(),
- delimiters,
- multiset,
- split_option);
- }
- template <typename T,
- typename Container>
- inline std::size_t parse(const std::string& data,
- const std::string& delimiters,
- std::queue<T,Container>& queue,
- const split_options::type& split_option = split_options::compress_delimiters)
- {
- return parse(data.data(),
- data.data() + data.size(),
- delimiters,
- queue,
- split_option);
- }
- template <typename T,
- typename Container>
- inline std::size_t parse(const std::string& data,
- const std::string& delimiters,
- std::stack<T,Container>& stack,
- const split_options::type& split_option = split_options::compress_delimiters)
- {
- return parse(data.data(),
- data.data() + data.size(),
- delimiters,
- stack,
- split_option);
- }
- template <typename T,
- typename Container,
- typename Comparator>
- inline std::size_t parse(const std::string& data,
- const std::string& delimiters,
- std::priority_queue<T,Container,Comparator>& priority_queue,
- const split_options::type& split_option = split_options::compress_delimiters)
- {
- return parse(data.data(),
- data.data() + data.size(),
- delimiters,
- priority_queue,
- split_option);
- }
- template <typename T,
- typename Allocator,
- template <typename,typename> class Sequence>
- inline std::size_t parse(const int& argc, char* argv[],
- Sequence<T,Allocator>& sequence,
- const bool break_on_fail = true)
- {
- T tmp;
- for (int i = 0; i < argc; ++i)
- {
- if (!string_to_type_converter(std::string(argv[i]),tmp))
- {
- if (break_on_fail)
- return i;
- else
- continue;
- }
- sequence.push_back(tmp);
- }
- return argc;
- }
- template <typename T1, typename T2, typename T3, typename T4,
- typename T5, typename T6, typename T7, typename T8,
- typename T9>
- inline std::size_t parse(const int& argc, char* argv[],
- T1& t1, T2& t2, T3& t3, T4& t4,
- T5& t5, T6& t6, T7& t7, T8& t8,
- T9& t9)
- {
- if (9 != argc) return 0;
- std::size_t result = 0;
- if (!string_to_type_converter(std::string(argv[0]),t1)) return result; ++result;
- if (!string_to_type_converter(std::string(argv[1]),t2)) return result; ++result;
- if (!string_to_type_converter(std::string(argv[2]),t3)) return result; ++result;
- if (!string_to_type_converter(std::string(argv[3]),t4)) return result; ++result;
- if (!string_to_type_converter(std::string(argv[4]),t5)) return result; ++result;
- if (!string_to_type_converter(std::string(argv[5]),t6)) return result; ++result;
- if (!string_to_type_converter(std::string(argv[6]),t7)) return result; ++result;
- if (!string_to_type_converter(std::string(argv[7]),t8)) return result; ++result;
- if (!string_to_type_converter(std::string(argv[8]),t9)) return result; ++result;
- return result;
- }
- template <typename T1, typename T2, typename T3, typename T4,
- typename T5, typename T6, typename T7, typename T8>
- inline std::size_t parse(const int& argc, char* argv[],
- T1& t1, T2& t2, T3& t3, T4& t4,
- T5& t5, T6& t6, T7& t7, T8& t8)
- {
- if (8 != argc) return 0;
- std::size_t result = 0;
- if (!string_to_type_converter(std::string(argv[0]),t1)) return result; ++result;
- if (!string_to_type_converter(std::string(argv[1]),t2)) return result; ++result;
- if (!string_to_type_converter(std::string(argv[2]),t3)) return result; ++result;
- if (!string_to_type_converter(std::string(argv[3]),t4)) return result; ++result;
- if (!string_to_type_converter(std::string(argv[4]),t5)) return result; ++result;
- if (!string_to_type_converter(std::string(argv[5]),t6)) return result; ++result;
- if (!string_to_type_converter(std::string(argv[6]),t7)) return result; ++result;
- if (!string_to_type_converter(std::string(argv[7]),t8)) return result; ++result;
- return result;
- }
- template <typename T1, typename T2, typename T3, typename T4,
- typename T5, typename T6, typename T7>
- inline std::size_t parse(const int& argc, char* argv[],
- T1& t1, T2& t2, T3& t3, T4& t4,
- T5& t5, T6& t6, T7& t7)
- {
- if (7 != argc) return 0;
- std::size_t result = 0;
- if (!string_to_type_converter(std::string(argv[0]),t1)) return result; ++result;
- if (!string_to_type_converter(std::string(argv[1]),t2)) return result; ++result;
- if (!string_to_type_converter(std::string(argv[2]),t3)) return result; ++result;
- if (!string_to_type_converter(std::string(argv[3]),t4)) return result; ++result;
- if (!string_to_type_converter(std::string(argv[4]),t5)) return result; ++result;
- if (!string_to_type_converter(std::string(argv[5]),t6)) return result; ++result;
- if (!string_to_type_converter(std::string(argv[6]),t7)) return result; ++result;
- return result;
- }
- template <typename T1, typename T2, typename T3, typename T4,
- typename T5, typename T6>
- inline std::size_t parse(const int& argc, char* argv[],
- T1& t1, T2& t2, T3& t3, T4& t4,
- T5& t5, T6& t6)
- {
- if (6 != argc) return 0;
- std::size_t result = 0;
- if (!string_to_type_converter(std::string(argv[0]),t1)) return result; ++result;
- if (!string_to_type_converter(std::string(argv[1]),t2)) return result; ++result;
- if (!string_to_type_converter(std::string(argv[2]),t3)) return result; ++result;
- if (!string_to_type_converter(std::string(argv[3]),t4)) return result; ++result;
- if (!string_to_type_converter(std::string(argv[4]),t5)) return result; ++result;
- if (!string_to_type_converter(std::string(argv[5]),t6)) return result; ++result;
- return result;
- }
- template <typename T1, typename T2, typename T3, typename T4, typename T5>
- inline std::size_t parse(const int& argc, char* argv[],
- T1& t1, T2& t2, T3& t3, T4& t4, T5& t5)
- {
- if (5 != argc) return 0;
- std::size_t result = 0;
- if (!string_to_type_converter(std::string(argv[0]),t1)) return result; ++result;
- if (!string_to_type_converter(std::string(argv[1]),t2)) return result; ++result;
- if (!string_to_type_converter(std::string(argv[2]),t3)) return result; ++result;
- if (!string_to_type_converter(std::string(argv[3]),t4)) return result; ++result;
- if (!string_to_type_converter(std::string(argv[4]),t5)) return result; ++result;
- return result;
- }
- template <typename T1, typename T2, typename T3, typename T4>
- inline std::size_t parse(const int& argc, char* argv[],
- T1& t1, T2& t2, T3& t3, T4& t4)
- {
- if (4 != argc) return 0;
- std::size_t result = 0;
- if (!string_to_type_converter(std::string(argv[0]),t1)) return result; ++result;
- if (!string_to_type_converter(std::string(argv[1]),t2)) return result; ++result;
- if (!string_to_type_converter(std::string(argv[2]),t3)) return result; ++result;
- if (!string_to_type_converter(std::string(argv[3]),t4)) return result; ++result;
- return result;
- }
- template <typename T1, typename T2, typename T3>
- inline std::size_t parse(const int& argc, char* argv[],
- T1& t1, T2& t2, T3& t3)
- {
- if (3 != argc) return 0;
- std::size_t result = 0;
- if (!string_to_type_converter(std::string(argv[0]),t1)) return result; ++result;
- if (!string_to_type_converter(std::string(argv[1]),t2)) return result; ++result;
- if (!string_to_type_converter(std::string(argv[2]),t3)) return result; ++result;
- return result;
- }
- template <typename T1, typename T2>
- inline std::size_t parse(const int& argc, char* argv[],
- T1& t1, T2& t2)
- {
- if (2 != argc) return 0;
- std::size_t result = 0;
- if (!string_to_type_converter(std::string(argv[0]),t1)) return result; ++result;
- if (!string_to_type_converter(std::string(argv[1]),t2)) return result; ++result;
- return result;
- }
- template <typename T1>
- inline std::size_t parse(const int& argc, char* argv[],
- T1& t1)
- {
- if (1 != argc) return 0;
- std::size_t result = 0;
- if (!string_to_type_converter(std::string(argv[0]),t1)) return result; ++result;
- return result;
- }
- #define strtk_parse_begin(Type)\
- namespace strtk {\
- bool parse(const std::string& data, const std::string& delimiters, Type& t)\
- { return parse(data,delimiters
- #define strtk_parse_type(T)\
- ,t.T
- #define strtk_parse_hex_type(T)\
- ,t.T
- #define strtk_parse_ignore_token()\
- ,ignore_token()
- #define strtk_parse_end()\
- );}}
- template <typename T,
- typename Allocator,
- template <typename,typename> class Sequence>
- inline std::size_t parse_n(const std::string& data,
- const std::string& delimiters,
- const std::size_t& n,
- Sequence<T,Allocator>& sequence,
- const split_options::type& split_option = split_options::compress_delimiters)
- {
- return parse_n(data.data(),
- data.data() + data.size(),
- delimiters,
- n,
- sequence,
- split_option);
- }
- template <typename T,
- typename Comparator,
- typename Allocator>
- inline std::size_t parse_n(const std::string& data,
- const std::string& delimiters,
- const std::size_t& n,
- std::set<T,Comparator,Allocator>& set,
- const split_options::type& split_option = split_options::compress_delimiters)
- {
- return parse_n(data.data(),
- data.data() + data.size(),
- delimiters,
- n,
- set,
- split_option);
- }
- template <typename T,
- typename Comparator,
- typename Allocator>
- inline std::size_t parse_n(const std::string& data,
- const std::string& delimiters,
- const std::size_t& n,
- std::multiset<T,Comparator,Allocator>& multiset,
- const split_options::type& split_option = split_options::compress_delimiters)
- {
- return parse_n(data.data(),
- data.data() + data.size(),
- delimiters,
- n,
- multiset,
- split_option);
- }
- template <typename T,
- typename Container>
- inline std::size_t parse_n(const std::string& data,
- const std::string& delimiters,
- const std::size_t& n,
- std::queue<T,Container>& queue,
- const split_options::type& split_option = split_options::compress_delimiters)
- {
- return parse_n(data.data(),
- data.data() + data.size(),
- delimiters,
- n,
- queue,
- split_option);
- }
- template <typename T,
- typename Container>
- inline std::size_t parse_n(const std::string& data,
- const std::string& delimiters,
- const std::size_t& n,
- std::stack<T,Container>& stack,
- const split_options::type& split_option = split_options::compress_delimiters)
- {
- return parse_n(data.data(),
- data.data() + data.size(),
- delimiters,
- n,
- stack,
- split_option);
- }
- template <typename T,
- typename Container,
- typename Comparator>
- inline std::size_t parse_n(const std::string& data,
- const std::string& delimiters,
- const std::size_t& n,
- std::priority_queue<T,Container,Comparator>& priority_queue,
- const split_options::type& split_option = split_options::compress_delimiters)
- {
- return parse_n(data.data(),
- data.data() + data.size(),
- delimiters,
- n,
- priority_queue,
- split_option);
- }
- template <typename T>
- inline std::size_t parse_n(const std::string& data,
- const std::string& delimiters,
- const std::size_t& n,
- T* out,
- const split_options::type& split_option = split_options::compress_delimiters)
- {
- return parse_n(data.data(),
- data.data() + data.size(),
- delimiters,
- n,
- out,
- split_option);
- }
- template <typename T1, typename T2, typename T3, typename T4,
- typename T5, typename T6, typename T7, typename T8,
- typename T9, typename T10, typename T11, typename T12>
- inline bool parse_line(std::ifstream& stream,
- const std::string& delimiters,
- T1& t1, T2& t2, T3& t3, T4& t4, T5& t5, T6& t6,
- T7& t7, T8& t8, T9& t9, T10& t10, T11& t11, T12& t12)
- {
- if (!stream)
- return false;
- std::string data;
- data.reserve(strtk::one_kilobyte);
- if (!std::getline(stream,data))
- return false;
- if (data.empty() || delimiters.empty())
- return false;
- return strtk::parse(data.data(),
- data.data() + data.size(),
- delimiters,
- t1,t2,t3,t4,t5,t6,
- t7,t8,t9,t10,t11,t12);
- }
- template <typename T1, typename T2, typename T3, typename T4,
- typename T5, typename T6, typename T7, typename T8,
- typename T9, typename T10, typename T11>
- inline bool parse_line(std::ifstream& stream,
- const std::string& delimiters,
- T1& t1, T2& t2, T3& t3, T4& t4, T5& t5, T6& t6,
- T7& t7, T8& t8, T9& t9, T10& t10, T11& t11)
- {
- if (!stream)
- return false;
- std::string data;
- data.reserve(strtk::one_kilobyte);
- if (!std::getline(stream,data))
- return false;
- if (data.empty() || delimiters.empty())
- return false;
- return strtk::parse(data.data(),
- data.data() + data.size(),
- delimiters,
- t1,t2,t3,t4,t5,t6,
- t7,t8,t9,t10,t11);
- }
- template <typename T1, typename T2, typename T3, typename T4,
- typename T5, typename T6, typename T7, typename T8,
- typename T9, typename T10>
- inline bool parse_line(std::ifstream& stream,
- const std::string& delimiters,
- T1& t1, T2& t2, T3& t3, T4& t4, T5& t5, T6& t6,
- T7& t7, T8& t8, T9& t9, T10& t10)
- {
- if (!stream)
- return false;
- std::string data;
- data.reserve(strtk::one_kilobyte);
- if (!std::getline(stream,data))
- return false;
- if (data.empty() || delimiters.empty())
- return false;
- return strtk::parse(data.data(),
- data.data() + data.size(),
- delimiters,
- t1,t2,t3,t4,t5,t6,
- t7,t8,t9,t10);
- }
- template <typename T1, typename T2, typename T3, typename T4,
- typename T5, typename T6, typename T7, typename T8,
- typename T9>
- inline bool parse_line(std::ifstream& stream,
- const std::string& delimiters,
- T1& t1, T2& t2, T3& t3, T4& t4, T5& t5, T6& t6,
- T7& t7, T8& t8, T9& t9)
- {
- if (!stream)
- return false;
- std::string data;
- data.reserve(strtk::one_kilobyte);
- if (!std::getline(stream,data))
- return false;
- if (data.empty() || delimiters.empty())
- return false;
- return strtk::parse(data.data(),
- data.data() + data.size(),
- delimiters,
- t1,t2,t3,t4,t5,t6,
- t7,t8,t9);
- }
- template <typename T1, typename T2, typename T3, typename T4,
- typename T5, typename T6, typename T7, typename T8>
- inline bool parse_line(std::ifstream& stream,
- const std::string& delimiters,
- T1& t1, T2& t2, T3& t3, T4& t4, T5& t5, T6& t6,
- T7& t7, T8& t8)
- {
- if (!stream)
- return false;
- std::string data;
- data.reserve(strtk::one_kilobyte);
- if (!std::getline(stream,data))
- return false;
- if (data.empty() || delimiters.empty())
- return false;
- return strtk::parse(data.data(),
- data.data() + data.size(),
- delimiters,
- t1,t2,t3,t4,t5,t6,
- t7,t8);
- }
- template <typename T1, typename T2, typename T3, typename T4,
- typename T5, typename T6, typename T7>
- inline bool parse_line(std::ifstream& stream,
- const std::string& delimiters,
- T1& t1, T2& t2, T3& t3, T4& t4, T5& t5, T6& t6,
- T7& t7)
- {
- if (!stream)
- return false;
- std::string data;
- data.reserve(strtk::one_kilobyte);
- if (!std::getline(stream,data))
- return false;
- if (data.empty() || delimiters.empty())
- return false;
- return strtk::parse(data.data(),
- data.data() + data.size(),
- delimiters,
- t1,t2,t3,t4,t5,t6,t7);
- }
- template <typename T1, typename T2, typename T3, typename T4,
- typename T5, typename T6>
- inline bool parse_line(std::ifstream& stream,
- const std::string& delimiters,
- T1& t1, T2& t2, T3& t3, T4& t4, T5& t5, T6& t6)
- {
- if (!stream)
- return false;
- std::string data;
- data.reserve(strtk::one_kilobyte);
- if (!std::getline(stream,data))
- return false;
- if (data.empty() || delimiters.empty())
- return false;
- return strtk::parse(data.data(),
- data.data() + data.size(),
- delimiters,
- t1,t2,t3,t4,t5,t6);
- }
- template <typename T1, typename T2, typename T3, typename T4,
- typename T5>
- inline bool parse_line(std::ifstream& stream,
- const std::string& delimiters,
- T1& t1, T2& t2, T3& t3, T4& t4, T5& t5)
- {
- if (!stream)
- return false;
- std::string data;
- data.reserve(strtk::one_kilobyte);
- if (!std::getline(stream,data))
- return false;
- if (data.empty() || delimiters.empty())
- return false;
- return strtk::parse(data.data(),
- data.data() + data.size(),
- delimiters,
- t1,t2,t3,t4,t5);
- }
- template <typename T1, typename T2, typename T3, typename T4>
- inline bool parse_line(std::ifstream& stream,
- const std::string& delimiters,
- T1& t1, T2& t2, T3& t3, T4& t4)
- {
- if (!stream)
- return false;
- std::string data;
- data.reserve(strtk::one_kilobyte);
- if (!std::getline(stream,data))
- return false;
- if (data.empty() || delimiters.empty())
- return false;
- return strtk::parse(data.data(),
- data.data() + data.size(),
- delimiters,
- t1,t2,t3,t4);
- }
- template <typename T1, typename T2, typename T3>
- inline bool parse_line(std::ifstream& stream,
- const std::string& delimiters,
- T1& t1, T2& t2, T3& t3)
- {
- if (!stream)
- return false;
- std::string data;
- data.reserve(strtk::one_kilobyte);
- if (!std::getline(stream,data))
- return false;
- if (data.empty() || delimiters.empty())
- return false;
- return strtk::parse(data.data(),
- data.data() + data.size(),
- delimiters,
- t1,t2,t3);
- }
- template <typename T1, typename T2>
- inline bool parse_line(std::ifstream& stream,
- const std::string& delimiters,
- T1& t1, T2& t2)
- {
- if (!stream)
- return false;
- std::string data;
- data.reserve(strtk::one_kilobyte);
- if (!std::getline(stream,data))
- return false;
- if (data.empty() || delimiters.empty())
- return false;
- return strtk::parse(data.data(),
- data.data() + data.size(),
- delimiters,
- t1,t2);
- }
- template <typename T1>
- inline bool parse_line(std::ifstream& stream,
- const std::string& delimiters,
- T1& t1)
- {
- if (!stream)
- return false;
- std::string data;
- data.reserve(strtk::one_kilobyte);
- if (!std::getline(stream,data))
- return false;
- if (data.empty() || delimiters.empty())
- return false;
- return strtk::parse(data.data(),
- data.data() + data.size(),
- delimiters,
- t1);
- }
- template <typename T,
- typename Allocator,
- template <typename,typename> class Sequence>
- inline std::size_t parse_line(std::ifstream& stream,
- const std::string& delimiters,
- Sequence<T,Allocator>& sequence,
- const split_options::type& split_option = split_options::compress_delimiters)
- {
- if (!stream)
- return 0;
- std::string data;
- data.reserve(strtk::one_kilobyte);
- if (!std::getline(stream,data))
- return 0;
- if (data.empty() || delimiters.empty())
- return false;
- return strtk::parse(data.data(),
- data.data() + data.size(),
- delimiters,
- sequence,
- split_option);
- }
- template <typename T,
- typename Comparator,
- typename Allocator>
- inline std::size_t parse_line(std::ifstream& stream,
- const std::string& delimiters,
- std::set<T,Comparator,Allocator>& set,
- const split_options::type& split_option = split_options::compress_delimiters)
- {
- if (!stream)
- return 0;
- std::string data;
- data.reserve(strtk::one_kilobyte);
- if (!std::getline(stream,data))
- return 0;
- if (data.empty() || delimiters.empty())
- return false;
- return strtk::parse(data.data(),
- data.data() + data.size(),
- delimiters,
- set,
- split_option);
- }
- template <typename T,
- typename Comparator,
- typename Allocator>
- inline std::size_t parse_line(std::ifstream& stream,
- const std::string& delimiters,
- std::multiset<T,Comparator,Allocator>& multiset,
- const split_options::type& split_option = split_options::compress_delimiters)
- {
- if (!stream)
- return 0;
- std::string data;
- data.reserve(strtk::one_kilobyte);
- if (!std::getline(stream,data))
- return 0;
- if (data.empty() || delimiters.empty())
- return false;
- return strtk::parse(data.data(),
- data.data() + data.size(),
- delimiters,
- multiset,
- split_option);
- }
- template <typename T,
- typename Container>
- inline std::size_t parse_line(std::ifstream& stream,
- const std::string& delimiters,
- std::queue<T,Container>& queue,
- const split_options::type& split_option = split_options::compress_delimiters)
- {
- if (!stream)
- return 0;
- std::string data;
- data.reserve(strtk::one_kilobyte);
- if (!std::getline(stream,data))
- return 0;
- if (data.empty() || delimiters.empty())
- return false;
- return strtk::parse(data.data(),
- data.data() + data.size(),
- delimiters,
- queue,
- split_option);
- }
- template <typename T,
- typename Container>
- inline std::size_t parse_line(std::ifstream& stream,
- const std::string& delimiters,
- std::stack<T,Container>& stack,
- const split_options::type& split_option = split_options::compress_delimiters)
- {
- if (!stream)
- return 0;
- std::string data;
- data.reserve(strtk::one_kilobyte);
- if (!std::getline(stream,data))
- return 0;
- if (data.empty() || delimiters.empty())
- return false;
- return strtk::parse(data.data(),
- data.data() + data.size(),
- delimiters,
- stack,
- split_option);
- }
- template <typename T,
- typename Container,
- typename Comparator>
- inline std::size_t parse_line(std::ifstream& stream,
- const std::string& delimiters,
- std::priority_queue<T,Container,Comparator>& priority_queue,
- const split_options::type& split_option = split_options::compress_delimiters)
- {
- if (!stream)
- return 0;
- std::string data;
- data.reserve(strtk::one_kilobyte);
- if (!std::getline(stream,data))
- return 0;
- if (data.empty() || delimiters.empty())
- return false;
- return strtk::parse(data.data(),
- data.data() + data.size(),
- delimiters,
- priority_queue,
- split_option);
- }
- template <typename T,
- typename Allocator,
- template <typename,typename> class Sequence>
- inline std::size_t parse_line_n(std::ifstream& stream,
- const std::string& delimiters,
- const std::size_t& n,
- Sequence<T,Allocator>& sequence,
- const split_options::type& split_option = split_options::compress_delimiters)
- {
- if (!stream)
- return 0;
- std::string data;
- data.reserve(strtk::one_kilobyte);
- if (!std::getline(stream,data))
- return 0;
- if (data.empty() || delimiters.empty())
- return 0;
- return strtk::parse_n(data.data(),
- data.data() + data.size(),
- delimiters,
- n,
- sequence,
- split_option);
- }
- template <typename T,
- typename Comparator,
- typename Allocator>
- inline std::size_t parse_line_n(std::ifstream& stream,
- const std::string& delimiters,
- const std::size_t& n,
- std::set<T,Comparator,Allocator>& set,
- const split_options::type& split_option = split_options::compress_delimiters)
- {
- if (!stream)
- return 0;
- std::string data;
- data.reserve(strtk::one_kilobyte);
- if (!std::getline(stream,data))
- return 0;
- if (data.empty() || delimiters.empty())
- return 0;
- return strtk::parse_n(data.data(),
- data.data() + data.size(),
- delimiters,
- n,
- set,
- split_option);
- }
- template <typename T,
- typename Comparator,
- typename Allocator>
- inline std::size_t parse_line_n(std::ifstream& stream,
- const std::string& delimiters,
- const std::size_t& n,
- std::multiset<T,Comparator,Allocator>& multiset,
- const split_options::type& split_option = split_options::compress_delimiters)
- {
- if (!stream)
- return 0;
- std::string data;
- data.reserve(strtk::one_kilobyte);
- if (!std::getline(stream,data))
- return 0;
- if (data.empty() || delimiters.empty())
- return 0;
- return strtk::parse_n(data.data(),
- data.data() + data.size(),
- delimiters,
- n,
- multiset,
- split_option);
- }
- template <typename T,
- typename Container>
- inline std::size_t parse_line_n(std::ifstream& stream,
- const std::string& delimiters,
- const std::size_t& n,
- std::queue<T,Container>& queue,
- const split_options::type& split_option = split_options::compress_delimiters)
- {
- if (!stream)
- return 0;
- std::string data;
- data.reserve(strtk::one_kilobyte);
- if (!std::getline(stream,data))
- return 0;
- if (data.empty() || delimiters.empty())
- return 0;
- return strtk::parse_n(data.data(),
- data.data() + data.size(),
- delimiters,
- n,
- queue,
- split_option);
- }
- template <typename T,
- typename Container>
- inline std::size_t parse_line_n(std::ifstream& stream,
- const std::string& delimiters,
- const std::size_t& n,
- std::stack<T,Container>& stack,
- const split_options::type& split_option = split_options::compress_delimiters)
- {
- if (!stream)
- return 0;
- std::string data;
- data.reserve(strtk::one_kilobyte);
- if (!std::getline(stream,data))
- return 0;
- if (data.empty() || delimiters.empty())
- return 0;
- return strtk::parse_n(data.data(),
- data.data() + data.size(),
- delimiters,
- n,
- stack,
- split_option);
- }
- template <typename T,
- typename Container,
- typename Comparator>
- inline std::size_t parse_line_n(std::ifstream& stream,
- const std::string& delimiters,
- const std::size_t& n,
- std::priority_queue<T,Container,Comparator>& priority_queue,
- const split_options::type& split_option = split_options::compress_delimiters)
- {
- if (!stream)
- return 0;
- std::string data;
- data.reserve(strtk::one_kilobyte);
- if (!std::getline(stream,data))
- return 0;
- if (data.empty() || delimiters.empty())
- return 0;
- return strtk::parse_n(data.data(),
- data.data() + data.size(),
- delimiters,
- n,
- priority_queue,
- split_option);
- }
- template <typename T1, typename T2, typename T3, typename T4,
- typename T5, typename T6, typename T7, typename T8,
- typename T9, typename T10, typename T11, typename T12>
- inline void construct(std::string& output,
- const std::string& delimiter,
- const T1& t1, const T2& t2, const T3& t3, const T4& t4,
- const T5& t5, const T6& t6, const T7& t7, const T8& t8,
- const T9& t9, const T10& t10, const T11& t11, const T12& t12)
- {
- output += type_to_string( t1); output += delimiter;
- output += type_to_string( t2); output += delimiter;
- output += type_to_string( t3); output += delimiter;
- output += type_to_string( t4); output += delimiter;
- output += type_to_string( t5); output += delimiter;
- output += type_to_string( t6); output += delimiter;
- output += type_to_string( t7); output += delimiter;
- output += type_to_string( t8); output += delimiter;
- output += type_to_string( t9); output += delimiter;
- output += type_to_string(t10); output += delimiter;
- output += type_to_string(t11); output += delimiter;
- output += type_to_string(t12);
- }
- template <typename T1, typename T2, typename T3, typename T4,
- typename T5, typename T6, typename T7, typename T8,
- typename T9, typename T10, typename T11>
- inline void construct(std::string& output,
- const std::string& delimiter,
- const T1& t1, const T2& t2, const T3& t3, const T4& t4,
- const T5& t5, const T6& t6, const T7& t7, const T8& t8,
- const T9& t9, const T10& t10, const T11& t11)
- {
- output += type_to_string( t1); output += delimiter;
- output += type_to_string( t2); output += delimiter;
- output += type_to_string( t3); output += delimiter;
- output += type_to_string( t4); output += delimiter;
- output += type_to_string( t5); output += delimiter;
- output += type_to_string( t6); output += delimiter;
- output += type_to_string( t7); output += delimiter;
- output += type_to_string( t8); output += delimiter;
- output += type_to_string( t9); output += delimiter;
- output += type_to_string(t10); output += delimiter;
- output += type_to_string(t11);
- }
- template <typename T1, typename T2, typename T3, typename T4,
- typename T5, typename T6, typename T7, typename T8,
- typename T9, typename T10>
- inline void construct(std::string& output,
- const std::string& delimiter,
- const T1& t1, const T2& t2, const T3& t3, const T4& t4,
- const T5& t5, const T6& t6, const T7& t7, const T8& t8,
- const T9& t9, const T10& t10)
- {
- output += type_to_string(t1); output += delimiter;
- output += type_to_string(t2); output += delimiter;
- output += type_to_string(t3); output += delimiter;
- output += type_to_string(t4); output += delimiter;
- output += type_to_string(t5); output += delimiter;
- output += type_to_string(t6); output += delimiter;
- output += type_to_string(t7); output += delimiter;
- output += type_to_string(t8); output += delimiter;
- output += type_to_string(t9); output += delimiter;
- output += type_to_string(t10);
- }
- template <typename T1, typename T2, typename T3, typename T4,
- typename T5, typename T6, typename T7, typename T8,
- typename T9>
- inline void construct(std::string& output,
- const std::string& delimiter,
- const T1& t1, const T2& t2, const T3& t3, const T4& t4,
- const T5& t5, const T6& t6, const T7& t7, const T8& t8,
- const T9& t9)
- {
- output += type_to_string(t1); output += delimiter;
- output += type_to_string(t2); output += delimiter;
- output += type_to_string(t3); output += delimiter;
- output += type_to_string(t4); output += delimiter;
- output += type_to_string(t5); output += delimiter;
- output += type_to_string(t6); output += delimiter;
- output += type_to_string(t7); output += delimiter;
- output += type_to_string(t8); output += delimiter;
- output += type_to_string(t9);
- }
- template <typename T1, typename T2, typename T3, typename T4,
- typename T5, typename T6, typename T7, typename T8>
- inline void construct(std::string& output,
- const std::string& delimiter,
- const T1& t1, const T2& t2, const T3& t3, const T4& t4,
- const T5& t5, const T6& t6, const T7& t7, const T8& t8)
- {
- output += type_to_string(t1); output += delimiter;
- output += type_to_string(t2); output += delimiter;
- output += type_to_string(t3); output += delimiter;
- output += type_to_string(t4); output += delimiter;
- output += type_to_string(t5); output += delimiter;
- output += type_to_string(t6); output += delimiter;
- output += type_to_string(t7); output += delimiter;
- output += type_to_string(t8);
- }
- template <typename T1, typename T2, typename T3, typename T4,
- typename T5, typename T6, typename T7>
- inline void construct(std::string& output,
- const std::string& delimiter,
- const T1& t1, const T2& t2, const T3& t3, const T4& t4,
- const T5& t5, const T6& t6, const T7& t7)
- {
- output += type_to_string(t1); output += delimiter;
- output += type_to_string(t2); output += delimiter;
- output += type_to_string(t3); output += delimiter;
- output += type_to_string(t4); output += delimiter;
- output += type_to_string(t5); output += delimiter;
- output += type_to_string(t6); output += delimiter;
- output += type_to_string(t7);
- }
- template <typename T1, typename T2, typename T3, typename T4,
- typename T5,typename T6>
- inline void construct(std::string& output,
- const std::string& delimiter,
- const T1& t1, const T2& t2, const T3& t3, const T4& t4,
- const T5& t5, const T6& t6)
- {
- output += type_to_string(t1); output += delimiter;
- output += type_to_string(t2); output += delimiter;
- output += type_to_string(t3); output += delimiter;
- output += type_to_string(t4); output += delimiter;
- output += type_to_string(t5); output += delimiter;
- output += type_to_string(t6);
- }
- template <typename T1, typename T2, typename T3, typename T4,
- typename T5>
- inline void construct(std::string& output,
- const std::string& delimiter,
- const T1& t1, const T2& t2, const T3& t3, const T4& t4,
- const T5& t5)
- {
- output += type_to_string(t1); output += delimiter;
- output += type_to_string(t2); output += delimiter;
- output += type_to_string(t3); output += delimiter;
- output += type_to_string(t4); output += delimiter;
- output += type_to_string(t5);
- }
- template <typename T1, typename T2, typename T3, typename T4>
- inline void construct(std::string& output,
- const std::string& delimiter,
- const T1& t1, const T2& t2, const T3& t3, const T4& t4)
- {
- output += type_to_string(t1); output += delimiter;
- output += type_to_string(t2); output += delimiter;
- output += type_to_string(t3); output += delimiter;
- output += type_to_string(t4);
- }
- template <typename T1, typename T2, typename T3>
- inline void construct(std::string& output,
- const std::string& delimiter,
- const T1& t1, const T2& t2, const T3& t3)
- {
- output += type_to_string(t1); output += delimiter;
- output += type_to_string(t2); output += delimiter;
- output += type_to_string(t3);
- }
- template <typename T1, typename T2>
- inline void construct(std::string& output,
- const std::string& delimiter,
- const T1& t1, const T2& t2)
- {
- output += type_to_string(t1); output += delimiter;
- output += type_to_string(t2);
- }
- template <typename InputIterator>
- inline void join(std::string& output,
- const std::string& delimiter,
- const InputIterator begin,
- const InputIterator end)
- {
- InputIterator itr = begin;
- while (end != itr)
- {
- output += type_to_string(*itr);
- if (end == (++itr))
- break;
- else
- output += delimiter;
- }
- }
- template <typename InputIterator>
- inline void join(std::string& output,
- const std::string& delimiter,
- const std::pair<InputIterator,InputIterator>& range)
- {
- InputIterator itr = range.first;
- while (range.second != itr)
- {
- output += type_to_string(*itr);
- if (range.second == (++itr))
- break;
- else
- output += delimiter;
- }
- }
- template <typename T,
- typename Allocator,
- template <typename,typename> class Sequence>
- inline void join(std::string& output,
- const std::string& delimiter,
- const Sequence<T,Allocator>& sequence)
- {
- join(output,delimiter,sequence.begin(),sequence.end());
- }
- template <typename T,
- typename Comparator,
- typename Allocator>
- inline void join(std::string& output,
- const std::string& delimiter,
- const std::set<T,Comparator,Allocator>& set)
- {
- join(output,delimiter,set.begin(),set.end());
- }
- template <typename T,
- typename Comparator,
- typename Allocator>
- inline void join(std::string& output,
- const std::string& delimiter,
- const std::multiset<T,Comparator,Allocator>& multiset)
- {
- join(output,delimiter,multiset.begin(),multiset.end());
- }
- inline void join(std::string& output,
- const std::string& delimiter,
- int argc, char* argv[])
- {
- for (int i = 0; i < argc; ++i)
- {
- output += argv[i];
- if (i < (argc - 1))
- output += delimiter;
- }
- }
- template <typename InputIterator>
- inline std::string join(const std::string& delimiter,
- const InputIterator begin,
- const InputIterator end)
- {
- std::string output;
- output.reserve(one_kilobyte);
- join(output,delimiter,begin,end);
- return output;
- }
- template <typename InputIterator>
- inline std::string join(const std::string& delimiter,
- const std::pair<InputIterator,InputIterator>& range)
- {
- std::string output;
- output.reserve(one_kilobyte);
- join(output,delimiter,range.first,range.second);
- return output;
- }
- template <typename T,
- typename Allocator,
- template <typename,typename> class Sequence>
- inline std::string join(const std::string& delimiter,
- const Sequence<T,Allocator>& sequence)
- {
- if (sequence.empty())
- return "";
- else
- return join(delimiter,sequence.begin(),sequence.end());
- }
- template <typename T,
- typename Comparator,
- typename Allocator>
- inline std::string join(const std::string& delimiter,
- const std::set<T,Comparator,Allocator>& set)
- {
- if (set.empty())
- return "";
- else
- return join(delimiter,set.begin(),set.end());
- }
- template <typename T,
- typename Comparator,
- typename Allocator>
- inline std::string join(const std::string& delimiter,
- const std::multiset<T,Comparator,Allocator>& multiset)
- {
- if (multiset.empty())
- return "";
- else
- return join(delimiter,multiset.begin(),multiset.end());
- }
- inline std::string join(const std::string& delimiter, int argc, char* argv[])
- {
- std::string result;
- result.reserve(one_kilobyte);
- join(result,delimiter,argc,argv);
- return result;
- }
- template <typename InputIterator, typename Predicate>
- inline void join_if(std::string& output,
- const std::string& delimiter,
- Predicate predicate,
- const InputIterator begin,
- const InputIterator end)
- {
- InputIterator itr = begin;
- bool first_time = true;
- while (end != itr)
- {
- if (predicate(*itr))
- {
- if (!first_time)
- output += delimiter;
- else
- first_time = false;
- output += type_to_string(*itr);
- }
- if (end == (++itr))
- break;
- }
- }
- template <typename InputIterator, typename Predicate>
- inline void join_if(std::string& output,
- const std::string& delimiter,
- Predicate predicate,
- const std::pair<InputIterator,InputIterator>& range)
- {
- InputIterator itr = range.first;
- bool first_time = true;
- while (range.second != itr)
- {
- if (predicate(*itr))
- {
- if (!first_time)
- output += delimiter;
- else
- first_time = false;
- output += type_to_string(*itr);
- }
- if (range.second == (++itr))
- break;
- }
- }
- template <typename T,
- typename Predicate,
- typename Allocator,
- template <typename,typename> class Sequence>
- inline void join_if(std::string& output,
- const std::string& delimiter,
- Predicate predicate,
- const Sequence<T,Allocator>& sequence)
- {
- join_if(output,delimiter,predicate,sequence.begin(),sequence.end());
- }
- template <typename T,
- typename Predicate,
- typename Comparator,
- typename Allocator>
- inline void join_if(std::string& output,
- const std::string& delimiter,
- Predicate predicate,
- const std::set<T,Comparator,Allocator>& set)
- {
- join_if(output,delimiter,predicate,set.begin(),set.end());
- }
- template <typename T,
- typename Predicate,
- typename Comparator,
- typename Allocator>
- inline void join_if(std::string& output,
- const std::string& delimiter,
- Predicate predicate,
- const std::multiset<T,Comparator,Allocator>& multiset)
- {
- join_if(output,delimiter,predicate,multiset.begin(),multiset.end());
- }
- template <typename InputIterator, typename Predicate>
- inline std::string join_if(const std::string& delimiter,
- Predicate predicate,
- const InputIterator begin,
- const InputIterator end)
- {
- std::string output;
- output.reserve(one_kilobyte);
- join_if(output,delimiter,predicate,begin,end);
- return output;
- }
- template <typename InputIterator, typename Predicate>
- inline std::string join_if(const std::string& delimiter,
- Predicate predicate,
- const std::pair<InputIterator,InputIterator>& range)
- {
- std::string output;
- output.reserve(one_kilobyte);
- join_if(output,delimiter,predicate,range.first,range.second);
- return output;
- }
- template <typename T,
- typename Predicate,
- typename Allocator,
- template <typename,typename> class Sequence>
- inline std::string join_if(const std::string& delimiter,
- Predicate predicate,
- const Sequence<T,Allocator>& sequence)
- {
- return join(delimiter,predicate,sequence.begin(),sequence.end());
- }
- template <typename T,
- typename Predicate,
- typename Comparator,
- typename Allocator>
- inline std::string join_if(const std::string& delimiter,
- Predicate predicate,
- const std::set<T,Comparator,Allocator>& set)
- {
- return join_if(delimiter,predicate,set.begin(),set.end());
- }
- template <typename T,
- typename Predicate,
- typename Comparator,
- typename Allocator>
- inline std::string join_if(const std::string& delimiter,
- Predicate predicate,
- const std::multiset<T,Comparator,Allocator>& multiset)
- {
- return join_if(delimiter,predicate,multiset.begin(),multiset.end());
- }
- class build_string
- {
- public:
- build_string(const std::size_t& initial_size = 64)
- {
- data_.reserve(initial_size);
- }
- template <typename T>
- inline build_string& operator << (const T& t)
- {
- data_ += type_to_string(t);
- return (*this);
- }
- inline operator std::string () const
- {
- return data_;
- }
- inline std::string as_string() const
- {
- return data_;
- }
- inline operator const char* () const
- {
- return data_.data();
- }
- private:
- std::string data_;
- };
- inline void replicate(const std::size_t& n,
- const std::string& str,
- std::string& output)
- {
- if (0 == n) return;
- output.reserve(output.size() + (str.size() * n));
- for (std::size_t i = 0; i < n; ++i)
- {
- output.append(str);
- }
- }
- inline std::string replicate(const std::size_t& n,
- const std::string& str)
- {
- std::string output;
- replicate(n,str,output);
- return output;
- }
- inline void replicate_inplace(const std::size_t& n,
- std::string& str)
- {
- std::string temp_str = str;
- str.reserve(str.size() + (str.size() * n));
- for (std::size_t i = 0; i < n; ++i)
- {
- str.append(temp_str);
- }
- }
- template <typename InputIterator>
- inline void bracketize(std::string& output,
- const std::string& pre,
- const std::string& post,
- const InputIterator begin,
- const InputIterator end)
- {
- InputIterator itr = begin;
- std::string s;
- s.reserve(one_kilobyte);
- while (end != itr)
- {
- s.clear();
- s.append(pre);
- s.append(type_to_string(*itr));
- s.append(post);
- output.append(s);
- ++itr;
- }
- }
- template <typename T,
- typename Allocator,
- template <typename,typename> class Sequence>
- inline void bracketize(std::string& output,
- const std::string& pre,
- const std::string& post,
- Sequence<T,Allocator>& sequence)
- {
- bracketize(output,pre,post,sequence.begin(),sequence.end());
- }
- template <typename T,
- typename Comparator,
- typename Allocator>
- inline void bracketize(std::string& output,
- const std::string& pre,
- const std::string& post,
- std::set<T,Comparator,Allocator>& set)
- {
- bracketize(output,pre,post,set.begin(),set.end());
- }
- template <typename T,
- typename Comparator,
- typename Allocator>
- inline void bracketize(std::string& output,
- const std::string& pre,
- const std::string& post,
- std::multiset<T,Comparator,Allocator>& multiset)
- {
- bracketize(output,pre,post,multiset.begin(),multiset.end());
- }
- template <typename InputIterator>
- inline std::string bracketize(const std::string& pre,
- const std::string& post,
- const InputIterator begin,
- const InputIterator end)
- {
- std::string output;
- output.reserve(one_kilobyte);
- bracketize(output,pre,post,begin,end);
- return output;
- }
- template <typename T,
- typename Allocator,
- template <typename,typename> class Sequence>
- inline std::string bracketize(const std::string& pre,
- const std::string& post,
- Sequence<T,Allocator>& sequence)
- {
- return bracketize(pre,post,sequence.begin(),sequence.end());
- }
- template <typename T,
- typename Comparator,
- typename Allocator>
- inline std::string bracketize(const std::string& pre,
- const std::string& post,
- std::set<T,Comparator,Allocator>& set)
- {
- return bracketize(pre,post,set.begin(),set.end());
- }
- template <typename T,
- typename Comparator,
- typename Allocator>
- inline std::string bracketize(const std::string& pre,
- const std::string& post,
- std::multiset<T,Comparator,Allocator>& multiset)
- {
- return bracketize(pre,post,multiset.begin(),multiset.end());
- }
- template <typename T>
- struct interval_inserter
- {
- typedef T type;
- interval_inserter(const std::size_t& interval, const T& t)
- : count_(0),
- interval_(interval),
- t_(t)
- {}
- inline bool operator()(const type&)
- {
- if (++count_ == interval_)
- {
- count_ = 0;
- return true;
- }
- else
- return false;
- }
- inline T operator()()
- {
- return t_;
- }
- private:
- std::size_t count_;
- std::size_t interval_;
- T t_;
- };
- template <typename Inserter,
- typename InputIterator,
- typename OutputIterator>
- inline std::size_t inserter(Inserter ins,
- const InputIterator begin, const InputIterator end,
- OutputIterator out)
- {
- std::size_t size = 0;
- InputIterator itr = begin;
- while (end != itr)
- {
- (*out) = (*itr);
- ++out;
- if (ins(*itr++))
- {
- (*out) = ins();
- ++out;
- size += 2;
- }
- else
- ++size;
- }
- return size;
- }
- template <typename Iterator, typename T>
- inline void iota(Iterator begin, Iterator end, T value)
- {
- Iterator itr = begin;
- while (end != itr)
- {
- (*itr) = value++;
- ++itr;
- }
- }
- template <typename T>
- inline void iota(typename range::adapter<T>& r, T value)
- {
- iota(r.begin(),r.end(),value);
- }
- template <typename T,
- typename Allocator,
- template <typename,typename> class Sequence>
- inline void iota(Sequence<T,Allocator>& sequence, std::size_t count, T value)
- {
- while (count)
- {
- sequence.push_back(value++);
- --count;
- }
- }
- template <typename T,
- typename Comparator,
- typename Allocator>
- inline void iota(std::set<T,Comparator,Allocator>& set, std::size_t count, T value)
- {
- while (count)
- {
- set.insert(value++);
- --count;
- }
- }
- template <typename T,
- typename Comparator,
- typename Allocator>
- inline void iota(std::multiset<T,Comparator,Allocator>& multiset, std::size_t count, T value)
- {
- while (count)
- {
- multiset.insert(value++);
- --count;
- }
- }
- template <typename OutputIterator, typename T>
- inline void iota(std::size_t count, T value, OutputIterator out)
- {
- while (count)
- {
- (*out) = value++;
- ++out;
- --count;
- }
- }
- template <typename T,
- typename Allocator,
- template <typename,typename> class Sequence>
- inline void iota(Sequence<T,Allocator>& sequence, const T& value)
- {
- strtk::iota(sequence.begin(),sequence.end(),value);
- }
- template <typename T,
- typename Comparator,
- typename Allocator>
- inline void iota(std::set<T,Comparator,Allocator>& set, const T& value)
- {
- strtk::iota(set.begin(),set.end(),value);
- }
- template <typename T,
- typename Comparator,
- typename Allocator>
- inline void iota(std::multiset<T,Comparator,Allocator>& multiset, const T& value)
- {
- strtk::iota(multiset.begin(),multiset.end(),value);
- }
- template <typename InputIterator, typename OutputIterator>
- inline void cut(const std::size_t& r0, const std::size_t& r1,
- const InputIterator begin, InputIterator end,
- OutputIterator out)
- {
- // static assert: InputIterator must be of type std::string
- InputIterator itr = begin;
- while (end != itr)
- {
- const std::string& s = (*itr);
- ++itr;
- if (s.size() < r0)
- continue;
- (*out++) = s.substr(r0,std::min(r1,s.size()) - r0);
- }
- }
- template <typename Allocator,
- template <typename,typename> class Sequence,
- typename OutputIterator>
- inline void cut(const std::size_t& r0, const std::size_t& r1,
- const Sequence<std::string, Allocator>& sequence,
- OutputIterator out)
- {
- cut(r0,r1,sequence.begin(),sequence.end(),out);
- }
- template <typename Iterator>
- inline void cut_inplace(const std::size_t& r0, const std::size_t& r1,
- const Iterator begin, const Iterator end)
- {
- // static assert: InputIterator must be of type std::string
- Iterator itr = begin;
- while (end != itr)
- {
- if ((*itr).size() >= r0)
- {
- (*itr) = (*itr).substr(r0,std::min(r1,(*itr).size()) - r0);
- }
- ++itr;
- }
- }
- template <typename Allocator,
- template <typename,typename> class Sequence>
- inline void cut(const std::size_t& r0, const std::size_t& r1,
- const Sequence<std::string, Allocator>& sequence)
- {
- cut(r0,r1,sequence.begin(),sequence.end());
- }
- template <typename Comparator, typename Allocator>
- inline void cut(const std::size_t& r0, const std::size_t& r1,
- const std::set<std::string, Comparator, Allocator>& set)
- {
- cut(r0,r1,set.begin(),set.end());
- }
- template <typename Comparator, typename Allocator>
- inline void cut(const std::size_t& r0, const std::size_t& r1,
- const std::multiset<std::string, Comparator, Allocator>& multiset)
- {
- cut(r0,r1,multiset.begin(),multiset.end());
- }
- class translation_table
- {
- public:
- translation_table(const std::string& itable, const std::string& otable)
- {
- if (itable.size() != otable.size())
- {
- throw std::runtime_error("translation_table() - Input/Output table size mismatch.");
- }
- strtk::iota(table_, table_ + 256, static_cast<unsigned char>(0));
- for (std::size_t i = 0; i < itable.size(); ++i)
- {
- table_[static_cast<unsigned int>(itable[i])] = static_cast<unsigned char>(otable[i]);
- }
- }
- inline char operator()(const char c) const
- {
- return static_cast<char>(table_[static_cast<unsigned int>(c)]);
- }
- inline unsigned char operator()(const unsigned char c) const
- {
- return static_cast<unsigned char>(table_[static_cast<unsigned int>(c)]);
- }
- private:
- unsigned char table_[256];
- };
- inline std::string translate(const translation_table& trans_table, const std::string& s)
- {
- std::string result = s;
- std::transform(result.begin(),result.end(),result.begin(),trans_table);
- return result;
- }
- inline void translate_inplace(const translation_table& trans_table, std::string& s)
- {
- std::transform(s.begin(),s.end(),s.begin(),trans_table);
- }
- #ifdef strtk_enable_random
- inline void generate_random_data(unsigned char* data,
- std::size_t length,
- unsigned int pre_gen_cnt = 0,
- unsigned int seed = magic_seed)
- {
- boost::mt19937 rng(static_cast<boost::mt19937::result_type>(seed));
- boost::uniform_int<unsigned int> dist(std::numeric_limits<unsigned int>::min(),std::numeric_limits<unsigned int>::max());
- boost::variate_generator<boost::mt19937&, boost::uniform_int<unsigned int> > rnd(rng,dist);
- if (pre_gen_cnt > 0)
- {
- while (pre_gen_cnt--) rnd();
- }
- unsigned char* itr = data;
- unsigned int* x = 0;
- while (length >= sizeof(unsigned int))
- {
- x = reinterpret_cast<unsigned int*>(itr);
- (*x) = rnd();
- itr += sizeof(unsigned int);
- length -= sizeof(unsigned int);
- }
- if (length > 0)
- {
- itr -= (sizeof(unsigned int) - length);
- x = reinterpret_cast<unsigned int*>(itr);
- (*x) = rnd();
- }
- }
- namespace details
- {
- struct rand_int_type_tag {};
- struct rand_real_type_tag {};
- template <typename T> struct supported_random_type {};
- #define strtk_register_rand_int_type_tag(T)\
- template<> struct supported_random_type<T> { typedef rand_int_type_tag type; enum { value = true }; };
- #define strtk_register_rand_real_type_tag(T)\
- template<> struct supported_random_type<T> { typedef rand_real_type_tag type; enum { value = true }; };
- strtk_register_rand_int_type_tag(char)
- strtk_register_rand_int_type_tag(unsigned char)
- strtk_register_rand_int_type_tag(short)
- strtk_register_rand_int_type_tag(int)
- strtk_register_rand_int_type_tag(long)
- strtk_register_rand_int_type_tag(unsigned short)
- strtk_register_rand_int_type_tag(unsigned int)
- strtk_register_rand_int_type_tag(unsigned long)
- strtk_register_rand_real_type_tag(float)
- strtk_register_rand_real_type_tag(double)
- strtk_register_rand_real_type_tag(long double)
- #undef strtk_register_rand_int_type_tag
- #undef strtk_register_rand_real_type_tag
- template <typename T, typename OutputIterator, typename RandomNumberGenerator>
- inline void generate_random_values_impl(const std::size_t& count,
- const T& min,
- const T& max,
- OutputIterator out,
- RandomNumberGenerator& rng,
- rand_int_type_tag)
- {
- // Note: The implied range will be: [min,max]
- using namespace boost;
- variate_generator<RandomNumberGenerator&,uniform_int<T> > rnd(rng,uniform_int<T>(min,max));
- for (std::size_t i = 0; i < count; ++i, *out++ = rnd()) ;
- }
- template <typename T, typename OutputIterator, typename RandomNumberGenerator>
- inline void generate_random_values_impl(const std::size_t& count,
- const T& min,
- const T& max,
- OutputIterator out,
- RandomNumberGenerator& rng,
- rand_real_type_tag)
- {
- // Note: The implied range will be: [min,max)
- using namespace boost;
- variate_generator<RandomNumberGenerator&, uniform_real<T> > rnd(rng,uniform_real<T>(min,max));
- for (std::size_t i = 0; i < count; ++i, *out++ = rnd()) ;
- }
- } // namespace details
- class uniform_real_rng
- {
- private:
- typedef boost::mt19937 rng_type;
- typedef boost::variate_generator<rng_type, boost::uniform_real<double> > variate_type;
- public:
- uniform_real_rng(const std::size_t& seed = magic_seed,
- std::size_t pregen = 0)
- : rng_(static_cast<rng_type::result_type>(seed)),
- rnd_(rng_,boost::uniform_real<double>(0.0,1.0))
- {
- while (pregen--) rng_();
- }
- inline double operator()()
- {
- return rnd_();
- }
- private:
- rng_type rng_;
- variate_type rnd_;
- };
- template <typename T, typename OutputIterator>
- inline void generate_random_values(const std::size_t& count,
- const T& min,
- const T& max,
- OutputIterator out,
- const std::size_t& seed = magic_seed,
- const std::size_t& pregen = 0)
- {
- typename details::supported_random_type<T>::type type;
- boost::mt19937 rng(static_cast<boost::mt19937::result_type>(seed));
- for (std::size_t i = 0; i++ < pregen; rng()) ;
- generate_random_values_impl(count,min,max,out,rng,type);
- }
- template <typename T,
- typename Allocator,
- template <typename,typename> class Sequence>
- inline void generate_random_values(const std::size_t& count,
- const T& min,
- const T& max,
- Sequence<T,Allocator>& sequence,
- const std::size_t& seed = magic_seed,
- const std::size_t& pregen = 0)
- {
- typename details::supported_random_type<T>::type type;
- boost::mt19937 rng(static_cast<boost::mt19937::result_type>(seed));
- for (std::size_t i = 0; i++ < pregen; rng()) ;
- generate_random_values_impl(count,min,max,std::back_inserter(sequence),rng,type);
- }
- template <typename Iterator,
- typename RandomNumberGenerator,
- typename OutputIterator>
- inline void random_permutation(const Iterator begin, const Iterator end,
- RandomNumberGenerator& rng,
- OutputIterator out)
- {
- const std::size_t size = std::distance(begin,end);
- if ((rng. min() < 0.0) || (rng.max() > 1.0)) return;
- std::deque<std::size_t> index;
- for (std::size_t i = 0; i < size; index.push_back(i++)) ;
- while (!index.empty())
- {
- std::size_t idx = static_cast<std::size_t>(index.size() * rng());
- (*out) = *(begin + index[idx]);
- index.erase(index.begin() + idx);
- ++out;
- }
- }
- template <typename Iterator,
- typename OutputIterator>
- inline void random_permutation(const Iterator begin, const Iterator end,
- OutputIterator out,
- const std::size_t& seed = magic_seed,
- const std::size_t& pregen = 0)
- {
- boost::mt19937 rng(static_cast<boost::mt19937::result_type>(seed));
- for (std::size_t i = 0; i++ < pregen; rng()) ;
- boost::uniform_real<double> dist(0.0,1.0);
- boost::variate_generator<boost::mt19937&, boost::uniform_real<double> > rnd(rng,dist);
- random_permutation(begin,end,rnd,out);
- }
- template <typename T,
- typename Allocator,
- template <typename,typename> class Sequence,
- typename OutputIterator>
- inline void random_permutation(const Sequence<T,Allocator>& sequence,
- OutputIterator out,
- const std::size_t& seed = magic_seed,
- const std::size_t& pregen = 0)
- {
- random_permutation(sequence.begin(),sequence.end(),out,seed,pregen);
- }
- template <typename Iterator,
- typename RandomNumberGenerator,
- typename OutputIterator>
- inline bool random_combination(const Iterator begin, const Iterator end,
- std::size_t set_size,
- RandomNumberGenerator& rng,
- OutputIterator out)
- {
- const std::size_t size = std::distance(begin,end);
- if ((size < set_size) || (rng. min() < 0.0) || (rng.max() > 1.0)) return false;
- std::deque<std::size_t> index;
- for (std::size_t i = 0; i < size; index.push_back(i++)) ;
- while (set_size)
- {
- std::size_t idx = static_cast<std::size_t>(index.size() * rng());
- (*out) = *(begin + index[idx]);
- index.erase(index.begin() + idx);
- ++out;
- --set_size;
- }
- return true;
- }
- template <typename Iterator,
- typename OutputIterator>
- inline void random_combination(const Iterator begin, const Iterator end,
- const std::size_t& set_size,
- OutputIterator out,
- const std::size_t& seed = magic_seed,
- const std::size_t& pregen = 0)
- {
- boost::mt19937 rng(static_cast<boost::mt19937::result_type>(seed));
- for (std::size_t i = 0; i++ < pregen; rng()) ;
- boost::uniform_real<double> dist(0.0,1.0);
- boost::variate_generator<boost::mt19937&, boost::uniform_real<double> > rnd(rng,dist);
- random_combination(begin,end,set_size,rnd,out);
- }
- template <typename T,
- typename Allocator,
- template <typename,typename> class Sequence,
- typename OutputIterator>
- inline void random_combination(const Sequence<T,Allocator>& sequence,
- const std::size_t& set_size,
- OutputIterator out,
- const std::size_t& seed = magic_seed,
- const std::size_t& pregen = 0)
- {
- random_combination(sequence.begin(),sequence.end(),set_size,out,seed,pregen);
- }
- template <typename Iterator,
- typename OutputIterator,
- typename RandomNumberGenerator>
- inline std::size_t select_k_randomly(const Iterator begin, const Iterator end,
- const std::size_t k,
- OutputIterator out,
- RandomNumberGenerator& rng)
- {
- typedef typename std::iterator_traits<Iterator>::value_type T;
- std::vector<T> selection;
- selection.resize(k);
- Iterator itr = begin;
- std::size_t index = 0;
- while ((index < k) && (end != itr))
- {
- selection[index] = (*itr);
- ++index;
- ++itr;
- }
- if (0 == index)
- return 0;
- else if (index < k)
- {
- std::copy(selection.begin(),selection.begin() + index, out);
- return index;
- }
- double n = k + 1;
- while (end != itr)
- {
- if (rng() < (k / n))
- {
- selection[static_cast<std::size_t>(rng() * k)] = (*itr);
- }
- ++itr;
- ++n;
- }
- std::copy(selection.begin(),selection.end(),out);
- return k;
- }
- template <typename Iterator,
- typename OutputIterator,
- typename RandomNumberGenerator>
- inline void select_1_randomly(const Iterator begin, const Iterator end,
- OutputIterator out,
- RandomNumberGenerator& rng)
- {
- typedef typename std::iterator_traits<Iterator>::value_type T;
- T selection;
- if (begin == end)
- return;
- Iterator itr = begin;
- std::size_t n = 0;
- while (end != itr)
- {
- if (rng() < (1.0 / ++n))
- {
- selection = (*itr);
- }
- ++itr;
- }
- (*out) = selection;
- ++out;
- }
- #endif // strtk_enable_random
- template <typename Iterator>
- inline bool next_combination(const Iterator first, Iterator k, const Iterator last)
- {
- /* Credits: Thomas Draper */
- if ((first == last) || (first == k) || (last == k))
- return false;
- Iterator itr1 = first;
- Iterator itr2 = last;
- ++itr1;
- if (last == itr1)
- return false;
- itr1 = last;
- --itr1;
- itr1 = k;
- --itr2;
- while (first != itr1)
- {
- if (*--itr1 < (*itr2))
- {
- Iterator j = k;
- while (!((*itr1) < (*j))) ++j;
- std::iter_swap(itr1,j);
- ++itr1;
- ++j;
- itr2 = k;
- std::rotate(itr1,j,last);
- while (last != j)
- {
- ++j;
- ++itr2;
- }
- std::rotate(k,itr2,last);
- return true;
- }
- }
- std::rotate(first,k,last);
- return false;
- }
- template <typename T,
- typename Allocator,
- template <typename,typename> class Sequence>
- inline bool next_combination(Sequence<T,Allocator>& sequence, const std::size_t& size)
- {
- return next_combination(sequence.begin(), sequence.begin() + size, sequence.end());
- }
- template <typename Iterator, typename Function>
- inline void for_each_permutation(Iterator begin, Iterator end, Function function)
- {
- do
- {
- function(begin,end);
- }
- while (std::next_permutation(begin,end));
- }
- template <typename Iterator, typename Function>
- inline bool for_each_permutation_conditional(Iterator begin, Iterator end, Function function)
- {
- do
- {
- if (!function(begin,end))
- return false;
- }
- while (std::next_permutation(begin,end));
- return true;
- }
- namespace details
- {
- /*
- Credits:
- (C) Copyright Howard Hinnant 2005-2011.
- Use, modification and distribution are subject to the Boost Software License,
- Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
- http://www.boost.org/LICENSE_1_0.txt).
- */
- template <typename Iterator>
- static inline void rotate_discontinuous(Iterator first1, Iterator last1,
- typename std::iterator_traits<Iterator>::difference_type d1,
- Iterator first2, Iterator last2,
- typename std::iterator_traits<Iterator>::difference_type d2)
- {
- using std::swap;
- if (d1 <= d2)
- std::rotate(first2, std::swap_ranges(first1, last1, first2), last2);
- else
- {
- Iterator i1 = last1;
- while (first2 != last2)
- {
- swap(*--i1,*--last2);
- }
- std::rotate(first1, i1, last1);
- }
- }
- template <typename Iterator, class Function>
- inline void combine_discontinuous(Iterator first1, Iterator last1, typename std::iterator_traits<Iterator>::difference_type d1,
- Iterator first2, Iterator last2, typename std::iterator_traits<Iterator>::difference_type d2,
- Function& f,
- typename std::iterator_traits<Iterator>::difference_type d = 0)
- {
- typedef typename std::iterator_traits<Iterator>::difference_type D;
- using std::swap;
- if ((0 == d1) || (0 == d2))
- return f();
- if (1 == d1)
- {
- Iterator i2 = first2;
- while (i2 != last2)
- {
- f();
- swap(*first1, *i2);
- ++i2;
- }
- }
- else
- {
- Iterator f1p = first1;
- std::advance(f1p,1);
- Iterator i2 = first2;
- D d22 = d2;
- while (i2 != last2)
- {
- combine_discontinuous(f1p, last1, d1 - 1, i2, last2, d22, f, d + 1);
- swap(*first1, *i2);
- ++i2;
- --d22;
- }
- }
- f();
- if (0 != d)
- {
- Iterator f2p = first2;
- std::advance(f2p,1);
- rotate_discontinuous(first1, last1, d1, f2p, last2, d2 - 1);
- }
- else
- rotate_discontinuous(first1, last1, d1, first2, last2, d2);
- }
- template <typename Iterator, class Function>
- inline bool combine_discontinuous_conditional(Iterator first1, Iterator last1, typename std::iterator_traits<Iterator>::difference_type d1,
- Iterator first2, Iterator last2, typename std::iterator_traits<Iterator>::difference_type d2,
- Function& f,
- typename std::iterator_traits<Iterator>::difference_type d = 0)
- {
- typedef typename std::iterator_traits<Iterator>::difference_type D;
- using std::swap;
- if (d1 == 0 || d2 == 0)
- return f();
- if (d1 == 1)
- {
- for (Iterator i2 = first2; i2 != last2; ++i2)
- {
- if (!f())
- return false;
- swap(*first1, *i2);
- }
- }
- else
- {
- Iterator f1p = first1;
- std::advance(f1p,1);
- Iterator i2 = first2;
- for (D d22 = d2; i2 != last2; ++i2, --d22)
- {
- if (!combine_discontinuous_conditional(f1p, last1, d1-1, i2, last2, d22, f, d + 1))
- return false;
- swap(*first1, *i2);
- }
- }
- if (!f())
- return false;
- if (d != 0)
- {
- Iterator f2p = first2;
- std::advance(f2p,1);
- rotate_discontinuous(first1, last1, d1, f2p, last2, d2 - 1);
- }
- else
- rotate_discontinuous(first1, last1, d1, first2, last2, d2);
- return true;
- }
- template <class Function, typename Iterator>
- class bound_range
- {
- public:
- bound_range(Function f, Iterator first, Iterator last)
- : f_(f),
- first_(first),
- last_(last)
- {}
- inline void operator()()
- {
- f_(first_,last_);
- }
- private:
- inline bound_range& operator=(const bound_range&);
- Function f_;
- Iterator first_;
- Iterator last_;
- };
- template <class Function, typename Iterator>
- class bound_range_conditional
- {
- public:
- bound_range_conditional(Function f, Iterator first, Iterator last)
- : f_(f),
- first_(first),
- last_(last)
- {}
- inline bool operator()()
- {
- return f_(first_,last_);
- }
- private:
- inline bound_range_conditional& operator=(const bound_range_conditional&);
- Function f_;
- Iterator first_;
- Iterator last_;
- };
- }
- template <typename Iterator, typename Function>
- inline void for_each_combination(Iterator begin, Iterator end,
- const std::size_t& size,
- Function function)
- {
- if (static_cast<typename std::iterator_traits<Iterator>::difference_type>(size) > std::distance(begin,end))
- return;
- Iterator mid = begin + size;
- details::bound_range<Function&, Iterator> func(function,begin,mid);
- details::combine_discontinuous(begin, mid,
- std::distance(begin,mid),
- mid, end,
- std::distance(mid,end),
- func);
- }
- template <typename Iterator, typename Function>
- inline void for_each_combination_conditional(Iterator begin, Iterator end,
- const std::size_t& size,
- Function function)
- {
- if (static_cast<typename std::iterator_traits<Iterator>::difference_type>(size) > std::distance(begin,end))
- return;
- Iterator mid = begin + size;
- details::bound_range_conditional<Function&, Iterator> func(function,begin,mid);
- details::combine_discontinuous_conditional(begin, mid,
- std::distance(begin,mid),
- mid, end,
- std::distance(mid,end),
- func);
- }
- inline unsigned long long int n_choose_k(const unsigned long long int& n, const unsigned long long int& k)
- {
- if (n < k) return 0;
- if (0 == n) return 0;
- if (0 == k) return 1;
- if (n == k) return 1;
- if (1 == k) return n;
- typedef unsigned long long int value_type;
- class n_choose_k_impl
- {
- public:
- n_choose_k_impl(value_type* table, const value_type& dimension)
- : table_(table),
- dimension_(dimension / 2)
- {}
- inline value_type& lookup(const value_type& n, const value_type& k)
- {
- const std::size_t difference = static_cast<std::size_t>(n - k);
- return table_[static_cast<std::size_t>((dimension_ * n) + std::min<value_type>(k,difference))];
- }
- inline value_type compute(const value_type& n, const value_type& k)
- {
- // n-Choose-k = (n-1)-Choose-(k-1) + (n-1)-Choose-k
- if ((0 == k) || (k == n))
- return 1;
- value_type v1 = lookup(n - 1,k - 1);
- if (0 == v1)
- v1 = lookup(n - 1,k - 1) = compute(n - 1,k - 1);
- value_type v2 = lookup(n - 1,k);
- if (0 == v2)
- v2 = lookup(n - 1,k) = compute(n - 1,k);
- return v1 + v2;
- }
- value_type* table_;
- const value_type dimension_;
- private:
- inline n_choose_k_impl& operator=(const n_choose_k_impl&)
- {
- return *this;
- }
- };
- static const std::size_t static_table_dim = 100;
- static const std::size_t static_table_size = static_cast<std::size_t>((static_table_dim * static_table_dim) / 2);
- static value_type static_table[static_table_size];
- static bool static_table_initialized = false;
- if (!static_table_initialized && (n <= static_table_dim))
- {
- std::fill_n(static_table,static_table_size,0);
- static_table_initialized = true;
- }
- const std::size_t table_size = static_cast<std::size_t>(n * (n / 2) + (n & 1));
- unsigned long long int dimension = static_table_dim;
- value_type* table = 0;
- if (table_size <= static_table_size)
- table = static_table;
- else
- {
- dimension = n;
- table = new value_type[table_size];
- std::fill_n(table,table_size,0ULL);
- }
- value_type result = n_choose_k_impl(table,dimension).compute(n,k);
- if (table != static_table)
- delete [] table;
- return result;
- }
- inline void initialize_n_choose_k()
- {
- const unsigned long long int max_n = 100ULL;
- for (unsigned long long int n = 0; n < max_n; ++n)
- {
- for (unsigned long long int k = 0; k < max_n; ++k)
- {
- n_choose_k(n,k);
- }
- }
- }
- template <typename OutputIterator>
- inline void nth_combination_sequence(unsigned long long int n,
- const std::size_t& r,
- const std::size_t& k,
- OutputIterator out,
- const bool complete_index = true)
- {
- //Compute the indicies for the n'th combination of r-choose-k
- //n must be in the range [0,r-choose-k)
- typedef unsigned long long int value_type;
- std::vector<std::size_t> index_list(k,0);
- value_type j = 0;
- value_type x = 0;
- ++n;
- for (std::size_t i = 1; i <= (k - 1); ++i)
- {
- index_list[i - 1] = 0;
- if (1 < i)
- {
- index_list[i - 1] = index_list[i - 2];
- }
- do
- {
- index_list[i - 1] += 1;
- j = n_choose_k(r - index_list[i - 1], k - i);
- x += j;
- }
- while (n > x);
- x -= j;
- }
- index_list[k - 1] = index_list[k - 2] + static_cast<std::size_t>(n) - static_cast<std::size_t>(x);
- for (std::size_t i = 0; i < index_list.size(); --index_list[i++]);
- std::copy(index_list.begin(),index_list.end(),out);
- if (complete_index)
- {
- std::vector<unsigned int> exist_table(r,0);
- for (std::size_t i = 0; i < index_list.size(); ++i)
- {
- exist_table[index_list[i]] = 1;
- }
- for (std::size_t i = 0; i < exist_table.size(); ++i)
- {
- if (0 == exist_table[i])
- {
- (*out) = i;
- ++out;
- }
- }
- }
- }
- template <typename InputIterator, typename OutputIterator>
- inline void nth_combination_sequence(const std::size_t& n,
- const std::size_t& k,
- const InputIterator begin,
- const InputIterator end,
- OutputIterator out,
- const bool complete_index = true)
- {
- const std::size_t length = std::distance(begin,end);
- std::vector<std::size_t> index_list;
- nth_combination_sequence(n,length,k,std::back_inserter(index_list),complete_index);
- for (std::size_t i = 0; i < index_list.size(); ++i)
- {
- (*out) = *(begin + index_list[i]);
- ++out;
- }
- }
- template <typename OutputIterator>
- inline void nth_permutation_sequence(std::size_t n, const std::size_t k, OutputIterator out)
- {
- //Note: n in [0,k!)
- std::vector<std::size_t> factorid (k,0);
- std::vector<std::size_t> permutate(k,0);
- factorid[0] = 1;
- for (std::size_t i = 1; i < k; ++i)
- {
- factorid[i] = factorid[i - 1] * i;
- }
- for (std::size_t i = 0; i < k; ++i)
- {
- permutate[i] = n / factorid[k - i - 1];
- n = n % factorid[k - i - 1];
- }
- for (std::size_t i = k - 1; i > 0; --i)
- {
- for (int j = static_cast<int>(i - 1); j >= 0; --j)
- {
- if (permutate[j] <= permutate[i])
- {
- ++permutate[i];
- }
- }
- }
- for (std::size_t i = 0; i < k; ++i)
- {
- *(out++) = permutate[i];
- }
- }
- template <typename InputIterator, typename OutputIterator>
- inline void nth_permutation_sequence(std::size_t n,
- const InputIterator begin,
- const InputIterator end,
- OutputIterator out)
- {
- const std::size_t size = std::distance(begin,end);
- std::vector<std::size_t> index_list(size,0);
- nth_permutation_sequence(n,size,index_list.begin());
- for (std::size_t i = 0; i < size; ++i)
- {
- *(out++) = (begin + index_list[i]);
- }
- }
- inline std::string nth_permutation_sequence(const std::size_t& n, const std::string& s)
- {
- std::vector<std::size_t> index_list(s.size(),0);
- nth_permutation_sequence(n,s.size(),index_list.begin());
- std::string result;
- result.reserve(s.size());
- for (std::size_t i = 0; i < index_list.size(); ++i)
- {
- result += s[index_list[i]];
- }
- return result;
- }
- template <typename Iterator>
- class combination_iterator : public std::iterator<std::forward_iterator_tag,
- std::pair<Iterator,Iterator>,
- void,
- void>
- {
- public:
- typedef Iterator iterator;
- typedef const iterator const_iterator;
- typedef std::pair<Iterator,Iterator> range_type;
- explicit inline combination_iterator(const std::size_t& k,
- iterator begin, iterator end,
- const bool sorted = true)
- : begin_(begin),
- end_(end),
- middle_(begin + k),
- current_combination_(begin_,middle_)
- {
- if (!sorted)
- {
- std::sort(begin,end);
- }
- }
- template <typename T,
- typename Allocator,
- template <typename,typename> class Sequence>
- explicit inline combination_iterator(const std::size_t& k,
- Sequence<T,Allocator>& seq,
- const bool sorted = true)
- : begin_(seq.begin()),
- end_(seq.end()),
- middle_(begin_ + k),
- current_combination_(begin_,middle_)
- {
- if (!sorted)
- {
- std::sort(begin_,end_);
- }
- }
- explicit inline combination_iterator(const std::size_t& k,
- std::string& str,
- const bool sorted = true)
- : begin_(const_cast<char*>(str.data())),
- end_(const_cast<char*>(str.data() + str.size())),
- middle_(begin_ + k),
- current_combination_(begin_,middle_)
- {
- if (!sorted)
- {
- std::sort(begin_,end_);
- }
- }
- inline combination_iterator(iterator end)
- : begin_(end),
- end_(end),
- middle_(end),
- current_combination_(end,end)
- {}
- inline combination_iterator(const std::string& str)
- : begin_(const_cast<char*>(str.data() + str.size())),
- end_(begin_),
- middle_(end_),
- current_combination_(end_,end_)
- {}
- template <typename T,
- typename Allocator,
- template <typename,typename> class Sequence>
- explicit inline combination_iterator(Sequence<T,Allocator>& seq)
- : begin_(seq.end()),
- end_(seq.end()),
- middle_(end_),
- current_combination_(end_,end_)
- {}
- inline combination_iterator& operator++()
- {
- if (begin_ != end_)
- {
- if (!next_combination(begin_,middle_,end_))
- {
- begin_ = middle_ = end_;
- }
- }
- return (*this);
- }
- inline combination_iterator operator++(int)
- {
- combination_iterator tmp = *this;
- this->operator++();
- return tmp;
- }
- inline combination_iterator& operator+=(const int inc)
- {
- if (inc > 0)
- {
- for (int i = 0; i < inc; ++i, ++(*this)) ;
- }
- return (*this);
- }
- inline range_type operator*() const
- {
- return current_combination_;
- }
- inline bool operator==(const combination_iterator& itr) const
- {
- return (begin_ == itr.begin_ ) &&
- (end_ == itr.end_ ) &&
- (middle_ == itr.middle_);
- }
- inline bool operator!=(const combination_iterator& itr) const
- {
- return !operator==(itr);
- }
- protected:
- iterator begin_;
- iterator end_;
- iterator middle_;
- range_type current_combination_;
- };
- namespace fast
- {
- /*
- Note: The following routines perform no sanity checks at all
- upon the input data. Hence they should only be used with
- data that is known to be completely 'valid'.
- */
- namespace details
- {
- template <typename Iterator, int N>
- struct all_digits_check_impl
- {
- static inline bool process(Iterator)
- {
- throw std::runtime_error("all_digits_check_impl - unsupported value for N.");
- }
- };
- template <typename Iterator>
- struct all_digits_check_impl<Iterator,19>
- {
- static inline bool process(Iterator itr)
- {
- return static_cast<unsigned char>(itr[0] - '0') < 10 &&
- all_digits_check_impl<Iterator,18>::process(itr + 1);
- }
- };
- template <typename Iterator>
- struct all_digits_check_impl<Iterator,18>
- {
- static inline bool process(Iterator itr)
- {
- return static_cast<unsigned char>(itr[0] - '0') < 10 &&
- all_digits_check_impl<Iterator,17>::process(itr + 1);
- }
- };
- template <typename Iterator>
- struct all_digits_check_impl<Iterator,17>
- {
- static inline bool process(Iterator itr)
- {
- return static_cast<unsigned char>(itr[0] - '0') < 10 &&
- all_digits_check_impl<Iterator,16>::process(itr + 1);
- }
- };
- template <typename Iterator>
- struct all_digits_check_impl<Iterator,16>
- {
- static inline bool process(Iterator itr)
- {
- return static_cast<unsigned char>(itr[ 0] - '0') < 10 &&
- static_cast<unsigned char>(itr[ 1] - '0') < 10 &&
- static_cast<unsigned char>(itr[ 2] - '0') < 10 &&
- static_cast<unsigned char>(itr[ 3] - '0') < 10 &&
- static_cast<unsigned char>(itr[ 4] - '0') < 10 &&
- static_cast<unsigned char>(itr[ 5] - '0') < 10 &&
- static_cast<unsigned char>(itr[ 6] - '0') < 10 &&
- static_cast<unsigned char>(itr[ 7] - '0') < 10 &&
- static_cast<unsigned char>(itr[ 8] - '0') < 10 &&
- static_cast<unsigned char>(itr[ 9] - '0') < 10 &&
- static_cast<unsigned char>(itr[10] - '0') < 10 &&
- static_cast<unsigned char>(itr[11] - '0') < 10 &&
- static_cast<unsigned char>(itr[12] - '0') < 10 &&
- static_cast<unsigned char>(itr[13] - '0') < 10 &&
- static_cast<unsigned char>(itr[14] - '0') < 10 &&
- static_cast<unsigned char>(itr[15] - '0') < 10;
- }
- };
- template <typename Iterator>
- struct all_digits_check_impl<Iterator,15>
- {
- static inline bool process(Iterator itr)
- {
- return static_cast<unsigned char>(itr[ 0] - '0') < 10 &&
- static_cast<unsigned char>(itr[ 1] - '0') < 10 &&
- static_cast<unsigned char>(itr[ 2] - '0') < 10 &&
- static_cast<unsigned char>(itr[ 3] - '0') < 10 &&
- static_cast<unsigned char>(itr[ 4] - '0') < 10 &&
- static_cast<unsigned char>(itr[ 5] - '0') < 10 &&
- static_cast<unsigned char>(itr[ 6] - '0') < 10 &&
- static_cast<unsigned char>(itr[ 7] - '0') < 10 &&
- static_cast<unsigned char>(itr[ 8] - '0') < 10 &&
- static_cast<unsigned char>(itr[ 9] - '0') < 10 &&
- static_cast<unsigned char>(itr[10] - '0') < 10 &&
- static_cast<unsigned char>(itr[11] - '0') < 10 &&
- static_cast<unsigned char>(itr[12] - '0') < 10 &&
- static_cast<unsigned char>(itr[13] - '0') < 10 &&
- static_cast<unsigned char>(itr[14] - '0') < 10;
- }
- };
- template <typename Iterator>
- struct all_digits_check_impl<Iterator,14>
- {
- static inline bool process(Iterator itr)
- {
- return static_cast<unsigned char>(itr[ 0] - '0') < 10 &&
- static_cast<unsigned char>(itr[ 1] - '0') < 10 &&
- static_cast<unsigned char>(itr[ 2] - '0') < 10 &&
- static_cast<unsigned char>(itr[ 3] - '0') < 10 &&
- static_cast<unsigned char>(itr[ 4] - '0') < 10 &&
- static_cast<unsigned char>(itr[ 5] - '0') < 10 &&
- static_cast<unsigned char>(itr[ 6] - '0') < 10 &&
- static_cast<unsigned char>(itr[ 7] - '0') < 10 &&
- static_cast<unsigned char>(itr[ 8] - '0') < 10 &&
- static_cast<unsigned char>(itr[ 9] - '0') < 10 &&
- static_cast<unsigned char>(itr[10] - '0') < 10 &&
- static_cast<unsigned char>(itr[11] - '0') < 10 &&
- static_cast<unsigned char>(itr[12] - '0') < 10 &&
- static_cast<unsigned char>(itr[13] - '0') < 10;
- }
- };
- template <typename Iterator>
- struct all_digits_check_impl<Iterator,13>
- {
- static inline bool process(Iterator itr)
- {
- return static_cast<unsigned char>(itr[ 0] - '0') < 10 &&
- static_cast<unsigned char>(itr[ 1] - '0') < 10 &&
- static_cast<unsigned char>(itr[ 2] - '0') < 10 &&
- static_cast<unsigned char>(itr[ 3] - '0') < 10 &&
- static_cast<unsigned char>(itr[ 4] - '0') < 10 &&
- static_cast<unsigned char>(itr[ 5] - '0') < 10 &&
- static_cast<unsigned char>(itr[ 6] - '0') < 10 &&
- static_cast<unsigned char>(itr[ 7] - '0') < 10 &&
- static_cast<unsigned char>(itr[ 8] - '0') < 10 &&
- static_cast<unsigned char>(itr[ 9] - '0') < 10 &&
- static_cast<unsigned char>(itr[10] - '0') < 10 &&
- static_cast<unsigned char>(itr[11] - '0') < 10 &&
- static_cast<unsigned char>(itr[12] - '0') < 10;
- }
- };
- template <typename Iterator>
- struct all_digits_check_impl<Iterator,12>
- {
- static inline bool process(Iterator itr)
- {
- return static_cast<unsigned char>(itr[ 0] - '0') < 10 &&
- static_cast<unsigned char>(itr[ 1] - '0') < 10 &&
- static_cast<unsigned char>(itr[ 2] - '0') < 10 &&
- static_cast<unsigned char>(itr[ 3] - '0') < 10 &&
- static_cast<unsigned char>(itr[ 4] - '0') < 10 &&
- static_cast<unsigned char>(itr[ 5] - '0') < 10 &&
- static_cast<unsigned char>(itr[ 6] - '0') < 10 &&
- static_cast<unsigned char>(itr[ 7] - '0') < 10 &&
- static_cast<unsigned char>(itr[ 8] - '0') < 10 &&
- static_cast<unsigned char>(itr[ 9] - '0') < 10 &&
- static_cast<unsigned char>(itr[10] - '0') < 10 &&
- static_cast<unsigned char>(itr[11] - '0') < 10;
- }
- };
- template <typename Iterator>
- struct all_digits_check_impl<Iterator,11>
- {
- static inline bool process(Iterator itr)
- {
- return static_cast<unsigned char>(itr[ 0] - '0') < 10 &&
- static_cast<unsigned char>(itr[ 1] - '0') < 10 &&
- static_cast<unsigned char>(itr[ 2] - '0') < 10 &&
- static_cast<unsigned char>(itr[ 3] - '0') < 10 &&
- static_cast<unsigned char>(itr[ 4] - '0') < 10 &&
- static_cast<unsigned char>(itr[ 5] - '0') < 10 &&
- static_cast<unsigned char>(itr[ 6] - '0') < 10 &&
- static_cast<unsigned char>(itr[ 7] - '0') < 10 &&
- static_cast<unsigned char>(itr[ 8] - '0') < 10 &&
- static_cast<unsigned char>(itr[ 9] - '0') < 10 &&
- static_cast<unsigned char>(itr[10] - '0') < 10;
- }
- };
- template <typename Iterator>
- struct all_digits_check_impl<Iterator,10>
- {
- static inline bool process(Iterator itr)
- {
- return static_cast<unsigned char>(itr[0] - '0') < 10 &&
- static_cast<unsigned char>(itr[1] - '0') < 10 &&
- static_cast<unsigned char>(itr[2] - '0') < 10 &&
- static_cast<unsigned char>(itr[3] - '0') < 10 &&
- static_cast<unsigned char>(itr[4] - '0') < 10 &&
- static_cast<unsigned char>(itr[5] - '0') < 10 &&
- static_cast<unsigned char>(itr[6] - '0') < 10 &&
- static_cast<unsigned char>(itr[7] - '0') < 10 &&
- static_cast<unsigned char>(itr[8] - '0') < 10 &&
- static_cast<unsigned char>(itr[9] - '0') < 10;
- }
- };
- template <typename Iterator>
- struct all_digits_check_impl<Iterator,9>
- {
- static inline bool process(Iterator itr)
- {
- return static_cast<unsigned char>(itr[0] - '0') < 10 &&
- static_cast<unsigned char>(itr[1] - '0') < 10 &&
- static_cast<unsigned char>(itr[2] - '0') < 10 &&
- static_cast<unsigned char>(itr[3] - '0') < 10 &&
- static_cast<unsigned char>(itr[4] - '0') < 10 &&
- static_cast<unsigned char>(itr[5] - '0') < 10 &&
- static_cast<unsigned char>(itr[6] - '0') < 10 &&
- static_cast<unsigned char>(itr[7] - '0') < 10 &&
- static_cast<unsigned char>(itr[8] - '0') < 10;
- }
- };
- template <typename Iterator>
- struct all_digits_check_impl<Iterator,8>
- {
- static inline bool process(Iterator itr)
- {
- return static_cast<unsigned char>(itr[0] - '0') < 10 &&
- static_cast<unsigned char>(itr[1] - '0') < 10 &&
- static_cast<unsigned char>(itr[2] - '0') < 10 &&
- static_cast<unsigned char>(itr[3] - '0') < 10 &&
- static_cast<unsigned char>(itr[4] - '0') < 10 &&
- static_cast<unsigned char>(itr[5] - '0') < 10 &&
- static_cast<unsigned char>(itr[6] - '0') < 10 &&
- static_cast<unsigned char>(itr[7] - '0') < 10;
- }
- };
- template <typename Iterator>
- struct all_digits_check_impl<Iterator,7>
- {
- static inline bool process(Iterator itr)
- {
- return static_cast<unsigned char>(itr[0] - '0') < 10 &&
- static_cast<unsigned char>(itr[1] - '0') < 10 &&
- static_cast<unsigned char>(itr[2] - '0') < 10 &&
- static_cast<unsigned char>(itr[3] - '0') < 10 &&
- static_cast<unsigned char>(itr[4] - '0') < 10 &&
- static_cast<unsigned char>(itr[5] - '0') < 10 &&
- static_cast<unsigned char>(itr[6] - '0') < 10;
- }
- };
- template <typename Iterator>
- struct all_digits_check_impl<Iterator,6>
- {
- static inline bool process(Iterator itr)
- {
- return static_cast<unsigned char>(itr[0] - '0') < 10 &&
- static_cast<unsigned char>(itr[1] - '0') < 10 &&
- static_cast<unsigned char>(itr[2] - '0') < 10 &&
- static_cast<unsigned char>(itr[3] - '0') < 10 &&
- static_cast<unsigned char>(itr[4] - '0') < 10 &&
- static_cast<unsigned char>(itr[5] - '0') < 10;
- }
- };
- template <typename Iterator>
- struct all_digits_check_impl<Iterator,5>
- {
- static inline bool process(Iterator itr)
- {
- return static_cast<unsigned char>(itr[0] - '0') < 10 &&
- static_cast<unsigned char>(itr[1] - '0') < 10 &&
- static_cast<unsigned char>(itr[2] - '0') < 10 &&
- static_cast<unsigned char>(itr[3] - '0') < 10 &&
- static_cast<unsigned char>(itr[4] - '0') < 10;
- }
- };
- template <typename Iterator>
- struct all_digits_check_impl<Iterator,4>
- {
- static inline bool process(Iterator itr)
- {
- return static_cast<unsigned char>(itr[0] - '0') < 10 &&
- static_cast<unsigned char>(itr[1] - '0') < 10 &&
- static_cast<unsigned char>(itr[2] - '0') < 10 &&
- static_cast<unsigned char>(itr[3] - '0') < 10;
- }
- };
- template <typename Iterator>
- struct all_digits_check_impl<Iterator,3>
- {
- static inline bool process(Iterator itr)
- {
- return
- static_cast<unsigned char>(itr[0] - '0') < 10 &&
- static_cast<unsigned char>(itr[1] - '0') < 10 &&
- static_cast<unsigned char>(itr[2] - '0') < 10;
- }
- };
- template <typename Iterator>
- struct all_digits_check_impl<Iterator,2>
- {
- static inline bool process(Iterator itr)
- {
- return static_cast<unsigned char>(itr[ 0] - '0') < 10 &&
- static_cast<unsigned char>(itr[ 1] - '0') < 10;
- }
- };
- template <typename Iterator>
- struct all_digits_check_impl<Iterator,1>
- {
- static inline bool process(Iterator itr)
- {
- return static_cast<unsigned char>(itr[ 0] - '0') < 10;
- }
- };
- template <typename Iterator>
- struct all_digits_check_impl<Iterator,0>
- {
- static inline bool process(Iterator)
- {
- return false;
- }
- };
- template <typename T, typename Iterator, int N>
- struct numeric_convert_impl
- {
- static inline void process(Iterator, T&)
- { throw std::runtime_error("numeric_convert_impl::process( - unsupported value for N."); }
- };
- template <typename T, typename Iterator>
- struct numeric_convert_impl<T,Iterator,19>
- {
- static inline void process(Iterator itr, T& t)
- {
- strtk::fast::details::numeric_convert_impl<T,Iterator,18>::process(itr + 1,t);
- t += static_cast<T>((itr[0] - '0') * 1000000000000000000LL);
- }
- };
- template <typename T, typename Iterator>
- struct numeric_convert_impl<T,Iterator,18>
- {
- static inline void process(Iterator itr, T& t)
- {
- strtk::fast::details::numeric_convert_impl<T,Iterator,17>::process(itr + 1,t);
- t += static_cast<T>((itr[0] - '0') * 100000000000000000LL);
- }
- };
- template <typename T, typename Iterator>
- struct numeric_convert_impl<T,Iterator,17>
- {
- static inline void process(Iterator itr, T& t)
- {
- numeric_convert_impl<T,Iterator,16>::process(itr + 1,t);
- t += static_cast<T>((itr[0] - '0') * 10000000000000000LL);
- }
- };
- template <typename T, typename Iterator>
- struct numeric_convert_impl<T,Iterator,16>
- {
- static inline void process(Iterator itr, T& t)
- {
- T x = static_cast<T>((itr[ 0] - '0') * 1000000000000000LL);
- x += static_cast<T>((itr[ 1] - '0') * 100000000000000LL);
- x += static_cast<T>((itr[ 2] - '0') * 10000000000000LL);
- x += static_cast<T>((itr[ 3] - '0') * 1000000000000LL);
- x += static_cast<T>((itr[ 4] - '0') * 100000000000LL);
- x += static_cast<T>((itr[ 5] - '0') * 10000000000LL);
- x += static_cast<T>((itr[ 6] - '0') * 1000000000LL);
- x += static_cast<T>((itr[ 7] - '0') * 100000000LL);
- x += static_cast<T>((itr[ 8] - '0') * 10000000LL);
- x += static_cast<T>((itr[ 9] - '0') * 1000000LL);
- x += static_cast<T>((itr[10] - '0') * 100000LL);
- x += static_cast<T>((itr[11] - '0') * 10000LL);
- x += static_cast<T>((itr[12] - '0') * 1000LL);
- x += static_cast<T>((itr[13] - '0') * 100LL);
- x += static_cast<T>((itr[14] - '0') * 10LL);
- x += static_cast<T>((itr[15] - '0') );
- t = x;
- }
- };
- template <typename T, typename Iterator>
- struct numeric_convert_impl<T,Iterator,15>
- {
- static inline void process(Iterator itr, T& t)
- {
- T x = static_cast<T>((itr[ 0] - '0') * 100000000000000LL);
- x += static_cast<T>((itr[ 1] - '0') * 10000000000000LL);
- x += static_cast<T>((itr[ 2] - '0') * 1000000000000LL);
- x += static_cast<T>((itr[ 3] - '0') * 100000000000LL);
- x += static_cast<T>((itr[ 4] - '0') * 10000000000LL);
- x += static_cast<T>((itr[ 5] - '0') * 1000000000LL);
- x += static_cast<T>((itr[ 6] - '0') * 100000000LL);
- x += static_cast<T>((itr[ 7] - '0') * 10000000LL);
- x += static_cast<T>((itr[ 8] - '0') * 1000000LL);
- x += static_cast<T>((itr[ 9] - '0') * 100000LL);
- x += static_cast<T>((itr[10] - '0') * 10000LL);
- x += static_cast<T>((itr[11] - '0') * 1000LL);
- x += static_cast<T>((itr[12] - '0') * 100LL);
- x += static_cast<T>((itr[13] - '0') * 10LL);
- x += static_cast<T>((itr[14] - '0') );
- t = x;
- }
- };
- template <typename T, typename Iterator>
- struct numeric_convert_impl<T,Iterator,14>
- {
- static inline void process(Iterator itr, T& t)
- {
- T x = static_cast<T>((itr[ 0] - '0') * 10000000000000LL);
- x += static_cast<T>((itr[ 1] - '0') * 1000000000000LL);
- x += static_cast<T>((itr[ 2] - '0') * 100000000000LL);
- x += static_cast<T>((itr[ 3] - '0') * 10000000000LL);
- x += static_cast<T>((itr[ 4] - '0') * 1000000000LL);
- x += static_cast<T>((itr[ 5] - '0') * 100000000LL);
- x += static_cast<T>((itr[ 6] - '0') * 10000000LL);
- x += static_cast<T>((itr[ 7] - '0') * 1000000LL);
- x += static_cast<T>((itr[ 8] - '0') * 100000LL);
- x += static_cast<T>((itr[ 9] - '0') * 10000LL);
- x += static_cast<T>((itr[10] - '0') * 1000LL);
- x += static_cast<T>((itr[11] - '0') * 100LL);
- x += static_cast<T>((itr[12] - '0') * 10LL);
- x += static_cast<T>((itr[13] - '0') );
- t = x;
- }
- };
- template <typename T, typename Iterator>
- struct numeric_convert_impl<T,Iterator,13>
- {
- static inline void process(Iterator itr, T& t)
- {
- T x = static_cast<T>((itr[ 0] - '0') * 1000000000000LL);
- x += static_cast<T>((itr[ 1] - '0') * 100000000000LL);
- x += static_cast<T>((itr[ 2] - '0') * 10000000000LL);
- x += static_cast<T>((itr[ 3] - '0') * 1000000000LL);
- x += static_cast<T>((itr[ 4] - '0') * 100000000LL);
- x += static_cast<T>((itr[ 5] - '0') * 10000000LL);
- x += static_cast<T>((itr[ 6] - '0') * 1000000LL);
- x += static_cast<T>((itr[ 7] - '0') * 100000LL);
- x += static_cast<T>((itr[ 8] - '0') * 10000LL);
- x += static_cast<T>((itr[ 9] - '0') * 1000LL);
- x += static_cast<T>((itr[10] - '0') * 100LL);
- x += static_cast<T>((itr[11] - '0') * 10LL);
- x += static_cast<T>((itr[12] - '0') );
- t = x;
- }
- };
- template <typename T, typename Iterator>
- struct numeric_convert_impl<T,Iterator,12>
- {
- static inline void process(Iterator itr, T& t)
- {
- T x = static_cast<T>((itr[ 0] - '0') * 100000000000LL);
- x += static_cast<T>((itr[ 1] - '0') * 10000000000LL);
- x += static_cast<T>((itr[ 2] - '0') * 1000000000LL);
- x += static_cast<T>((itr[ 3] - '0') * 100000000LL);
- x += static_cast<T>((itr[ 4] - '0') * 10000000LL);
- x += static_cast<T>((itr[ 5] - '0') * 1000000LL);
- x += static_cast<T>((itr[ 6] - '0') * 100000LL);
- x += static_cast<T>((itr[ 7] - '0') * 10000LL);
- x += static_cast<T>((itr[ 8] - '0') * 1000LL);
- x += static_cast<T>((itr[ 9] - '0') * 100LL);
- x += static_cast<T>((itr[10] - '0') * 10LL);
- x += static_cast<T>((itr[11] - '0') );
- t = x;
- }
- };
- template <typename T, typename Iterator>
- struct numeric_convert_impl<T,Iterator,11>
- {
- static inline void process(Iterator itr, T& t)
- {
- T x = static_cast<T>((itr[ 0] - '0') * 10000000000LL);
- x += static_cast<T>((itr[ 1] - '0') * 1000000000LL);
- x += static_cast<T>((itr[ 2] - '0') * 100000000LL);
- x += static_cast<T>((itr[ 3] - '0') * 10000000LL);
- x += static_cast<T>((itr[ 4] - '0') * 1000000LL);
- x += static_cast<T>((itr[ 5] - '0') * 100000LL);
- x += static_cast<T>((itr[ 6] - '0') * 10000LL);
- x += static_cast<T>((itr[ 7] - '0') * 1000LL);
- x += static_cast<T>((itr[ 8] - '0') * 100LL);
- x += static_cast<T>((itr[ 9] - '0') * 10LL);
- x += static_cast<T>((itr[10] - '0') );
- t = x;
- }
- };
- template <typename T, typename Iterator>
- struct numeric_convert_impl<T,Iterator,10>
- {
- static inline void process(Iterator itr, T& t)
- {
- T x = static_cast<T>((itr[0] - '0') * 1000000000);
- x += static_cast<T>((itr[1] - '0') * 100000000);
- x += static_cast<T>((itr[2] - '0') * 10000000);
- x += static_cast<T>((itr[3] - '0') * 1000000);
- x += static_cast<T>((itr[4] - '0') * 100000);
- x += static_cast<T>((itr[5] - '0') * 10000);
- x += static_cast<T>((itr[6] - '0') * 1000);
- x += static_cast<T>((itr[7] - '0') * 100);
- x += static_cast<T>((itr[8] - '0') * 10);
- x += static_cast<T>((itr[9] - '0') );
- t = x;
- }
- };
- template <typename T, typename Iterator>
- struct numeric_convert_impl<T,Iterator,9>
- {
- static inline void process(Iterator itr, T& t)
- {
- T x = static_cast<T>((itr[0] - '0') * 100000000);
- x += static_cast<T>((itr[1] - '0') * 10000000);
- x += static_cast<T>((itr[2] - '0') * 1000000);
- x += static_cast<T>((itr[3] - '0') * 100000);
- x += static_cast<T>((itr[4] - '0') * 10000);
- x += static_cast<T>((itr[5] - '0') * 1000);
- x += static_cast<T>((itr[6] - '0') * 100);
- x += static_cast<T>((itr[7] - '0') * 10);
- x += static_cast<T>((itr[8] - '0') );
- t = x;
- }
- };
- template <typename T, typename Iterator>
- struct numeric_convert_impl<T,Iterator,8>
- {
- static inline void process(Iterator itr, T& t)
- {
- T x = static_cast<T>((itr[0] - '0') * 10000000);
- x += static_cast<T>((itr[1] - '0') * 1000000);
- x += static_cast<T>((itr[2] - '0') * 100000);
- x += static_cast<T>((itr[3] - '0') * 10000);
- x += static_cast<T>((itr[4] - '0') * 1000);
- x += static_cast<T>((itr[5] - '0') * 100);
- x += static_cast<T>((itr[6] - '0') * 10);
- x += static_cast<T>((itr[7] - '0') );
- t = x;
- }
- };
- template <typename T, typename Iterator>
- struct numeric_convert_impl<T,Iterator,7>
- {
- static inline void process(Iterator itr, T& t)
- {
- T x = static_cast<T>((itr[0] - '0') * 1000000);
- x += static_cast<T>((itr[1] - '0') * 100000);
- x += static_cast<T>((itr[2] - '0') * 10000);
- x += static_cast<T>((itr[3] - '0') * 1000);
- x += static_cast<T>((itr[4] - '0') * 100);
- x += static_cast<T>((itr[5] - '0') * 10);
- x += static_cast<T>((itr[6] - '0') );
- t = x;
- }
- };
- template <typename T, typename Iterator>
- struct numeric_convert_impl<T,Iterator,6>
- {
- static inline void process(Iterator itr, T& t)
- {
- T x = static_cast<T>((itr[0] - '0') * 100000);
- x += static_cast<T>((itr[1] - '0') * 10000);
- x += static_cast<T>((itr[2] - '0') * 1000);
- x += static_cast<T>((itr[3] - '0') * 100);
- x += static_cast<T>((itr[4] - '0') * 10);
- x += static_cast<T>((itr[5] - '0') );
- t = x;
- }
- };
- template <typename T, typename Iterator>
- struct numeric_convert_impl<T,Iterator,5>
- {
- static inline void process(Iterator itr, T& t)
- {
- T x = static_cast<T>((itr[0] - '0') * 10000);
- x += static_cast<T>((itr[1] - '0') * 1000);
- x += static_cast<T>((itr[2] - '0') * 100);
- x += static_cast<T>((itr[3] - '0') * 10);
- x += static_cast<T>((itr[4] - '0') );
- t = x;
- }
- };
- template <typename T, typename Iterator>
- struct numeric_convert_impl<T,Iterator,4>
- {
- static inline void process(Iterator itr, T& t)
- {
- T x = static_cast<T>((itr[0] - '0') * 1000);
- x += static_cast<T>((itr[1] - '0') * 100);
- x += static_cast<T>((itr[2] - '0') * 10);
- x += static_cast<T>((itr[3] - '0') );
- t = x;
- }
- };
- template <typename T, typename Iterator>
- struct numeric_convert_impl<T,Iterator,3>
- {
- static inline void process(Iterator itr, T& t)
- {
- T x = static_cast<T>((itr[0] - '0') * 100);
- x += static_cast<T>((itr[1] - '0') * 10);
- x += static_cast<T>((itr[2] - '0') );
- t = x;
- }
- };
- template <typename T, typename Iterator>
- struct numeric_convert_impl<T,Iterator,2>
- {
- static inline void process(Iterator itr, T& t)
- {
- T x = static_cast<T>((itr[0] - '0') * 10);
- x += static_cast<T>((itr[1] - '0') );
- t = x;
- }
- };
- template <typename T, typename Iterator>
- struct numeric_convert_impl<T,Iterator,1>
- {
- static inline void process(Iterator itr, T& t)
- {
- t = static_cast<T>((itr[0] - '0'));
- }
- };
- template <typename T, typename Iterator>
- struct numeric_convert_impl<T,Iterator,0>
- {
- static inline void process(Iterator, T& t)
- {
- t = 0;
- }
- };
- template <typename T, typename NoneSignedTag>
- inline bool negate(T&, NoneSignedTag)
- {
- return false;
- }
- template <typename T>
- inline bool negate(T& t, strtk::details::signed_type_tag)
- {
- t = -t;
- return true;
- }
- } // namespace details
- template <int N, typename Iterator>
- inline bool all_digits_check(Iterator itr)
- {
- typedef typename strtk::details::is_valid_iterator<Iterator>::type itr_type;
- return details::all_digits_check_impl<Iterator,N>::process(itr);
- }
- template <int N, typename Iterator>
- inline bool all_digits_check(const std::string& s)
- {
- return all_digits_check<N,const char*>(s.data());
- }
- template <typename Iterator>
- inline bool all_digits_check(const std::size_t& n, Iterator itr)
- {
- switch (n)
- {
- case 0 : return details::all_digits_check_impl<Iterator, 0>::process(itr);
- case 1 : return details::all_digits_check_impl<Iterator, 1>::process(itr);
- case 2 : return details::all_digits_check_impl<Iterator, 2>::process(itr);
- case 3 : return details::all_digits_check_impl<Iterator, 3>::process(itr);
- case 4 : return details::all_digits_check_impl<Iterator, 4>::process(itr);
- case 5 : return details::all_digits_check_impl<Iterator, 5>::process(itr);
- case 6 : return details::all_digits_check_impl<Iterator, 6>::process(itr);
- case 7 : return details::all_digits_check_impl<Iterator, 7>::process(itr);
- case 8 : return details::all_digits_check_impl<Iterator, 8>::process(itr);
- case 9 : return details::all_digits_check_impl<Iterator, 9>::process(itr);
- case 10 : return details::all_digits_check_impl<Iterator,10>::process(itr);
- case 11 : return details::all_digits_check_impl<Iterator,11>::process(itr);
- case 12 : return details::all_digits_check_impl<Iterator,12>::process(itr);
- case 13 : return details::all_digits_check_impl<Iterator,13>::process(itr);
- case 14 : return details::all_digits_check_impl<Iterator,14>::process(itr);
- case 15 : return details::all_digits_check_impl<Iterator,15>::process(itr);
- case 16 : return details::all_digits_check_impl<Iterator,16>::process(itr);
- case 17 : return details::all_digits_check_impl<Iterator,17>::process(itr);
- case 18 : return details::all_digits_check_impl<Iterator,18>::process(itr);
- case 19 : return details::all_digits_check_impl<Iterator,19>::process(itr);
- default : return false;
- }
- }
- template <typename Iterator>
- inline bool all_digits_check(Iterator begin, Iterator end)
- {
- return all_digits_check(std::distance(begin,end),begin);
- }
- inline bool all_digits_check(const std::string& s)
- {
- return all_digits_check(s.size(),s.data());
- }
- template <int N, typename Iterator>
- inline bool signed_all_digits_check(Iterator itr)
- {
- if (('-' == (*itr)) || ('+' == (*itr)))
- return all_digits_check<Iterator,N - 1>((itr + 1));
- else
- return all_digits_check<Iterator,N>(itr);
- }
- template <typename Iterator>
- inline bool signed_all_digits_check(const std::size_t& n, Iterator itr)
- {
- if (('-' == (*itr)) || ('+' == (*itr)))
- return all_digits_check(n - 1,(itr + 1));
- else
- return all_digits_check(n,itr);
- }
- template <int N>
- inline bool signed_all_digits_check(const std::string& s)
- {
- return signed_all_digits_check<N,const char*>(s.data());
- }
- template <typename Iterator>
- inline bool signed_all_digits_check(Iterator begin, Iterator end)
- {
- return signed_all_digits_check(std::distance(begin,end),begin);
- }
- inline bool signed_all_digits_check(const std::string& s)
- {
- return signed_all_digits_check(s.size(),s.data());
- }
- template <int N, typename T, typename Iterator>
- inline void numeric_convert(Iterator itr, T& t, const bool digit_check = false)
- {
- typedef typename strtk::details::is_valid_iterator<Iterator>::type itr_type;
- if (digit_check)
- {
- if (!all_digits_check<N,Iterator>(itr))
- {
- t = 0;
- return;
- }
- }
- details::numeric_convert_impl<T,Iterator,N>::process(itr,t);
- }
- template <int N, typename T>
- inline void numeric_convert(const std::string& s, T& t, const bool digit_check = false)
- {
- numeric_convert<N,T,const char*>(s.data(),t,digit_check);
- }
- template <typename T, typename Iterator>
- inline bool numeric_convert(const std::size_t& n,
- Iterator itr, T& t,
- const bool digit_check = false)
- {
- if (digit_check)
- {
- if (!all_digits_check(n,itr))
- {
- return false;
- }
- }
- switch (n)
- {
- case 0 : details::numeric_convert_impl<T,Iterator, 0>::process(itr,t); return true;
- case 1 : details::numeric_convert_impl<T,Iterator, 1>::process(itr,t); return true;
- case 2 : details::numeric_convert_impl<T,Iterator, 2>::process(itr,t); return true;
- case 3 : details::numeric_convert_impl<T,Iterator, 3>::process(itr,t); return true;
- case 4 : details::numeric_convert_impl<T,Iterator, 4>::process(itr,t); return true;
- case 5 : details::numeric_convert_impl<T,Iterator, 5>::process(itr,t); return true;
- case 6 : details::numeric_convert_impl<T,Iterator, 6>::process(itr,t); return true;
- case 7 : details::numeric_convert_impl<T,Iterator, 7>::process(itr,t); return true;
- case 8 : details::numeric_convert_impl<T,Iterator, 8>::process(itr,t); return true;
- case 9 : details::numeric_convert_impl<T,Iterator, 9>::process(itr,t); return true;
- case 10 : details::numeric_convert_impl<T,Iterator,10>::process(itr,t); return true;
- case 11 : details::numeric_convert_impl<T,Iterator,11>::process(itr,t); return true;
- case 12 : details::numeric_convert_impl<T,Iterator,12>::process(itr,t); return true;
- case 13 : details::numeric_convert_impl<T,Iterator,13>::process(itr,t); return true;
- case 14 : details::numeric_convert_impl<T,Iterator,14>::process(itr,t); return true;
- case 15 : details::numeric_convert_impl<T,Iterator,15>::process(itr,t); return true;
- case 16 : details::numeric_convert_impl<T,Iterator,16>::process(itr,t); return true;
- case 17 : details::numeric_convert_impl<T,Iterator,17>::process(itr,t); return true;
- case 18 : details::numeric_convert_impl<T,Iterator,18>::process(itr,t); return true;
- case 19 : details::numeric_convert_impl<T,Iterator,19>::process(itr,t); return true;
- default : return false;
- }
- }
- template <typename T>
- inline void numeric_convert(const std::string& s, T& t, const bool digit_check = false)
- {
- numeric_convert(s.size(),s.data(),t,digit_check);
- }
- template <int N, typename T, typename Iterator>
- inline void signed_numeric_convert(Iterator itr, T& t, const bool digit_check = false)
- {
- if ('-' == (*itr))
- {
- numeric_convert<N - 1,T,Iterator>((itr + 1),t,digit_check);
- typename strtk::details::supported_conversion_to_type<T>::type type;
- details::negate(t,type);
- }
- else if ('+' == (*itr))
- {
- numeric_convert<N - 1,T,Iterator>((itr + 1),t,digit_check);
- }
- else
- numeric_convert<N,T,Iterator>(itr,t,digit_check);
- }
- template <typename T, typename Iterator>
- inline bool signed_numeric_convert(const std::size_t& n,
- Iterator itr,
- T& t,
- const bool digit_check = false)
- {
- if ('-' == (*itr))
- {
- bool result = numeric_convert((n - 1),(itr + 1),t,digit_check);
- typename strtk::details::supported_conversion_to_type<T>::type type;
- return details::negate<T>(t,type) && result;
- }
- else if ('+' == (*itr))
- {
- return numeric_convert((n - 1),(itr + 1),t,digit_check);
- }
- else
- return numeric_convert(n,itr,t,digit_check);
- }
- template <int N, typename T>
- inline void signed_numeric_convert(const std::string& s,
- T& t,
- const bool digit_check = false)
- {
- signed_numeric_convert<N,T,const char*>(s.data(),t,digit_check);
- }
- template <typename T>
- inline bool signed_numeric_convert(const std::string& s,
- T& t,
- const bool digit_check = false)
- {
- return signed_numeric_convert<T,const char*>(s.size(),s.data(),t,digit_check);
- }
- } // namespace fast
- namespace binary
- {
- namespace details
- {
- namespace details_endian
- {
- #if (defined(__LITTLE_ENDIAN__)) ||\
- (defined(WIN32)) ||\
- (defined(__MINGW32_VERSION)) ||\
- (defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__))
- static const bool __le_result = true;
- static const bool __be_result = false;
- #else
- static const bool __le_result = false;
- static const bool __be_result = true;
- #endif
- }
- static inline bool is_little_endian()
- {
- //Is the current architecture/platform little-endian?
- return details_endian::__le_result;
- }
- static inline bool is_big_endian()
- {
- return details_endian::__be_result;
- }
- static inline unsigned short convert(const unsigned short v)
- {
- //static_assert(2 == sizeof(v),"");
- return ((v >> 8) & 0x00FF) | ((v << 8) & 0xFFFF);
- }
- static inline unsigned int convert(const unsigned int v)
- {
- //static_assert(4 == sizeof(v),"");
- return ((v >> 24) & 0x000000FF) | ((v << 24) & 0x0000FF00) |
- ((v << 8) & 0x00FF0000) | ((v >> 8) & 0xFF000000);
- }
- static inline unsigned long long int convert(const unsigned long long int v)
- {
- //static_assert(8 == sizeof(v),"");
- return ((v >> 56) & 0x00000000000000FFLL) | ((v << 56) & 0xFF00000000000000LL) |
- ((v >> 40) & 0x000000000000FF00LL) | ((v << 40) & 0x00FF000000000000LL) |
- ((v >> 24) & 0x0000000000FF0000LL) | ((v << 24) & 0x0000FF0000000000LL) |
- ((v >> 8) & 0x00000000FF000000LL) | ((v << 8) & 0x000000FF00000000LL) ;
- }
- static inline short convert(const short v)
- {
- return static_cast<short>(convert(static_cast<unsigned short>(v)));
- }
- static inline int convert(const int v)
- {
- return static_cast<int>(convert(static_cast<unsigned int>(v)));
- }
- static inline unsigned long long int convert(const long long int v)
- {
- return static_cast<long long>(convert(static_cast<unsigned long long int>(v)));
- }
- static inline unsigned short convert_to_be(const unsigned short v)
- {
- if (is_little_endian()) convert(v);
- return v;
- }
- static inline unsigned int convert_to_be(const unsigned int v)
- {
- if (is_little_endian()) convert(v);
- return v;
- }
- static inline unsigned long long int convert_to_be(const unsigned long long int v)
- {
- if (is_little_endian()) convert(v);
- return v;
- }
- static inline short convert_to_be(const short v)
- {
- if (is_little_endian()) convert(v);
- return v;
- }
- static inline int convert_to_be(const int v)
- {
- if (is_little_endian()) convert(v);
- return v;
- }
- static inline unsigned long long int convert_to_be(const long long int v)
- {
- if (is_little_endian()) convert(v);
- return v;
- }
- static inline unsigned short convert_to_le(const unsigned short v)
- {
- if (is_big_endian()) convert(v);
- return v;
- }
- static inline unsigned int convert_to_le(const unsigned int v)
- {
- if (is_big_endian()) convert(v);
- return v;
- }
- static inline unsigned long long int convert_to_le(const unsigned long long int v)
- {
- if (is_big_endian()) convert(v);
- return v;
- }
- static inline short convert_to_le(const short v)
- {
- if (is_big_endian()) convert(v);
- return v;
- }
- static inline int convert_to_le(const int v)
- {
- if (is_big_endian()) convert(v);
- return v;
- }
- static inline unsigned long long int convert_to_le(const long long int v)
- {
- if (is_big_endian()) convert(v);
- return v;
- }
- class marker
- {
- private:
- typedef std::pair<std::size_t,char*> mark_type;
- public:
- inline bool reset(std::size_t& v1, char*& v2)
- {
- if (stack_.empty())
- return false;
- v1 = stack_.top().first;
- v2 = stack_.top().second;
- stack_.pop();
- return true;
- }
- inline void mark(const std::size_t& v1,char* v2)
- {
- stack_.push(std::make_pair(v1,v2));
- }
- private:
- std::stack<mark_type> stack_;
- };
- }
- class reader
- {
- public:
- // should be sourced from cstdint
- typedef unsigned int uint32_t;
- typedef unsigned short uint16_t;
- typedef unsigned char uint8_t;
- typedef unsigned long long int uint64_t;
- template <typename T>
- reader(T* buffer,
- const std::size_t& buffer_length)
- : original_buffer_(reinterpret_cast<char*>(buffer)),
- buffer_(reinterpret_cast<char*>(buffer)),
- buffer_length_(buffer_length * sizeof(T)),
- amount_read_sofar_(0)
- {}
- inline bool operator!() const
- {
- return (0 == buffer_length_) ||
- (0 == original_buffer_) ||
- (0 == buffer_);
- }
- inline void reset(const bool clear_buffer = false)
- {
- amount_read_sofar_ = 0;
- buffer_ = original_buffer_;
- if (clear_buffer)
- clear();
- }
- inline std::size_t position() const
- {
- return amount_read_sofar_;
- }
- inline const char* position_ptr() const
- {
- return buffer_ ;
- }
- inline std::size_t amount_read()
- {
- return amount_read_sofar_;
- }
- inline bool rewind(const std::size_t& n_bytes)
- {
- if (n_bytes <= amount_read_sofar_)
- {
- amount_read_sofar_ -= n_bytes;
- buffer_ -= n_bytes;
- return true;
- }
- else
- return false;
- }
- inline bool seek(const int& n_bytes)
- {
- if (n_bytes < 0)
- return rewind(-n_bytes);
- else if (n_bytes > 0)
- {
- if ((amount_read_sofar_ + n_bytes) <= buffer_length_)
- {
- amount_read_sofar_ += n_bytes;
- buffer_ += n_bytes;
- return true;
- }
- else
- return false;
- }
- else
- return true;
- }
- inline void clear()
- {
- reset();
- std::memset(buffer_,0x00,buffer_length_);
- }
- template <typename T>
- inline bool operator()(T*& data, uint32_t& length, const bool read_length = true)
- {
- if (read_length && !operator()(length))
- return false;
- const std::size_t raw_size = length * sizeof(T);
- if (!buffer_capacity_ok(raw_size))
- return false;
- if (read_length)
- {
- data = new T[length];
- }
- std::copy(buffer_, buffer_ + raw_size, reinterpret_cast<char*>(data));
- buffer_ += raw_size;
- amount_read_sofar_ += raw_size;
- return true;
- }
- template <typename T>
- inline bool operator()(T*& data, uint64_t& length, const bool read_length = true)
- {
- uint32_t l = 0;
- if (read_length)
- l = static_cast<uint32_t>(length);
- if (!operator()(data,l,read_length))
- return false;
- if (read_length)
- length = l;
- return true;
- }
- inline bool operator()(std::string& output)
- {
- uint32_t length = 0;
- if (!operator()(length))
- return false;
- if (!buffer_capacity_ok(length))
- return false;
- output.resize(length);
- std::copy(buffer_,
- buffer_ + length,
- const_cast<char*>(output.data()));
- buffer_ += length;
- amount_read_sofar_ += length;
- return true;
- }
- template <typename T1, typename T2>
- inline bool operator()(std::pair<T1,T2>& p)
- {
- if (!operator()(p.first))
- return false;
- if (!operator()(p.second))
- return false;
- return true;
- }
- template <typename T,
- typename Allocator,
- template <typename,typename> class Sequence>
- inline bool operator()(Sequence<T,Allocator>& seq)
- {
- uint32_t size = 0;
- if (!read_pod(size))
- return false;
- const std::size_t raw_size = size * sizeof(T);
- if (!buffer_capacity_ok(raw_size))
- return false;
- T t = T();
- for (std::size_t i = 0; i < size; ++i)
- {
- if (operator()(t))
- seq.push_back(t);
- else
- return false;
- }
- return true;
- }
- template <typename T, typename Allocator>
- inline bool operator()(std::vector<T,Allocator>& vec)
- {
- uint32_t size = 0;
- if (!read_pod(size))
- return false;
- const std::size_t raw_size = size * sizeof(T);
- if (!buffer_capacity_ok(raw_size))
- return false;
- vec.resize(size);
- return selector<T>::type::batch_vector_read(*this,size,vec,false);
- }
- template <typename T,
- typename Comparator,
- typename Allocator>
- inline bool operator()(std::set<T,Comparator,Allocator>& set)
- {
- uint32_t size = 0;
- if (!read_pod(size))
- return false;
- const std::size_t raw_size = size * sizeof(T);
- if (!buffer_capacity_ok(raw_size))
- return false;
- T t;
- for (std::size_t i = 0; i < size; ++i)
- {
- if (!operator()(t))
- return false;
- set.insert(t);
- }
- return true;
- }
- template <typename T,
- typename Allocator,
- typename Comparator>
- inline bool operator()(std::multiset<T,Allocator,Comparator>& multiset)
- {
- uint32_t size = 0;
- if (!read_pod(size))
- return false;
- const std::size_t raw_size = size * sizeof(T);
- if (!buffer_capacity_ok(raw_size))
- return false;
- T t;
- for (std::size_t i = 0; i < size; ++i)
- {
- if (!operator()(t))
- return false;
- multiset.insert(t);
- }
- return true;
- }
- inline bool operator()(std::ifstream& stream, const std::size_t& length)
- {
- if (length > buffer_length_) return false;
- stream.read(original_buffer_,static_cast<std::streamsize>(length));
- return true;
- }
- inline bool operator()(std::ifstream& stream)
- {
- if (0 == amount_read_sofar_) return false;
- stream.read(original_buffer_,static_cast<std::streamsize>(amount_read_sofar_));
- return true;
- }
- template <typename T>
- inline bool operator()(T& output)
- {
- return selector<T>::type::run(*this,output);
- }
- template <typename T>
- inline bool operator()(const T& output)
- {
- return selector<T>::type::run(*this,const_cast<T&>(output));
- }
- template <typename T>
- inline bool be_to_native(T& output)
- {
- //From big-endian to native
- if (details::is_little_endian())
- {
- if (!operator()<T>(output)) return false;
- output = details::convert(output);
- return true;
- }
- else
- return operator()(output);
- }
- template <typename T>
- inline bool le_to_native(T& output)
- {
- //From little-endian to native
- if (details::is_little_endian())
- return operator()(output);
- else
- {
- if (!operator()<T>(output)) return false;
- output = details::convert(output);
- return true;
- }
- }
- template <typename T, std::size_t N>
- inline bool operator()(T (&output)[N])
- {
- const std::size_t raw_size = N * sizeof(T);
- if (buffer_capacity_ok(raw_size))
- {
- std::copy(buffer_,
- buffer_ + raw_size,
- reinterpret_cast<char*>(output));
- buffer_ += raw_size;
- amount_read_sofar_ += raw_size;
- return true;
- }
- else
- return false;
- }
- template <typename T>
- inline bool operator()(T& output, const std::size_t& size)
- {
- if (buffer_capacity_ok(size))
- {
- bool result = strtk::string_to_type_converter<char*,T>(buffer_,buffer_ + size,output);
- buffer_ += size;
- amount_read_sofar_ += size;
- return result;
- }
- else
- return false;
- }
- inline void mark()
- {
- marker_.mark(amount_read_sofar_,buffer_);
- }
- inline bool reset_to_mark()
- {
- return marker_.reset(amount_read_sofar_,buffer_);
- }
- private:
- reader();
- reader(const reader& s);
- reader& operator=(const reader& s);
- inline bool buffer_capacity_ok(const std::size_t& required_read_qty)
- {
- return ((required_read_qty + amount_read_sofar_) <= buffer_length_);
- }
- template <typename Type>
- struct selector
- {
- private:
- template <typename T, typename IsPOD>
- struct selector_impl
- {
- template <typename Reader>
- static inline bool run(Reader& r, T& t)
- {
- return t(r);
- }
- template <typename Reader,
- typename Allocator>
- static inline bool batch_vector_read(Reader& r,
- const std::size_t& size,
- std::vector<T,Allocator>& v,
- const bool)
- {
- T t;
- for (std::size_t i = 0; i < size; ++i)
- {
- if (r.operator()(t))
- v[i] = t;
- else
- return false;
- }
- return true;
- }
- };
- template <typename T>
- struct selector_impl<T,strtk::details::yes_t>
- {
- template <typename Reader>
- static inline bool run(Reader& r,
- T& t,
- const bool perform_buffer_capacity_check = true)
- {
- return r.read_pod(t,perform_buffer_capacity_check);
- }
- template <typename Reader,
- typename Allocator>
- static inline bool batch_vector_read(Reader& r,
- const std::size_t& size,
- std::vector<T,Allocator>& v,
- const bool)
- {
- const std::size_t raw_size = sizeof(T) * size;
- char* ptr = const_cast<char*>(reinterpret_cast<const char*>(&v[0]));
- std::copy(r.buffer_, r.buffer_ + raw_size, ptr);
- r.buffer_ += raw_size;
- r.amount_read_sofar_ += raw_size;
- return true;
- }
- };
- public:
- typedef selector_impl<Type,typename strtk::details::is_pod<Type>::result_t> type;
- };
- template <typename T>
- inline bool read_pod(T& data, const bool perform_buffer_capacity_check = true)
- {
- static const std::size_t data_length = sizeof(T);
- if (perform_buffer_capacity_check)
- {
- if (!buffer_capacity_ok(data_length))
- return false;
- }
- data = (*reinterpret_cast<T*>(buffer_));
- buffer_ += data_length;
- amount_read_sofar_ += data_length;
- return true;
- }
- char* const original_buffer_;
- char* buffer_;
- std::size_t buffer_length_;
- std::size_t amount_read_sofar_;
- details::marker marker_;
- };
- class writer
- {
- public:
- // should be sourced from cstdint
- // should be sourced from cstdint
- typedef unsigned int uint32_t;
- typedef unsigned short uint16_t;
- typedef unsigned char uint8_t;
- typedef unsigned long long int uint64_t;
- template <typename T>
- writer(T* buffer, const std::size_t& buffer_length)
- : original_buffer_(reinterpret_cast<char*>(buffer)),
- buffer_(reinterpret_cast<char*>(buffer)),
- buffer_length_(buffer_length * sizeof(T)),
- amount_written_sofar_(0)
- {}
- inline bool operator!() const
- {
- return (0 == buffer_length_) ||
- (0 == original_buffer_) ||
- (0 == buffer_);
- }
- inline void reset(const bool clear_buffer = false)
- {
- amount_written_sofar_ = 0;
- buffer_ = original_buffer_;
- if (clear_buffer)
- clear();
- }
- inline std::size_t position() const
- {
- return amount_written_sofar_;
- }
- inline const char* position_ptr() const
- {
- return buffer_ ;
- }
- inline std::size_t amount_written() const
- {
- return amount_written_sofar_;
- }
- inline void clear()
- {
- reset();
- std::memset(buffer_,0x00,buffer_length_);
- }
- template <typename T, std::size_t N>
- inline bool operator()(const T (&data)[N], const bool write_length = false)
- {
- if (write_length && !operator()(N))
- return false;
- const std::size_t raw_size = N * sizeof(T);
- if (!buffer_capacity_ok(raw_size))
- return false;
- const char* ptr = reinterpret_cast<const char*>(data);
- std::copy(ptr, ptr + raw_size, buffer_);
- buffer_ += raw_size;
- amount_written_sofar_ += raw_size;
- return true;
- }
- template <typename T>
- inline bool operator()(const T* data, const uint32_t& length, const bool write_length = true)
- {
- if (write_length && !operator()(length))
- return false;
- const std::size_t raw_size = length * sizeof(T);
- if (!buffer_capacity_ok(raw_size))
- return false;
- const char* ptr = reinterpret_cast<const char*>(data);
- std::copy(ptr, ptr + raw_size, buffer_);
- buffer_ += raw_size;
- amount_written_sofar_ += raw_size;
- return true;
- }
- template <typename T>
- inline bool operator()(const T* data, const uint64_t& length, const bool write_length = true)
- {
- return operator()(data,static_cast<uint32_t>(length),write_length);
- }
- template <typename T>
- inline bool operator()(const T* data, const uint16_t& length, const bool write_length = true)
- {
- return operator()(data,static_cast<uint32_t>(length),write_length);
- }
- template <typename T>
- inline bool operator()(const T* data, const uint8_t& length, const bool write_length = true)
- {
- return operator()(data,static_cast<uint32_t>(length),write_length);
- }
- template <typename T1, typename T2>
- inline bool operator()(const std::pair<T1,T2>& p)
- {
- if (!operator()(p.first))
- return false;
- if (!operator()(p.second))
- return false;
- return true;
- }
- inline bool operator()(const std::string& input)
- {
- return operator()<const char>(input.data(),static_cast<uint32_t>(input.size()));
- }
- template <typename T,
- typename Allocator,
- template <typename,typename> class Sequence>
- inline bool operator()(const Sequence<T,Allocator>& seq)
- {
- const uint32_t size = static_cast<uint32_t>(seq.size());
- if (!operator()(size))
- return false;
- typename Sequence<T,Allocator>::