跳到正文

剪贴板

剪贴板 q9e84f58

原文
剪贴板信息

剪贴板信息

收录
更新

upd on 2026/1/11:

  • fix: 一些愚蠢的问题;
  • change: 删除了意义不明的 const_bit 类;
  • change: 为提升性能,修改了 bitset operator|(const bitset &o) const 等函数的边界处理,现在你需要保证 this->n 不小于 o.n。
  • add: 现在允许自定义字长了(修改 bitset::kW 即可),不要求是 2 的幂!
  • add: 添加了 const_slice::all()、const_slice::count() 等函数。
C++
#include <string>
#include <vector>

using uint = unsigned int;
using ull = unsigned long long;

namespace my_bitset {
  struct bitset {
    using T = bitset;

    static constexpr uint kW = 64;
    static constexpr ull kM = (kW == 64 ? 0 : (1ull << kW));
    struct bit;
    struct slice;
    struct const_slice;

    uint n;
    std::vector<ull> a;

    bitset(uint _n = 0, bool v = 0);
    bitset(const const_slice& t);
    void reduce();  // 将 [0, n) 之外的位清零
    void clear();
    void resize(uint _n);
    void assign(uint _n, bool v);
    static T from_string(const std::string& s);
    static T from_ull(ull v);
    std::string to_string() const;

    bit operator[](uint i);
    bool operator[](uint i) const;
    bool test(uint i) const;
    void reset(uint i);
    void set(uint i, bool v = 1);
    void flip(uint i);
    void set();
    void reset();
    void flip();

    bool all() const;
    bool any() const;
    bool none() const;
    uint count() const;
    bool operator==(const T& o) const;
    bool operator!=(const T& o) const;

    uint find_first() const;
    int find_last() const;
    uint find_next(uint i) const;
    int find_prev(int i) const;

    T operator|(const T& o) const;
    T operator&(const T& o) const;
    T operator^(const T& o) const;
    T& operator|=(const T& o);
    T& operator&=(const T& o);
    T& operator^=(const T& o);
    T operator~() const;

    slice get_range(uint l, uint r);
    const_slice get_range(uint l, uint r) const;
    T operator<<(uint v) const;
    T operator>>(uint v) const;
    T& operator<<=(uint v);
    T& operator>>=(uint v);

    T operator+(const T& o) const;
    T operator-(const T& o) const;
    T& operator+=(const T& o);
    T& operator-=(const T& o);

    std::vector<uint> extract_ones() const;

    struct bit {
      ull* a;
      uint p;

      bit(const slice& t, uint _p);
      bit& operator=(bool v);
      bit& operator=(const bit& v);
      operator bool() const;
      bool operator~() const;
      bit& operator|=(bool v);
      bit& operator&=(bool v);
      bit& operator^=(bool v);
      void flip();
    };
    struct slice {
      ull* a;
      uint l, r;

      slice(T& t);
      slice(const slice& t, uint _l, uint _r);
      slice& operator=(const const_slice& o);
      slice& operator=(const slice& o);

      bit operator[](uint i);
      void reset(uint i);
      void set(uint i, bool v = 1);
      void flip(uint i);
      void set();
      void reset();
      void flip();

      slice& operator|=(const const_slice& o);
      slice& operator&=(const const_slice& o);
      slice& operator^=(const const_slice& o);

      slice get_range(uint l, uint r);
      const_slice get_range(uint l, uint r) const;
    };
    struct const_slice {
      const ull* a;
      uint l, r;

      const_slice(const T& t);
      const_slice(const slice& t);
      const_slice(const const_slice& t, uint _l, uint _r);

      bool operator[](uint i) const;
      bool test(uint i) const;

      bool all() const;
      bool any() const;
      bool none() const;
      uint count() const;
      bool operator==(const const_slice &o) const;
      bool operator!=(const const_slice &o) const;

      const_slice get_range(uint l, uint r) const;

      std::vector<uint> extract_ones() const;
    };
  };
  std::ostream& operator<<(std::ostream& out, const bitset& o);
}