File: bitfield.h

package info (click to toggle)
ctorrent 1.3.4.dnh3.3.2-5
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 1,240 kB
  • sloc: cpp: 13,161; sh: 675; ansic: 383; makefile: 6
file content (55 lines) | stat: -rw-r--r-- 1,234 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#ifndef BITFIELD_H
#define BITFIELD_H

#include <sys/types.h>

class BitField
{
 private:
  static size_t nbits;
  static size_t nbytes;

  unsigned char *b;
  size_t nset;

  void _recalc();
  void _setall(unsigned char* buf);
  void _set(size_t idx);

 public:
  BitField();
  BitField(size_t n_bits);
  BitField(const BitField &bf);
  ~BitField(){ if(b) delete []b; }

  void operator=(const BitField &bf);

  void SetAll();
  void Clear();
  void Set(size_t idx);
  void UnSet(size_t idx);

  int IsSet(size_t idx) const;
  int IsFull() const { return (nset >= nbits) ? 1 : 0; }
  int IsEmpty() const { return (nset == 0) ? 1 : 0; }

  size_t Count() const { return nset;}
  size_t NBytes() const { return nbytes; }
  size_t NBits() const { return nbits; }
  size_t Random() const;

  void Comb(const BitField &bf); 
  void Comb(const BitField *pbf) { if(pbf) Comb(*pbf); }
  void Except(const BitField &bf);
  void Except(const BitField *pbf) { if(pbf) Except(*pbf); }
  void And(const BitField &bf);
  void And(const BitField *pbf) { if(pbf) And(*pbf); }
  void Invert();

  void SetReferBuffer(char *buf);
  void WriteToBuffer(char *buf);
  int SetReferFile(const char *fname);
  int WriteToFile(const char *fname);
};

#endif