| 22 | { |
| 23 | public: |
| 24 | class Freqs |
| 25 | { |
| 26 | public: |
| 27 | using Table = std::map<uint32_t, uint32_t>; |
| 28 | |
| 29 | Freqs() = default; |
| 30 | |
| 31 | template <typename... Args> |
| 32 | Freqs(Args const &... args) |
| 33 | { |
| 34 | Add(args...); |
| 35 | } |
| 36 | |
| 37 | void Add(strings::UniString const & s) { Add(s.begin(), s.end()); } |
| 38 | |
| 39 | void Add(std::string const & s) { Add(s.begin(), s.end()); } |
| 40 | |
| 41 | template <typename T> |
| 42 | void Add(T const * begin, T const * const end) |
| 43 | { |
| 44 | static_assert(std::is_integral<T>::value, ""); |
| 45 | AddImpl(begin, end); |
| 46 | } |
| 47 | |
| 48 | template <typename It> |
| 49 | void Add(It begin, It const end) |
| 50 | { |
| 51 | static_assert(std::is_integral<typename It::value_type>::value, ""); |
| 52 | AddImpl(begin, end); |
| 53 | } |
| 54 | |
| 55 | template <typename T> |
| 56 | void Add(std::vector<T> const & v) |
| 57 | { |
| 58 | for (auto const & e : v) |
| 59 | Add(std::begin(e), std::end(e)); |
| 60 | } |
| 61 | |
| 62 | Table const & GetTable() const { return m_table; } |
| 63 | |
| 64 | private: |
| 65 | template <typename It> |
| 66 | void AddImpl(It begin, It const end) |
| 67 | { |
| 68 | static_assert(sizeof(*begin) <= 4, ""); |
| 69 | for (; begin != end; ++begin) |
| 70 | ++m_table[static_cast<uint32_t>(*begin)]; |
| 71 | } |
| 72 | |
| 73 | Table m_table; |
| 74 | }; |
| 75 | |
| 76 | // A Code encodes a path to a leaf. It is read starting from |
| 77 | // the least significant bit. |