comparison foosdk/sdk/pfc/bit_array.h @ 1:20d02a178406 default tip

*: check in everything else yay
author Paper <paper@tflc.us>
date Mon, 05 Jan 2026 02:15:46 -0500
parents
children
comparison
equal deleted inserted replaced
0:e9bb126753e7 1:20d02a178406
1 #pragma once
2
3 #include <functional>
4
5 namespace pfc {
6 //! Bit array interface class, constant version (you can only retrieve values). \n
7 //! Range of valid indexes depends on the context. When passing a bit_array as a parameter to some code, valid index range must be signaled independently.
8 class NOVTABLE bit_array {
9 public:
10 virtual bool get(t_size n) const = 0;
11 //! 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. \n
12 //! Can be overridden by bit_array implementations for improved speed in specific cases.
13 virtual t_size find(bool val, t_size start, t_ssize count) const;
14 bool operator[](t_size n) const { return get(n); }
15
16 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
17
18 t_size find_first(bool val, t_size start, t_size max) const;
19 t_size find_next(bool val, t_size previous, t_size max) const;
20
21 void for_each( bool value, size_t base, size_t max, std::function<void(size_t)> f) const;
22
23 void walk(size_t to, std::function< void ( size_t ) > f, bool val = true ) const;
24 void walkBack(size_t from, std::function< void ( size_t ) > f, bool val = true ) const;
25 protected:
26 bit_array() {}
27 ~bit_array() {}
28 };
29
30 //! Minimal lambda-based bit_array implementation, no find(), only get().
31 class bit_array_lambda : public bit_array {
32 public:
33 typedef std::function<bool (size_t)> f_t;
34 f_t f;
35
36 bit_array_lambda( f_t f_ ) : f(f_) { }
37
38 bool get(t_size n) const {return f(n);}
39 };
40
41 //! Bit array interface class, variable version (you can both set and retrieve values). \n
42 //! As with the constant version, valid index range depends on the context.
43 class NOVTABLE bit_array_var : public bit_array {
44 public:
45 virtual void set(t_size n,bool val)=0;
46 protected:
47 bit_array_var() {}
48 ~bit_array_var() {}
49 };
50
51 class bit_array_wrapper_permutation : public bit_array {
52 public:
53 bit_array_wrapper_permutation(const t_size * p_permutation, t_size p_size) : m_permutation(p_permutation), m_size(p_size) {}
54 bool get(t_size n) const {
55 if (n < m_size) {
56 return m_permutation[n] != n;
57 } else {
58 return false;
59 }
60 }
61 private:
62 const t_size * const m_permutation;
63 const t_size m_size;
64 };
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) ) )