3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2025-05-06 15:25:46 +00:00

adding fixed size bit-vectors

Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
This commit is contained in:
Nikolaj Bjorner 2014-09-15 20:00:45 -07:00
parent c09903288f
commit cd12fa8461
4 changed files with 388 additions and 0 deletions

118
src/util/fixed_bit_vector.h Normal file
View file

@ -0,0 +1,118 @@
/*++
Copyright (c) 2014 Microsoft Corporation
Module Name:
fixed_bit_vector.h
Abstract:
Simple bitvector implementation for fixed size bit-vectors.
Author:
Nikolaj Bjorner (nbjorner) 2014-9-15.
Revision History:
Related to bit_vector, but is based on a manager.
--*/
#ifndef _FIXED_BIT_VECTOR_H_
#define _FIXED_BIT_VECTOR_H_
#include<string.h>
#include"debug.h"
#include"small_object_allocator.h"
class fixed_bit_vector;
class fixed_bit_vector_manager {
friend class fixed_bit_vector;
small_object_allocator m_alloc;
unsigned m_num_bits;
unsigned m_num_bytes;
unsigned m_num_words;
unsigned m_mask;
unsigned num_bytes() const { return m_num_bytes; }
static unsigned num_words(unsigned num_bits) {
return (num_bits + 31) / 32;
}
public:
fixed_bit_vector_manager(unsigned num_bits);
fixed_bit_vector* allocate();
fixed_bit_vector* allocate1();
fixed_bit_vector* allocate0();
fixed_bit_vector* allocate(fixed_bit_vector& bv);
void deallocate(fixed_bit_vector* bv);
void copy(fixed_bit_vector& dst, fixed_bit_vector const& src) const;
unsigned num_words() const { return m_num_words; }
unsigned num_bits() const { return m_num_bits; }
fixed_bit_vector& reset(fixed_bit_vector& bv) const { return fill0(bv); }
fixed_bit_vector& fill0(fixed_bit_vector& bv) const;
fixed_bit_vector& fill1(fixed_bit_vector& bv) const;
fixed_bit_vector& set_and(fixed_bit_vector& dst, fixed_bit_vector const& src) const;
fixed_bit_vector& set_or(fixed_bit_vector& dst, fixed_bit_vector const& src) const;
fixed_bit_vector& set_neg(fixed_bit_vector& dst) const;
unsigned last_word(fixed_bit_vector const& bv) const;
bool equals(fixed_bit_vector const& a, fixed_bit_vector const& b) const;
unsigned hash(fixed_bit_vector const& src) const;
bool contains(fixed_bit_vector const& a, fixed_bit_vector const& b) const;
std::ostream& display(std::ostream& out, fixed_bit_vector const& b) const;
};
class fixed_bit_vector {
friend class fixed_bit_vector_manager;
unsigned m_data[1];
static unsigned get_pos_mask(unsigned bit_idx) {
return 1 << (bit_idx % 32);
}
unsigned get_bit_word(unsigned bit_idx) const {
return m_data[bit_idx / 32];
}
unsigned & get_bit_word(unsigned bit_idx) {
return m_data[bit_idx / 32];
}
public:
fixed_bit_vector() {}
~fixed_bit_vector() {}
unsigned get_word(unsigned word_idx) const { return m_data[word_idx]; }
bool operator[](unsigned bit_idx) const {
return get(bit_idx);
}
bool get(unsigned bit_idx) const {
return (get_bit_word(bit_idx) & get_pos_mask(bit_idx)) != 0;
}
void set(unsigned bit_idx) {
get_bit_word(bit_idx) |= get_pos_mask(bit_idx);
}
void unset(unsigned bit_idx) {
get_bit_word(bit_idx) &= ~get_pos_mask(bit_idx);
}
void set(unsigned bit_idx, bool val) {
int _val = static_cast<int>(val);
get_bit_word(bit_idx) ^= (-_val ^ get_bit_word(bit_idx)) & get_pos_mask(bit_idx);
}
};
#endif /* _FIXED_BIT_VECTOR_H_ */