Tifa's CP Library

:heavy_check_mark: huffman_tree (src/code/edh/huffman_tree.hpp)

Depends on

Verified with

Code

#ifndef TIFALIBS_EDH_HUFFMAN_TREE
#define TIFALIBS_EDH_HUFFMAN_TREE

#include "../ds/radix_heap.hpp"
#include "../util/util.hpp"

namespace tifa_libs {

template <class T = u64>
class huffman {
  struct YYZ {
    T weight;
    vecu ch;
    constexpr YYZ(T weight = T{}, u32 child_cnt = 0) : weight(weight), ch(child_cnt) {}
  };
  u32 cnt_w, cnt_l, ch_sz;
  vec<YYZ> data;

  template <class Res, class Op>
  constexpr vec<Res> run(Op &&operate) const {
    vec<Res> ret(cnt_w);
    std::queue<std::pair<u32, Res>> q;
    q.emplace(data.size() - 1, Res{});
    while (!q.empty()) {
      auto [now_idx, now_code] = q.front();
      auto &ch = data[now_idx].ch;
      q.pop();
      for (u32 i = 0; i < ch_sz; ++i) {
        auto &&next_child = ch[i];
        if (next_child < cnt_l) {
          if (next_child < cnt_w) ret[next_child] = operate(now_code, i);
          continue;
        } else q.emplace(next_child, operate(now_code, i));
      }
    }
    return ret;
  }

 public:
  explicit constexpr huffman(vec<T> const &weights, u32 child_sz = 2) : cnt_w((u32)weights.size()), cnt_l(), ch_sz(child_sz), data() {
    assert(1 < child_sz && child_sz < weights.size());
    for (T now : weights) data.emplace_back(now);
    for (u32 i = 0, iend = ((ch_sz - 1) - ((cnt_w - 1) % (ch_sz - 1))) % (ch_sz - 1); i < iend; ++i) data.emplace_back();
    cnt_l = (u32)data.size();
    ds::rheap<T, u32> q;
    for (u32 i = 0; i < data.size(); ++i) q.emplace(data[i].weight, i);
    while (q.size() > 1) {
      data.emplace_back(T{}, ch_sz);
      for (u32 i = 0; i < ch_sz; ++i) {
        auto [now_weight, now_idx] = q.top();
        data.back().weight += now_weight;
        data.back().ch[i] = now_idx;
        q.pop();
      }
      q.emplace(data.back().weight, data.size() - 1);
    }
  }

  constexpr vec<strn> encode(strnv char_set = "01") const {
    assert(char_set.size() == ch_sz);
    return run<strn>([&](strn const &pre_code, u32 idx) { return pre_code + char_set[idx]; });
  }
  constexpr vecu depths() const {
    return run<u32>([](u32 const &pre_depth, u32) { return pre_depth + 1; });
  }
};

}  // namespace tifa_libs

#endif
#line 1 "src/code/edh/huffman_tree.hpp"



#line 1 "src/code/ds/radix_heap.hpp"



#line 1 "src/code/util/util.hpp"



#include <bits/stdc++.h>

template <class T>
constexpr T abs(T x) { return x < 0 ? -x : x; }

using i8 = int8_t;
using i16 = int16_t;
using i32 = int32_t;
using i64 = int64_t;
using i128 = __int128_t;
using isz = ptrdiff_t;

using u8 = uint8_t;
using u16 = uint16_t;
using u32 = uint32_t;
using u64 = uint64_t;
using u128 = __uint128_t;
using usz = size_t;

using f32 = float;
using f64 = double;
using f128 = long double;

template <class T>
using ptt = std::pair<T, T>;
template <class T>
using pt3 = std::tuple<T, T, T>;
template <class T>
using pt4 = std::tuple<T, T, T, T>;

template <class T, usz N>
using arr = std::array<T, N>;
template <class T>
using vec = std::vector<T>;
template <class T>
using vvec = vec<vec<T>>;
template <class T>
using v3ec = vec<vvec<T>>;
template <class U, class T>
using vecp = vec<std::pair<U, T>>;
template <class U, class T>
using vvecp = vvec<std::pair<U, T>>;
template <class T>
using vecpt = vec<ptt<T>>;
template <class T>
using vvecpt = vvec<ptt<T>>;

template <class T, class C = std::less<T>>
using pq = std::priority_queue<T, vec<T>, C>;
template <class T>
using pqg = std::priority_queue<T, vec<T>, std::greater<T>>;

using strn = std::string;
using strnv = std::string_view;

using vecu = vec<u32>;
using vvecu = vvec<u32>;
using v3ecu = v3ec<u32>;
using vecu64 = vec<u64>;
using vecb = vec<bool>;
using vvecb = vvec<bool>;

#ifdef ONLINE_JUDGE
#undef assert
#define assert(x) 42
#endif

using namespace std::literals;

constexpr i8 operator""_i8(unsigned long long x) { return (i8)x; }
constexpr i16 operator""_i16(unsigned long long x) { return (i16)x; }
constexpr i32 operator""_i32(unsigned long long x) { return (i32)x; }
constexpr i64 operator""_i64(unsigned long long x) { return (i64)x; }
constexpr isz operator""_iz(unsigned long long x) { return (isz)x; }

