| 1 | // charset.hpp |
| 2 | // Copyright (c) 2005-2015 Ben Hanson (http://www.benhanson.net/) |
| 3 | // |
| 4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 5 | // file licence_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | |
| 7 | #ifndef LEXERTL_CHARSET_HPP |
| 8 | #define LEXERTL_CHARSET_HPP |
| 9 | |
| 10 | #include <algorithm> |
| 11 | #include <iterator> |
| 12 | #include <set> |
| 13 | #include "../size_t.hpp" |
| 14 | #include "../string_token.hpp" |
| 15 | |
| 16 | namespace lexertl |
| 17 | { |
| 18 | namespace detail |
| 19 | { |
| 20 | template<typename char_type, typename id_type> |
| 21 | struct basic_charset |
| 22 | { |
| 23 | typedef basic_string_token<char_type> token; |
| 24 | typedef std::set<id_type> index_set; |
| 25 | |
| 26 | token _token; |
| 27 | index_set _index_set; |
| 28 | |
| 29 | basic_charset() : |
| 30 | _token(), |
| 31 | _index_set() |
| 32 | { |
| 33 | } |
| 34 | |
| 35 | basic_charset(const token &token_, const std::size_t index_) : |
| 36 | _token(token_), |
| 37 | _index_set() |
| 38 | { |
| 39 | _index_set.insert(index_); |
| 40 | } |
| 41 | |
| 42 | bool empty() const |
| 43 | { |
| 44 | return _token.empty() && _index_set.empty(); |
| 45 | } |
| 46 | |
| 47 | void intersect(basic_charset &rhs_, basic_charset &overlap_) |
| 48 | { |
| 49 | _token.intersect(rhs_._token, overlap_._token); |
| 50 | |
| 51 | if (!overlap_._token.empty()) |
| 52 | { |
| 53 | std::merge(_index_set.begin(), _index_set.end(), |
| 54 | rhs_._index_set.begin(), rhs_._index_set.end(), |
| 55 | std::inserter(overlap_._index_set, |
| 56 | overlap_._index_set.end())); |
| 57 | |
| 58 | if (_token.empty()) |
| 59 | { |
| 60 | _index_set.clear(); |
| 61 | } |
| 62 | |
| 63 | if (rhs_._token.empty()) |
| 64 | { |
| 65 | rhs_._index_set.clear(); |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | }; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | #endif |