foobar2000 SDK  2015-08-03
bit_array.h
Go to the documentation of this file.
1 #ifndef _PFC_BIT_ARRAY_H_
2 #define _PFC_BIT_ARRAY_H_
3 namespace pfc {
6  class NOVTABLE bit_array {
7  public:
8  virtual bool get(t_size n) const = 0;
11  virtual t_size find(bool val,t_size start,t_ssize count) const
12  {
13  t_ssize d, todo, ptr = start;
14  if (count==0) return start;
15  else if (count<0) {d = -1; todo = -count;}
16  else {d = 1; todo = count;}
17  while(todo>0 && get(ptr)!=val) {ptr+=d;todo--;}
18  return ptr;
19  }
20  inline bool operator[](t_size n) const {return get(n);}
21 
22  t_size calc_count(bool val,t_size start,t_size count,t_size count_max = ~0) const//counts number of vals for start<=n<start+count
23  {
24  t_size found = 0;
25  t_size max = start+count;
26  t_size ptr;
27  for(ptr=find(val,start,count);found<count_max && ptr<max;ptr=find(val,ptr+1,max-ptr-1)) found++;
28  return found;
29  }
30 
31  inline t_size find_first(bool val,t_size start,t_size max) const {return find(val,start,max-start);}
32  inline t_size find_next(bool val,t_size previous,t_size max) const {return find(val,previous+1,max-(previous+1));}
33  protected:
34  bit_array() {}
36  };
37 
40  class NOVTABLE bit_array_var : public bit_array {
41  public:
42  virtual void set(t_size n,bool val)=0;
43  protected:
46  };
47 }
48 
49 typedef pfc::bit_array bit_array; //for compatibility
50 typedef pfc::bit_array_var bit_array_var; //for compatibility
51 
53 public:
54  bit_array_wrapper_permutation(const t_size * p_permutation, t_size p_size) : m_permutation(p_permutation), m_size(p_size) {}
55  bool get(t_size n) const {
56  if (n < m_size) {
57  return m_permutation[n] != n;
58  } else {
59  return false;
60  }
61  }
62 private:
63  const t_size * const m_permutation;
64  const t_size m_size;
65 };
66 
67 
68 #define PFC_FOR_EACH_INDEX( bitArray, var, count ) \
69 for( size_t var = (bitArray).find_first( true, 0, (count) ); var < (count); var = (bitArray).find_next( true, var, (count) ) )
70 
71 #endif
pfc::bit_array_var bit_array_var
Definition: bit_array.h:50
t_size find_next(bool val, t_size previous, t_size max) const
Definition: bit_array.h:32
t_size find_first(bool val, t_size start, t_size max) const
Definition: bit_array.h:31
virtual t_size find(bool val, t_size start, t_ssize count) const
Returns the first occurance of val between start and start+count (excluding start+count), or start+count if not found; count may be negative to search back rather than forward. Can be overridden by bit_array implementations for improved speed in specific cases.
Definition: bit_array.h:11
Bit array interface class, constant version (you can only retrieve values). Range of valid indexes d...
Definition: bit_array.h:6
const t_size *const m_permutation
Definition: bit_array.h:63
t_size calc_count(bool val, t_size start, t_size count, t_size count_max=~0) const
Definition: bit_array.h:22
bool operator[](t_size n) const
Definition: bit_array.h:20
size_t t_size
Definition: int_types.h:48
Bit array interface class, variable version (you can both set and retrieve values). As with the constant version, valid index range depends on the context.
Definition: bit_array.h:40
pfc::sized_int_t< sizeof(size_t) >::t_signed t_ssize
Definition: int_types.h:49
pfc::bit_array bit_array
Definition: bit_array.h:49
bit_array_wrapper_permutation(const t_size *p_permutation, t_size p_size)
Definition: bit_array.h:54