constexpr u8 operator""_u8(unsigned long long x) { return (u8)x; }
constexpr u16 operator""_u16(unsigned long long x) { return (u16)x; }
constexpr u32 operator""_u32(unsigned long long x) { return (u32)x; }
constexpr u64 operator""_u64(unsigned long long x) { return (u64)x; }
constexpr usz operator""_uz(unsigned long long x) { return (usz)x; }

inline const auto fn_0 = [](auto&&...) {};
inline const auto fn_is0 = [](auto x) { return x == 0; };


#line 5 "src/code/ds/radix_heap.hpp"

namespace tifa_libs::ds {

template <std::unsigned_integral K, class V, class C = std::less<K>>
class radix_heap {
  static constexpr u32 B = sizeof(K) * 8;
  static constexpr C comp{};
  arr<vecp<K, V>, B + 1> vs;
  arr<K, B + 1> ms;
  u32 s;
  K last;

 public:
  constexpr explicit radix_heap() : s(0), last(0) { std::ranges::fill(ms, K(-1)); }

  constexpr u32 size() const { return s; }
  constexpr bool empty() const { return !s; }

  constexpr void emplace(K key, V const &val) {
    ++s;
    K b = (K)std::bit_width(key ^ last);
    vs[b].emplace_back(key, val), ms[b] = std::min(key, ms[b], comp);
  }

  constexpr std::pair<K, V> top() {
    if (!~ms[0]) {
      u32 idx = u32(std::ranges::find_if(ms, [](auto x) { return !!~x; }) - ms.begin());
      last = ms[idx];
      for (auto &p : vs[idx]) {
        K b = (K)std::bit_width(p.first ^ last);
        vs[b].emplace_back(p), ms[b] = std::min(p.first, ms[b], comp);
      }
      vs[idx].clear(), ms[idx] = K(-1);
    }
    return vs[0].back();
  }

  constexpr void pop() {
    top(), --s, vs[0].pop_back();
    if (vs[0].empty()) ms[0] = K(-1);
  }
};

template <class K, class V>
using rheap = std::conditional_t<std::unsigned_integral<K>, ds::radix_heap<K, V>, pqg<std::pair<K, V>>>;
template <class K, class V>
using rheapg = std::conditional_t<std::unsigned_integral<K>, ds::radix_heap<K, V, std::greater<K>>, pq<std::pair<K, V>>>;

}  // namespace tifa_libs::ds


#line 6 "src/code/edh/huffman_tree.hpp"

namespace tifa_libs {

template <class T = u64>
class huffman {
  struct YYZ {
    T weight;
    vecu ch;
    constexpr YYZ(T weight = T{}, u32 child_cnt = 0) : weight(weight), ch(child_cnt) {}
  };
  u32 cnt_w, cnt_l, ch_sz;
  vec<YYZ> data;

  template <class Res, class Op>
  constexpr vec<Res> run(Op &&operate) const {
    vec<Res> ret(cnt_w);
    std::queue<std::pair<u32, Res>> q;
    q.emplace(data.size() - 1, Res{});
    while (!q.empty()) {
      auto [now_idx, now_code] = q.front();
      auto &ch = data[now_idx].ch;
      q.pop();
      for (u32 i = 0; i < ch_sz; ++i) {
        auto &&next_child = ch[i];
        if (next_child < cnt_l) {
          if (next_child < cnt_w) ret[next_child] = operate(now_code, i);
          continue;
        } else q.emplace(next_child, operate(now_code, i));
      }
    }
    return ret;
  }

 public:
  explicit constexpr huffman(vec<T> const &weights, u32 child_sz = 2) : cnt_w((u32)weights.size()), cnt_l(), ch_sz(child_sz), data() {
    assert(1 < child_sz && child_sz < weights.size());
    for (T now : weights) data.emplace_back(now);
    for (u32 i = 0, iend = ((ch_sz - 1) - ((cnt_w - 1) % (ch_sz - 1))) % (ch_sz - 1); i < iend; ++i) data.emplace_back();
    cnt_l = (u32)data.size();
    ds::rheap<T, u32> q;
    for (u32 i = 0; i < data.size(); ++i) q.emplace(data[i].weight, i);
    while (q.size() > 1) {
      data.emplace_back(T{}, ch_sz);
      for (u32 i = 0; i < ch_sz; ++i) {
        auto [now_weight, now_idx] = q.top();
        data.back().weight += now_weight;
        data.back().ch[i] = now_idx;
        q.pop();
      }
      q.emplace(data.back().weight, data.size() - 1);
    }
  }

  constexpr vec<strn> encode(strnv char_set = "01") const {
    assert(char_set.size() == ch_sz);
    return run<strn>([&](strn const &pre_code, u32 idx) { return pre_code + char_set[idx]; });
  }
  constexpr vecu depths() const {
    return run<u32>([](u32 const &pre_depth, u32) { return pre_depth + 1; });
  }
};

}  // namespace tifa_libs


Back to top page