mirror of
https://github.com/Z3Prover/z3
synced 2025-09-02 08:10:43 +00:00
more cleanup
Signed-off-by: Leonardo de Moura <leonardo@microsoft.com>
This commit is contained in:
parent
c2e95bb0c5
commit
683687b153
15 changed files with 2 additions and 508 deletions
3688
src/muz_qe/imdd.cpp
Normal file
3688
src/muz_qe/imdd.cpp
Normal file
File diff suppressed because it is too large
Load diff
849
src/muz_qe/imdd.h
Normal file
849
src/muz_qe/imdd.h
Normal file
|
@ -0,0 +1,849 @@
|
|||
/*++
|
||||
Copyright (c) 2006 Microsoft Corporation
|
||||
|
||||
Module Name:
|
||||
|
||||
imdd.h
|
||||
|
||||
Abstract:
|
||||
|
||||
Interval based Multiple-valued Decision Diagrams.
|
||||
|
||||
Author:
|
||||
|
||||
Leonardo de Moura (leonardo) 2010-10-13.
|
||||
|
||||
Revision History:
|
||||
|
||||
--*/
|
||||
#ifndef _IMDD_H_
|
||||
#define _IMDD_H_
|
||||
|
||||
#include"id_gen.h"
|
||||
#include"hashtable.h"
|
||||
#include"map.h"
|
||||
#include"obj_hashtable.h"
|
||||
#include"obj_pair_hashtable.h"
|
||||
#include"buffer.h"
|
||||
#include"interval_skip_list.h"
|
||||
#include"region.h"
|
||||
#include"obj_ref.h"
|
||||
|
||||
class imdd;
|
||||
class imdd_manager;
|
||||
|
||||
/**
|
||||
\brief Manager for skip-lists used to implement IMDD nodes.
|
||||
*/
|
||||
class sl_imdd_manager : public random_level_manager {
|
||||
imdd_manager * m_manager; // real manager
|
||||
small_object_allocator & m_alloc;
|
||||
friend class imdd_manager;
|
||||
public:
|
||||
sl_imdd_manager(small_object_allocator & alloc):m_alloc(alloc) {}
|
||||
void * allocate(size_t size) { return m_alloc.allocate(size); }
|
||||
void deallocate(size_t size, void* p) { m_alloc.deallocate(size, p); }
|
||||
void inc_ref_eh(imdd * v);
|
||||
void dec_ref_eh(imdd * v);
|
||||
};
|
||||
|
||||
#define IMDD_BUCKET_CAPACITY 128
|
||||
#define IMDD_MAX_LEVEL 32
|
||||
|
||||
typedef interval_skip_list<unsigned_interval_skip_list_traits<imdd*,
|
||||
default_eq<imdd*>,
|
||||
IMDD_BUCKET_CAPACITY,
|
||||
IMDD_MAX_LEVEL,
|
||||
true, /* support ref-counting */
|
||||
sl_imdd_manager> > imdd_children;
|
||||
|
||||
typedef interval_skip_list<unsigned_interval_skip_list_traits<unsigned,
|
||||
default_eq<unsigned>,
|
||||
IMDD_BUCKET_CAPACITY,
|
||||
IMDD_MAX_LEVEL,
|
||||
false,
|
||||
sl_manager_base<unsigned> > > sl_interval_set;
|
||||
|
||||
/*
|
||||
Notes:
|
||||
|
||||
- We use reference counting for garbage collecting IMDDs nodes.
|
||||
|
||||
- Each IMDD node has a "memoized" flag. If the flag is true, the we use hash-consing for this node.
|
||||
|
||||
- The children of a memoized node must be memoized.
|
||||
|
||||
- The children of a non-memoized node may be memoized.
|
||||
|
||||
- The "memoized" flag cannot be reset after it was set.
|
||||
|
||||
- The result of some operations may be cached. We only use caching for
|
||||
operations processing memoized nodes.
|
||||
|
||||
- For non-memoized nodes, if m_ref_count <= 1, destructive updates may be performed by some operations.
|
||||
|
||||
- IMPORTANT: "memoized" flag == false doesn't imply m_ref_count <= 1.
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
\brief IMDDs
|
||||
*/
|
||||
class imdd {
|
||||
|
||||
protected:
|
||||
friend class imdd_manager;
|
||||
|
||||
unsigned m_id; //!< Unique ID
|
||||
unsigned m_ref_count;
|
||||
unsigned m_arity:30;
|
||||
unsigned m_memoized:1;
|
||||
unsigned m_dead:1;
|
||||
imdd_children m_children;
|
||||
|
||||
void inc_ref() {
|
||||
m_ref_count ++;
|
||||
}
|
||||
|
||||
void dec_ref() {
|
||||
SASSERT(m_ref_count > 0);
|
||||
m_ref_count --;
|
||||
}
|
||||
|
||||
void mark_as_memoized(bool flag = true) {
|
||||
SASSERT(is_memoized() != flag);
|
||||
m_memoized = flag;
|
||||
}
|
||||
|
||||
void mark_as_dead() { SASSERT(!m_dead); m_dead = true; }
|
||||
|
||||
void replace_children(sl_imdd_manager & m, sbuffer<imdd_children::entry> & new_children);
|
||||
|
||||
public:
|
||||
imdd(sl_imdd_manager & m, unsigned id, unsigned arity):m_id(id), m_ref_count(0), m_arity(arity), m_memoized(false), m_dead(false), m_children(m) {}
|
||||
unsigned get_id() const { return m_id; }
|
||||
unsigned get_ref_count() const { return m_ref_count; }
|
||||
bool is_memoized() const { return m_memoized; }
|
||||
bool is_shared() const { return m_ref_count > 1; }
|
||||
bool is_dead() const { return m_dead; }
|
||||
unsigned get_arity() const { return m_arity; }
|
||||
imdd_children::iterator begin_children() const { return m_children.begin(); }
|
||||
imdd_children::iterator end_children() const { return m_children.end(); }
|
||||
unsigned hc_hash() const; // hash code for hash-consing.
|
||||
bool hc_equal(imdd const * other) const; // eq function for hash-consing
|
||||
bool empty() const { return m_children.empty(); }
|
||||
unsigned hash() const { return m_id; }
|
||||
unsigned memory() const { return sizeof(imdd) + m_children.memory() - sizeof(imdd_children); }
|
||||
};
|
||||
|
||||
// -----------------------------------
|
||||
//
|
||||
// IMDD hash-consing
|
||||
//
|
||||
// -----------------------------------
|
||||
|
||||
// this is the internal hashing functor for hash-consing IMDDs.
|
||||
struct imdd_hash_proc {
|
||||
unsigned operator()(imdd const * d) const { return d->hc_hash(); }
|
||||
};
|
||||
|
||||
// This is the internal comparison functor for hash-consing IMDDs.
|
||||
struct imdd_eq_proc {
|
||||
bool operator()(imdd const * d1, imdd const * d2) const { return d1->hc_equal(d2); }
|
||||
};
|
||||
|
||||
typedef ptr_hashtable<imdd, imdd_hash_proc, imdd_eq_proc> imdd_table;
|
||||
typedef obj_hashtable<imdd> imdd_cache;
|
||||
typedef obj_map<imdd, imdd*> imdd2imdd_cache;
|
||||
typedef obj_pair_map<imdd, imdd, imdd*> imdd_pair2imdd_cache;
|
||||
typedef obj_pair_map<imdd, imdd, bool> imdd_pair2bool_cache;
|
||||
typedef obj_map<imdd, sl_interval_set*> imdd2intervals;
|
||||
|
||||
typedef std::pair<imdd*, unsigned> imdd_value_pair;
|
||||
|
||||
struct fi_cache_entry {
|
||||
imdd * m_d;
|
||||
unsigned m_lower;
|
||||
unsigned m_upper;
|
||||
unsigned m_hash;
|
||||
unsigned m_num_result;
|
||||
imdd_value_pair m_result[0];
|
||||
|
||||
void mk_hash() {
|
||||
m_hash = hash_u_u(m_d->get_id(), hash_u_u(m_lower, m_upper));
|
||||
}
|
||||
|
||||
fi_cache_entry(imdd * d, unsigned l, unsigned u):
|
||||
m_d(d),
|
||||
m_lower(l),
|
||||
m_upper(u) {
|
||||
mk_hash();
|
||||
}
|
||||
|
||||
fi_cache_entry(imdd * d, unsigned l, unsigned u, unsigned num, imdd_value_pair result[]):
|
||||
m_d(d),
|
||||
m_lower(l),
|
||||
m_upper(u),
|
||||
m_num_result(num) {
|
||||
mk_hash();
|
||||
memcpy(m_result, result, sizeof(imdd_value_pair)*num);
|
||||
}
|
||||
|
||||
unsigned hash() const {
|
||||
return m_hash;
|
||||
}
|
||||
|
||||
bool operator==(fi_cache_entry const & other) const {
|
||||
return
|
||||
m_d == other.m_d &&
|
||||
m_lower == other.m_lower &&
|
||||
m_upper == other.m_upper;
|
||||
}
|
||||
};
|
||||
|
||||
typedef obj_hashtable<fi_cache_entry> imdd_fi_cache;
|
||||
typedef union {
|
||||
imdd * m_d;
|
||||
fi_cache_entry * m_entry;
|
||||
} mk_fi_result;
|
||||
|
||||
struct filter_cache_entry {
|
||||
imdd * m_d;
|
||||
imdd * m_r;
|
||||
unsigned m_hash;
|
||||
unsigned m_ctx_size;
|
||||
unsigned m_ctx[0]; // lower and upper bounds that are part of the context.
|
||||
|
||||
static unsigned get_obj_size(unsigned ctx_size) {
|
||||
return sizeof(filter_cache_entry) + ctx_size * sizeof(unsigned);
|
||||
}
|
||||
|
||||
void mk_hash() {
|
||||
if (m_ctx_size > 0)
|
||||
m_hash = string_hash(reinterpret_cast<char *>(m_ctx), m_ctx_size * sizeof(unsigned), m_d->get_id());
|
||||
else
|
||||
m_hash = m_d->get_id();
|
||||
}
|
||||
|
||||
filter_cache_entry(imdd * d, imdd * r, unsigned ctx_size, unsigned * ctx):
|
||||
m_d(d),
|
||||
m_r(r),
|
||||
m_ctx_size(ctx_size) {
|
||||
memcpy(m_ctx, ctx, sizeof(unsigned)*m_ctx_size);
|
||||
mk_hash();
|
||||
}
|
||||
|
||||
unsigned hash() const {
|
||||
return m_hash;
|
||||
}
|
||||
|
||||
bool operator==(filter_cache_entry const & other) const {
|
||||
if (m_d != other.m_d)
|
||||
return false;
|
||||
if (m_ctx_size != other.m_ctx_size)
|
||||
return false;
|
||||
for (unsigned i = 0; i < m_ctx_size; i++)
|
||||
if (m_ctx[i] != other.m_ctx[i])
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
typedef obj_hashtable<filter_cache_entry> imdd_mk_filter_cache;
|
||||
|
||||
typedef obj_ref<imdd, imdd_manager> imdd_ref;
|
||||
|
||||
class imdd_manager {
|
||||
typedef imdd_children::entry entry;
|
||||
small_object_allocator m_alloc;
|
||||
id_gen m_id_gen;
|
||||
vector<imdd_table> m_tables; // we keep a table for each height.
|
||||
sl_imdd_manager m_sl_manager;
|
||||
unsigned m_simple_max_entries; //!< maximum number of entries in a "simple" node.
|
||||
bool m_delay_dealloc;
|
||||
ptr_vector<imdd> m_to_delete; //!< list of IMDDs marked as dead. These IMDDs may still be in cache tables.
|
||||
|
||||
// generic cache and todo-lists
|
||||
ptr_vector<imdd> m_worklist;
|
||||
imdd_cache m_visited;
|
||||
|
||||
void mark_as_dead(imdd * d);
|
||||
void deallocate_imdd(imdd * d);
|
||||
void delete_imdd(imdd * d);
|
||||
|
||||
class delay_dealloc;
|
||||
friend class delay_dealloc;
|
||||
|
||||
class delay_dealloc {
|
||||
imdd_manager & m_manager;
|
||||
bool m_delay_dealloc_value;
|
||||
unsigned m_to_delete_size;
|
||||
public:
|
||||
delay_dealloc(imdd_manager & m):
|
||||
m_manager(m),
|
||||
m_delay_dealloc_value(m_manager.m_delay_dealloc),
|
||||
m_to_delete_size(m_manager.m_to_delete.size()) {
|
||||
m_manager.m_delay_dealloc = true;
|
||||
}
|
||||
~delay_dealloc();
|
||||
};
|
||||
|
||||
bool is_simple_node(imdd * d) const;
|
||||
|
||||
void add_child(imdd * d, unsigned lower, unsigned upper, imdd * child) {
|
||||
d->m_children.insert(m_sl_manager, lower, upper, child);
|
||||
}
|
||||
|
||||
void add_child(imdd * d, unsigned value, imdd * child) {
|
||||
add_child(d, value, value, child);
|
||||
}
|
||||
|
||||
void remove_child(imdd * d, unsigned lower, unsigned upper) {
|
||||
d->m_children.remove(m_sl_manager, lower, upper);
|
||||
}
|
||||
|
||||
imdd * copy_main(imdd * d);
|
||||
|
||||
imdd * insert_main(imdd * d, unsigned b, unsigned e, bool destructive, bool memoize_res);
|
||||
|
||||
imdd * remove_main(imdd * d, unsigned b, unsigned e, bool destructive, bool memoize_res);
|
||||
|
||||
imdd2imdd_cache m_mk_product_cache;
|
||||
struct null2imdd_proc;
|
||||
struct mk_product_proc;
|
||||
friend struct mk_product_proc;
|
||||
imdd * mk_product_core(imdd * d1, imdd * d2, bool destructive, bool memoize);
|
||||
imdd * mk_product_main(imdd * d1, imdd * d2, bool destructive, bool memoize_res);
|
||||
|
||||
imdd2imdd_cache m_add_facts_cache;
|
||||
ptr_vector<imdd> m_add_facts_new_children;
|
||||
void init_add_facts_new_children(unsigned num, unsigned const * lowers, unsigned const * uppers, bool memoize_res);
|
||||
imdd * add_facts_core(imdd * d, unsigned num, unsigned const * lowers, unsigned const * uppers, bool destructive, bool memoize_res);
|
||||
imdd * add_facts_main(imdd * d, unsigned num, unsigned const * lowers, unsigned const * uppers, bool destructive, bool memoize_res);
|
||||
|
||||
imdd2imdd_cache m_remove_facts_cache;
|
||||
imdd * remove_facts_core(imdd * d, unsigned num, unsigned const * lowers, unsigned const * uppers, bool destructive, bool memoize_res);
|
||||
imdd * remove_facts_main(imdd * d, unsigned num, unsigned const * lowers, unsigned const * uppers, bool destructive, bool memoize_res);
|
||||
|
||||
|
||||
imdd2imdd_cache m_defrag_cache;
|
||||
imdd * defrag_core(imdd * d);
|
||||
|
||||
imdd_pair2imdd_cache m_union_cache;
|
||||
void push_back_entries(unsigned head, imdd_children::iterator & it, imdd_children::iterator & end,
|
||||
imdd_children::push_back_proc & push_back, bool & children_memoized);
|
||||
void push_back_upto(unsigned & head, imdd_children::iterator & it, imdd_children::iterator & end, unsigned limit,
|
||||
imdd_children::push_back_proc & push_back, bool & children_memoized);
|
||||
void move_head(unsigned & head, imdd_children::iterator & it, imdd_children::iterator & end, unsigned new_head);
|
||||
void copy_upto(unsigned & head, imdd_children::iterator & it, imdd_children::iterator & end, unsigned limit, sbuffer<entry> & result);
|
||||
void reset_union_cache();
|
||||
imdd * mk_union_core(imdd * d1, imdd * d2, bool destructive, bool memoize_res);
|
||||
imdd * mk_union_main(imdd * d1, imdd * d2, bool destructive, bool memoize_res);
|
||||
void mk_union_core_dupdt(imdd_ref & d1, imdd * d2, bool memoize_res);
|
||||
void mk_union_core(imdd * d1, imdd * d2, imdd_ref & r, bool memoize_res);
|
||||
|
||||
imdd_pair2bool_cache m_is_equal_cache;
|
||||
bool is_equal_core(imdd * d1, imdd * d2);
|
||||
|
||||
imdd_pair2bool_cache m_subsumes_cache;
|
||||
bool subsumes_core(imdd * d1, imdd * d2);
|
||||
|
||||
imdd2imdd_cache m_complement_cache;
|
||||
imdd * mk_complement_core(imdd * d, unsigned num, unsigned const * mins, unsigned const * maxs, bool destructive, bool memoize_res);
|
||||
imdd * mk_complement_main(imdd * d, unsigned num, unsigned const * mins, unsigned const * maxs, bool destructive, bool memoize_res);
|
||||
|
||||
imdd2imdd_cache m_filter_equal_cache;
|
||||
imdd * mk_filter_equal_core(imdd * d, unsigned vidx, unsigned value, bool destructive, bool memoize_res);
|
||||
imdd * mk_filter_equal_main(imdd * d, unsigned vidx, unsigned value, bool destructive, bool memoize_res);
|
||||
|
||||
|
||||
// original
|
||||
imdd2intervals m_imdd2interval_set;
|
||||
ptr_vector<sl_interval_set> m_alloc_is;
|
||||
typedef sl_manager_base<unsigned> sl_imanager;
|
||||
void reset_fi_intervals(sl_imanager& m);
|
||||
sl_interval_set const* init_fi_intervals(sl_imanager& m, imdd* d, unsigned var, unsigned num_found);
|
||||
|
||||
imdd2imdd_cache m_fi_top_cache;
|
||||
imdd_fi_cache m_fi_bottom_cache;
|
||||
unsigned m_fi_num_vars;
|
||||
unsigned * m_fi_begin_vars;
|
||||
unsigned * m_fi_end_vars;
|
||||
region m_fi_entries;
|
||||
bool is_fi_var(unsigned v) const { return std::find(m_fi_begin_vars, m_fi_end_vars, v) != m_fi_end_vars; }
|
||||
fi_cache_entry * mk_fi_cache_entry(imdd * d, unsigned lower, unsigned upper, unsigned num_pairs, imdd_value_pair pairs[]);
|
||||
mk_fi_result mk_filter_identical_core(imdd * d, unsigned offset, unsigned num_found, unsigned lower, unsigned upper,
|
||||
bool destructive, bool memoize_res);
|
||||
imdd * mk_filter_identical_main(imdd * d, unsigned num_vars, unsigned * vars, bool destructive, bool memoize_res);
|
||||
|
||||
// v2
|
||||
obj_map<imdd, imdd*> m_filter_identical_cache;
|
||||
void filter_identical_core2(imdd* d, unsigned num_vars, unsigned b, unsigned e, ptr_vector<imdd>& ch);
|
||||
imdd* filter_identical_core2(imdd* d, unsigned var, unsigned num_vars, bool memoize_res);
|
||||
void filter_identical_main2(imdd * d, imdd_ref& r, unsigned num_vars, unsigned * vars, bool destructive, bool memoize_res);
|
||||
void swap_in(imdd * d, imdd_ref& r, unsigned num_vars, unsigned * vars);
|
||||
void swap_out(imdd * d, imdd_ref& r, unsigned num_vars, unsigned * vars);
|
||||
|
||||
// v3
|
||||
struct interval {
|
||||
unsigned m_lo;
|
||||
unsigned m_hi;
|
||||
interval(unsigned lo, unsigned hi): m_lo(lo), m_hi(hi) {}
|
||||
};
|
||||
struct interval_dd : public interval {
|
||||
imdd* m_dd;
|
||||
interval_dd(unsigned lo, unsigned hi, imdd* d): interval(lo, hi), m_dd(d) {}
|
||||
};
|
||||
template<typename I>
|
||||
class id_map {
|
||||
unsigned m_T;
|
||||
unsigned_vector m_Ts;
|
||||
svector<svector<I>*> m_vecs;
|
||||
unsigned_vector m_alloc;
|
||||
unsigned m_first_free;
|
||||
void hard_reset() {
|
||||
std::for_each(m_vecs.begin(), m_vecs.end(), delete_proc<svector<I> >());
|
||||
m_alloc.reset();
|
||||
m_first_free = 0;
|
||||
m_vecs.reset();
|
||||
m_Ts.reset();
|
||||
m_T = 0;
|
||||
}
|
||||
|
||||
void allocate_entry(unsigned id) {
|
||||
if (m_vecs[id]) {
|
||||
return;
|
||||
}
|
||||
while (m_first_free < m_alloc.size()) {
|
||||
if (m_vecs[m_first_free] && m_Ts[m_first_free] < m_T) {
|
||||
svector<I>* v = m_vecs[m_first_free];
|
||||
m_vecs[m_first_free] = 0;
|
||||
m_vecs[id] = v;
|
||||
++m_first_free;
|
||||
return;
|
||||
}
|
||||
++m_first_free;
|
||||
}
|
||||
m_vecs[id] = alloc(svector<I>);
|
||||
m_alloc.push_back(id);
|
||||
}
|
||||
public:
|
||||
id_map():m_T(0) {}
|
||||
~id_map() { hard_reset(); }
|
||||
void reset() { ++m_T; m_first_free = 0; if (m_T == UINT_MAX) hard_reset(); }
|
||||
svector<I>& init(imdd* d) {
|
||||
unsigned id = d->get_id();
|
||||
if (id >= m_vecs.size()) {
|
||||
m_vecs.resize(id+1);
|
||||
m_Ts.resize(id+1);
|
||||
}
|
||||
if (m_Ts[id] < m_T) {
|
||||
allocate_entry(id);
|
||||
m_vecs[id]->reset();
|
||||
m_Ts[id] = m_T;
|
||||
}
|
||||
return *m_vecs[id];
|
||||
}
|
||||
|
||||
typedef svector<I> data;
|
||||
struct iterator {
|
||||
unsigned m_offset;
|
||||
id_map const& m;
|
||||
iterator(unsigned o, id_map const& m):m_offset(o),m(m) {}
|
||||
data const & operator*() const { return *m.m_vecs[m_offset]; }
|
||||
data const * operator->() const { return &(operator*()); }
|
||||
data * operator->() { return &(operator*()); }
|
||||
iterator & operator++() { ++m_offset; return move_to_used(); }
|
||||
iterator operator++(int) { iterator tmp = *this; ++*this; return tmp; }
|
||||
bool operator==(iterator const & it) const { return m_offset == it.m_offset; }
|
||||
bool operator!=(iterator const & it) const { return m_offset != it.m_offset; }
|
||||
iterator & move_to_used() {
|
||||
while (m_offset < m.m_vecs.size() &&
|
||||
m.m_Ts[m_offset] < m.m_T) {
|
||||
++m_offset;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
iterator begin() const { return iterator(0, *this).move_to_used(); }
|
||||
iterator end() const { return iterator(m_vecs.size(), *this); }
|
||||
};
|
||||
typedef id_map<interval > filter_id_map;
|
||||
typedef id_map<interval_dd > filter_idd_map;
|
||||
filter_id_map m_nodes;
|
||||
filter_idd_map m_nodes_dd;
|
||||
svector<interval_dd> m_i_nodes_dd, m_i_nodes_dd_tmp;
|
||||
svector<interval> m_i_nodes, m_i_nodes_tmp;
|
||||
unsigned_vector m_offsets;
|
||||
void filter_identical_main3(imdd * d, imdd_ref& r, unsigned num_vars, unsigned * vars, bool destructive, bool memoize_res);
|
||||
void filter_identical_main3(imdd * d, imdd_ref& r, unsigned v1, bool del1, unsigned v2, bool del2, bool memoize_res);
|
||||
imdd* filter_identical_loop3(imdd * d, unsigned v1, bool del1, unsigned v2, bool del2, bool memoize_res);
|
||||
void refine_intervals(svector<interval>& i_nodes, svector<interval_dd> const& i_nodes_dd);
|
||||
void merge_intervals(svector<interval>& dst, svector<interval> const& src);
|
||||
imdd* filter_identical_mk_nodes(imdd* d, unsigned v, bool del1, bool del2, bool memoize_res);
|
||||
void print_filter_idd(std::ostream& out, filter_idd_map const& m);
|
||||
void print_interval_dd(std::ostream& out, svector<interval_dd> const& m);
|
||||
|
||||
|
||||
|
||||
unsigned m_proj_num_vars;
|
||||
unsigned * m_proj_begin_vars;
|
||||
unsigned * m_proj_end_vars;
|
||||
imdd2imdd_cache m_proj_cache;
|
||||
bool is_proj_var(unsigned v) const { return std::find(m_proj_begin_vars, m_proj_end_vars, v) != m_proj_end_vars; }
|
||||
void mk_project_init(unsigned num_vars, unsigned * vars);
|
||||
void mk_project_core(imdd * d, imdd_ref & r, unsigned var, unsigned num_found, bool memoize_res);
|
||||
void mk_project_dupdt_core(imdd_ref & d, unsigned var, unsigned num_found, bool memoize_res);
|
||||
|
||||
imdd2imdd_cache m_swap_cache;
|
||||
imdd * m_swap_new_child;
|
||||
bool m_swap_granchildren_memoized;
|
||||
imdd * mk_swap_new_child(unsigned lower, unsigned upper, imdd * child);
|
||||
void mk_swap_acc1_dupdt(imdd_ref & d, unsigned lower, unsigned upper, imdd * grandchild, bool memoize_res);
|
||||
void mk_swap_acc1(imdd * d, imdd_ref & r, unsigned lower, unsigned upper, imdd * grandchild, bool memoize_res);
|
||||
void mk_swap_acc2(imdd_ref & r, unsigned lower1, unsigned upper1, unsigned lower2, unsigned upper2, imdd * grandchild, bool memoize_res);
|
||||
void mk_swap_top_vars(imdd * d, imdd_ref & r, bool memoize_res);
|
||||
imdd * mk_swap_memoize(imdd * d);
|
||||
void mk_swap_core(imdd * d, imdd_ref & r, unsigned vidx, bool memoize_res);
|
||||
void mk_swap_dupdt_core(imdd_ref & d, unsigned vidx, bool memoize_res);
|
||||
|
||||
imdd2imdd_cache m_add_bounded_var_cache;
|
||||
imdd * add_bounded_var_core(imdd * d, unsigned before_vidx, unsigned lower, unsigned upper, bool destructive, bool memoize_res);
|
||||
imdd * add_bounded_var_main(imdd * d, unsigned before_vidx, unsigned lower, unsigned upper, bool destructive, bool memoize_res);
|
||||
|
||||
friend struct distinct_proc;
|
||||
imdd * mk_distinct_imdd(unsigned l1, unsigned u1, unsigned l2, unsigned u2, imdd * d, bool memoize_res = true);
|
||||
|
||||
imdd_mk_filter_cache m_filter_cache;
|
||||
region m_filter_entries;
|
||||
unsigned m_filter_num_vars;
|
||||
unsigned * m_filter_begin_vars;
|
||||
unsigned * m_filter_end_vars;
|
||||
unsigned_vector m_filter_context;
|
||||
bool is_filter_var(unsigned v) const { return std::find(m_filter_begin_vars, m_filter_end_vars, v) != m_filter_end_vars; }
|
||||
filter_cache_entry * mk_filter_cache_entry(imdd * d, unsigned ctx_sz, unsigned * ctx, imdd * r);
|
||||
imdd * is_mk_filter_cached(imdd * d, unsigned ctx_sz, unsigned * ctx);
|
||||
void cache_mk_filter(imdd * d, unsigned ctx_sz, unsigned * ctx, imdd * r);
|
||||
void init_mk_filter(unsigned arity, unsigned num_vars, unsigned * vars);
|
||||
template<typename FilterProc>
|
||||
void mk_filter_dupdt_core(imdd_ref & d, unsigned vidx, unsigned num_found, FilterProc & proc, bool memoize_res);
|
||||
template<typename FilterProc>
|
||||
void mk_filter_core(imdd * d, imdd_ref & r, unsigned vidx, unsigned num_found, FilterProc & proc, bool memoize_res);
|
||||
/**
|
||||
\brief Filter the elements of the given IMDD using the given filter.
|
||||
|
||||
The FilterProc template parameter is a filter for computing subsets of sets of the form:
|
||||
|
||||
[L_1, U_1] X [L_2, U_2] X ... X [L_n, U_n] X d (where d is an IMDD)
|
||||
|
||||
where n == num_vars
|
||||
|
||||
The subset of elements is returned as an IMDD.
|
||||
|
||||
FilterProc must have a method of the form:
|
||||
|
||||
void operator()(unsigned * lowers_uppers, imdd * d, imdd_ref & r, bool memoize_res);
|
||||
|
||||
The size of the array lowers_uppers is 2*num_vars
|
||||
|
||||
The arity of the resultant IMDD must be num_vars + d->get_arity().
|
||||
*/
|
||||
template<typename FilterProc>
|
||||
void mk_filter_dupdt(imdd_ref & d, unsigned num_vars, unsigned * vars, FilterProc & proc, bool memoize_res = true);
|
||||
template<typename FilterProc>
|
||||
void mk_filter(imdd * d, imdd_ref & r, unsigned num_vars, unsigned * vars, FilterProc & proc, bool memoize_res = true);
|
||||
|
||||
imdd * mk_disequal_imdd(unsigned l1, unsigned u1, unsigned value, imdd * d, bool memoize_res);
|
||||
friend struct disequal_proc;
|
||||
|
||||
public:
|
||||
imdd_manager();
|
||||
|
||||
void inc_ref(imdd * d) {
|
||||
if (d)
|
||||
d->inc_ref();
|
||||
}
|
||||
|
||||
void dec_ref(imdd * d) {
|
||||
if (d) {
|
||||
d->dec_ref();
|
||||
if (d->get_ref_count() == 0)
|
||||
delete_imdd(d);
|
||||
}
|
||||
}
|
||||
|
||||
unsigned get_num_nodes(imdd const * d) const;
|
||||
|
||||
// count number of keys (rows) in table as if table is uncompressed.
|
||||
size_t get_num_rows(imdd const* d) const;
|
||||
|
||||
unsigned memory(imdd const * d) const;
|
||||
|
||||
private:
|
||||
imdd * _mk_empty(unsigned arity);
|
||||
|
||||
public:
|
||||
imdd * mk_empty(unsigned arity) {
|
||||
imdd * r = _mk_empty(arity);
|
||||
STRACE("imdd_trace", tout << "mk_empty(" << arity << ", 0x" << r << ");\n";);
|
||||
return r;
|
||||
}
|
||||
|
||||
private:
|
||||
imdd * memoize(imdd * d);
|
||||
|
||||
public:
|
||||
void memoize(imdd_ref const & d, imdd_ref & r) { r = memoize(d.get()); }
|
||||
|
||||
void memoize(imdd_ref & d) { d = memoize(d.get()); }
|
||||
|
||||
imdd * memoize_new_imdd_if(bool cond, imdd * r) {
|
||||
if (cond && is_simple_node(r)) {
|
||||
SASSERT(!r->is_shared());
|
||||
imdd * can_r = memoize(r);
|
||||
if (can_r != r) {
|
||||
SASSERT(r->get_ref_count() == 0);
|
||||
delete_imdd(r);
|
||||
}
|
||||
return can_r;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
public:
|
||||
void defrag(imdd_ref & d);
|
||||
|
||||
void unmemoize(imdd * d);
|
||||
|
||||
void unmemoize_rec(imdd * d);
|
||||
|
||||
void copy(imdd * d, imdd_ref & r) { r = copy_main(d); }
|
||||
|
||||
void insert_dupdt(imdd_ref & d, unsigned b, unsigned e, bool memoize_res = true) {
|
||||
d = insert_main(d, b, e, true, memoize_res);
|
||||
}
|
||||
|
||||
void insert(imdd * d, imdd_ref & r, unsigned b, unsigned e, bool memoize_res = true) {
|
||||
r = insert_main(d, b, e, false, memoize_res);
|
||||
}
|
||||
|
||||
void mk_product_dupdt(imdd_ref & d1, imdd * d2, bool memoize_res = true) {
|
||||
d1 = mk_product_main(d1.get(), d2, true, memoize_res);
|
||||
}
|
||||
|
||||
void mk_product(imdd * d1, imdd * d2, imdd_ref & r, bool memoize_res = true) {
|
||||
r = mk_product_main(d1, d2, false, memoize_res);
|
||||
STRACE("imdd_trace", tout << "mk_product(0x" << d1 << ", 0x" << d2 << ", 0x" << r.get() << ", " << memoize_res << ");\n";);
|
||||
}
|
||||
|
||||
void mk_union_dupdt(imdd_ref & d1, imdd * d2, bool memoize_res = true) {
|
||||
d1 = mk_union_main(d1.get(), d2, true, memoize_res);
|
||||
}
|
||||
|
||||
void mk_union(imdd * d1, imdd * d2, imdd_ref & r, bool memoize_res = true) {
|
||||
r = mk_union_main(d1, d2, false, memoize_res);
|
||||
STRACE("imdd_trace", tout << "mk_union(0x" << d1 << ", 0x" << d2 << ", 0x" << r.get() << ", " << memoize_res << ");\n";);
|
||||
}
|
||||
|
||||
void mk_complement_dupdt(imdd_ref & d, unsigned num, unsigned const * mins, unsigned const * maxs, bool memoize_res = true) {
|
||||
d = mk_complement_main(d, num, mins, maxs, true, memoize_res);
|
||||
}
|
||||
|
||||
void mk_complement(imdd * d, imdd_ref & r, unsigned num, unsigned const * mins, unsigned const * maxs, bool memoize_res = true) {
|
||||
r = mk_complement_main(d, num, mins, maxs, false, memoize_res);
|
||||
|
||||
STRACE("imdd_trace", tout << "mk_complement(0x" << d << ", 0x" << r.get() << ", ";
|
||||
for (unsigned i = 0; i < num; i++) tout << mins[i] << ", " << maxs[i] << ", ";
|
||||
tout << memoize_res << ");\n";);
|
||||
}
|
||||
|
||||
void mk_filter_equal_dupdt(imdd_ref & d, unsigned vidx, unsigned value, bool memoize_res = true) {
|
||||
d = mk_filter_equal_main(d, vidx, value, true, memoize_res);
|
||||
}
|
||||
|
||||
void mk_filter_equal(imdd * d, imdd_ref & r, unsigned vidx, unsigned value, bool memoize_res = true) {
|
||||
r = mk_filter_equal_main(d, vidx, value, false, memoize_res);
|
||||
|
||||
STRACE("imdd_trace", tout << "mk_filter_equal(0x" << d << ", 0x" << r.get() << ", " << vidx << ", " << value << ", " << memoize_res << ");\n";);
|
||||
}
|
||||
|
||||
void mk_filter_identical_dupdt(imdd_ref & d, unsigned num_vars, unsigned * vars, bool memoize_res = true) {
|
||||
// d = mk_filter_identical_main(d, num_vars, vars, true, memoize_res);
|
||||
filter_identical_main3(d, d, num_vars, vars, true, memoize_res);
|
||||
}
|
||||
|
||||
void mk_filter_identical(imdd * d, imdd_ref & r, unsigned num_vars, unsigned * vars, bool memoize_res = true) {
|
||||
filter_identical_main3(d, r, num_vars, vars, false, memoize_res);
|
||||
|
||||
STRACE("imdd_trace", tout << "mk_filter_identical(0x" << d << ", 0x" << r.get() << ", ";
|
||||
for (unsigned i = 0; i < num_vars; i++) tout << vars[i] << ", ";
|
||||
tout << memoize_res << ");\n";);
|
||||
}
|
||||
|
||||
void mk_project_dupdt(imdd_ref & d, unsigned num_vars, unsigned * vars, bool memoize_res = true);
|
||||
|
||||
void mk_project(imdd * d, imdd_ref & r, unsigned num_vars, unsigned * vars, bool memoize_res = true);
|
||||
|
||||
// swap vidx and vidx+1
|
||||
void mk_swap_dupdt(imdd_ref & d, unsigned vidx, bool memoize_res = true);
|
||||
|
||||
// swap vidx and vidx+1
|
||||
void mk_swap(imdd * d, imdd_ref & r, unsigned vidx, bool memoize_res = true);
|
||||
|
||||
void add_facts_dupdt(imdd_ref & d, unsigned num, unsigned const * lowers, unsigned const * uppers, bool memoize_res = true) {
|
||||
d = add_facts_main(d, num, lowers, uppers, true, memoize_res);
|
||||
}
|
||||
|
||||
void add_facts(imdd * d, imdd_ref & r, unsigned num, unsigned const * lowers, unsigned const * uppers, bool memoize_res = true) {
|
||||
r = add_facts_main(d, num, lowers, uppers, false, memoize_res);
|
||||
|
||||
STRACE("imdd_trace", tout << "add_facts(0x" << d << ", 0x" << r.get() << ", ";
|
||||
for (unsigned i = 0; i < num; i++) tout << lowers[i] << ", " << uppers[i] << ", ";
|
||||
tout << memoize_res << ");\n";);
|
||||
}
|
||||
|
||||
void add_fact_dupdt(imdd_ref & d, unsigned num, unsigned const * values, bool memoize_res = true) {
|
||||
add_facts_dupdt(d, num, values, values, memoize_res);
|
||||
}
|
||||
|
||||
void add_fact(imdd * d, imdd_ref & r, unsigned num, unsigned const * values, bool memoize_res = true) {
|
||||
add_facts(d, r, num, values, values, memoize_res);
|
||||
}
|
||||
|
||||
void add_bounded_var_dupdt(imdd_ref & d, unsigned before_vidx, unsigned lower, unsigned upper, bool memoize_res = true) {
|
||||
d = add_bounded_var_main(d, before_vidx, lower, upper, true, memoize_res);
|
||||
}
|
||||
|
||||
void add_bounded_var(imdd * d, imdd_ref & r, unsigned before_vidx, unsigned lower, unsigned upper, bool memoize_res = true) {
|
||||
r = add_bounded_var_main(d, before_vidx, lower, upper, false, memoize_res);
|
||||
}
|
||||
|
||||
void mk_filter_distinct_dupdt(imdd_ref & d, unsigned v1, unsigned v2, bool memoize_res = true);
|
||||
|
||||
void mk_filter_distinct(imdd * d, imdd_ref & r, unsigned v1, unsigned v2, bool memoize_res = true);
|
||||
|
||||
void mk_filter_disequal_dupdt(imdd_ref & d, unsigned var, unsigned value, bool memoize_res = true);
|
||||
|
||||
void mk_filter_disequal(imdd * d, imdd_ref & r, unsigned var, unsigned value, bool memoize_res = true);
|
||||
|
||||
void mk_join(imdd * d1, imdd * d2, imdd_ref & r, unsigned_vector const& vars1, unsigned_vector const& vars2, bool memoize_res = true);
|
||||
|
||||
void mk_join_project(imdd * d1, imdd * d2, imdd_ref & r,
|
||||
unsigned_vector const& vars1, unsigned_vector const& vars2,
|
||||
unsigned_vector const& proj_vars, bool memoize_res = true);
|
||||
|
||||
void mk_join_dupdt(imdd_ref & d1, imdd * d2, unsigned num_vars, unsigned const * vars1, unsigned const * vars2, bool memoize_res = true);
|
||||
|
||||
void remove_facts_dupdt(imdd_ref & d, unsigned num, unsigned const * lowers, unsigned const * uppers);
|
||||
|
||||
void remove_facts(imdd * d, imdd_ref & r, unsigned num, unsigned const * lowers, unsigned const * uppers);
|
||||
|
||||
bool is_equal(imdd * d1, imdd * d2);
|
||||
|
||||
bool contains(imdd * d, unsigned num, unsigned const * values) const;
|
||||
|
||||
bool contains(imdd * d, unsigned v) const { return contains(d, 1, &v); }
|
||||
|
||||
bool contains(imdd * d, unsigned v1, unsigned v2) const { unsigned vs[2] = {v1, v2}; return contains(d, 2, vs); }
|
||||
|
||||
bool contains(imdd * d, unsigned v1, unsigned v2, unsigned v3) const { unsigned vs[3] = {v1,v2,v3}; return contains(d, 3, vs); }
|
||||
|
||||
bool subsumes(imdd * d1, imdd * d2);
|
||||
|
||||
bool is_subset(imdd * d1, imdd * d2) { return subsumes(d2, d1); }
|
||||
|
||||
private:
|
||||
void display(std::ostream & out, imdd const * d, unsigned_vector & intervals, bool & first) const;
|
||||
|
||||
public:
|
||||
void display(std::ostream & out, imdd const * d) const;
|
||||
|
||||
void display_ll(std::ostream & out, imdd const * d) const;
|
||||
|
||||
/**
|
||||
\brief Execute proc (once) in each node in the IMDD rooted by d.
|
||||
*/
|
||||
template<typename Proc>
|
||||
void for_each(imdd * d, Proc & proc) const {
|
||||
// for_each is a generic procedure, we don't know what proc will actually do.
|
||||
// So, it is not safe to reuse m_worklist and m_visited.
|
||||
ptr_buffer<imdd,128> worklist;
|
||||
imdd_cache visited;
|
||||
worklist.push_back(d);
|
||||
while (!worklist.empty()) {
|
||||
d = worklist.back();
|
||||
worklist.pop_back();
|
||||
if (d->is_shared() && visited.contains(d))
|
||||
continue;
|
||||
if (d->is_shared())
|
||||
visited.insert(d);
|
||||
proc(d);
|
||||
if (d->get_arity() > 1) {
|
||||
imdd_children::iterator it = d->begin_children();
|
||||
imdd_children::iterator end = d->end_children();
|
||||
for (; it != end; ++it)
|
||||
worklist.push_back(it->val());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class iterator {
|
||||
bool m_done;
|
||||
svector<imdd_children::iterator> m_iterators;
|
||||
svector<unsigned> m_element;
|
||||
|
||||
void begin_iterators(imdd const * curr, unsigned start_idx);
|
||||
|
||||
public:
|
||||
iterator():m_done(true) {}
|
||||
iterator(imdd_manager const & m, imdd const * d);
|
||||
|
||||
unsigned get_arity() const { return m_element.size(); }
|
||||
unsigned * operator*() const { return m_element.c_ptr(); }
|
||||
iterator & operator++();
|
||||
|
||||
bool operator==(iterator const & it) const;
|
||||
bool operator!=(iterator const & it) const { return !operator==(it); }
|
||||
};
|
||||
|
||||
friend class iterator;
|
||||
|
||||
iterator begin(imdd const * d) const { return iterator(*this, d); }
|
||||
iterator end(imdd const * d) const { return iterator(); }
|
||||
};
|
||||
|
||||
inline std::ostream & operator<<(std::ostream & out, imdd_ref const & r) {
|
||||
r.get_manager().display(out, r.get());
|
||||
return out;
|
||||
}
|
||||
|
||||
struct mk_imdd_pp {
|
||||
imdd * m_d;
|
||||
imdd_manager & m_manager;
|
||||
mk_imdd_pp(imdd * d, imdd_manager & m):m_d(d), m_manager(m) {}
|
||||
};
|
||||
|
||||
inline mk_imdd_pp mk_pp(imdd * d, imdd_manager & m) {
|
||||
return mk_imdd_pp(d, m);
|
||||
}
|
||||
|
||||
inline std::ostream & operator<<(std::ostream & out, mk_imdd_pp const & pp) {
|
||||
pp.m_manager.display(out, pp.m_d);
|
||||
return out;
|
||||
}
|
||||
|
||||
struct mk_imdd_ll_pp : public mk_imdd_pp {
|
||||
mk_imdd_ll_pp(imdd * d, imdd_manager & m):mk_imdd_pp(d, m) {}
|
||||
};
|
||||
|
||||
inline mk_imdd_ll_pp mk_ll_pp(imdd * d, imdd_manager & m) {
|
||||
return mk_imdd_ll_pp(d, m);
|
||||
}
|
||||
|
||||
inline std::ostream & operator<<(std::ostream & out, mk_imdd_ll_pp const & pp) {
|
||||
pp.m_manager.display_ll(out, pp.m_d);
|
||||
return out;
|
||||
}
|
||||
|
||||
#endif /* _IMDD_H_ */
|
||||
|
1876
src/muz_qe/interval_skip_list.h
Normal file
1876
src/muz_qe/interval_skip_list.h
Normal file
File diff suppressed because it is too large
Load diff
871
src/muz_qe/skip_list_base.h
Normal file
871
src/muz_qe/skip_list_base.h
Normal file
|
@ -0,0 +1,871 @@
|
|||
/*++
|
||||
Copyright (c) 2006 Microsoft Corporation
|
||||
|
||||
Module Name:
|
||||
|
||||
skip_list_base.h
|
||||
|
||||
Abstract:
|
||||
|
||||
<abstract>
|
||||
|
||||
Author:
|
||||
|
||||
Leonardo de Moura (leonardo) 2010-10-01.
|
||||
|
||||
Revision History:
|
||||
|
||||
WARNING: IT IS NOT SAFE TO STORE KEYS, VALUES in the SKIP_LIST that need non-default constructors/destructors.
|
||||
|
||||
--*/
|
||||
#ifndef _SKIP_LIST_BASE_H_
|
||||
#define _SKIP_LIST_BASE_H_
|
||||
|
||||
#include<memory.h>
|
||||
#include"util.h"
|
||||
#include"memory_manager.h"
|
||||
#include"small_object_allocator.h"
|
||||
#include"trace.h"
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(disable : 4200)
|
||||
#endif
|
||||
|
||||
/*
|
||||
This file defines a base class for implementing skip-list like data-structures.
|
||||
This base class is relies on a manager for providing some basic services.
|
||||
The manager is a template parameter.
|
||||
|
||||
A Skip-list manager is responsible for:
|
||||
|
||||
- Providing primitives for allocating/deallocating memory
|
||||
void * allocate(size_t size);
|
||||
void deallocate(size_t size, void* p);
|
||||
- Generating random skip-list levels efficiently
|
||||
unsigned random_level(unsigned max_level);
|
||||
- Call-backs that will be invoked when a reference for a "value" stored in the skip-list is incremented/decremented.
|
||||
void inc_ref_eh(value const & v);
|
||||
void dec_ref_eh(value const & h);
|
||||
*/
|
||||
|
||||
/**
|
||||
\brief Base class for generating random_levels.
|
||||
*/
|
||||
class random_level_manager {
|
||||
#define SL_BITS_IN_RANDOM 16
|
||||
unsigned m_random_data;
|
||||
unsigned m_random_bits:16;
|
||||
unsigned m_random_left:16;
|
||||
|
||||
unsigned random_value() {
|
||||
return ((m_random_data = m_random_data * 214013L + 2531011L) >> 16) & 0xffff;
|
||||
}
|
||||
|
||||
void init_random() {
|
||||
m_random_data = 0;
|
||||
m_random_bits = random_value();
|
||||
m_random_left = SL_BITS_IN_RANDOM/2;
|
||||
}
|
||||
public:
|
||||
random_level_manager() {
|
||||
init_random();
|
||||
}
|
||||
|
||||
unsigned random_level(unsigned max_level) {
|
||||
unsigned level = 1;
|
||||
unsigned b;
|
||||
do {
|
||||
b = m_random_bits&3;
|
||||
if (!b)
|
||||
level++;
|
||||
m_random_bits >>= 2;
|
||||
m_random_left--;
|
||||
if (m_random_left == 0) {
|
||||
m_random_bits = random_value();
|
||||
m_random_left = SL_BITS_IN_RANDOM/2;
|
||||
}
|
||||
} while (!b);
|
||||
return (level > max_level ? max_level : level);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
\brief Basic skip-list manager.
|
||||
The class is parametrized by the Value type that is stored in the skip-list.
|
||||
*/
|
||||
template<typename Value>
|
||||
class sl_manager_base : public random_level_manager {
|
||||
typedef Value value;
|
||||
|
||||
small_object_allocator m_alloc;
|
||||
|
||||
public:
|
||||
void * allocate(size_t size) {
|
||||
return m_alloc.allocate(size);
|
||||
}
|
||||
|
||||
void deallocate(size_t size, void* p) {
|
||||
m_alloc.deallocate(size, p);
|
||||
}
|
||||
|
||||
void inc_ref_eh(value const & v) {
|
||||
/* do nothing */
|
||||
}
|
||||
|
||||
void dec_ref_eh(value const & h) {
|
||||
/* do nothing */
|
||||
}
|
||||
};
|
||||
|
||||
#define SL_SIZE_NUM_BITS 12
|
||||
#define SL_CAPACITY_NUM_BITS SL_SIZE_NUM_BITS
|
||||
#define SL_MAX_CAPACITY ((1 << SL_SIZE_NUM_BITS) - 1)
|
||||
#define SL_LEVEL_NUM_BITS 8
|
||||
#define SL_MAX_LEVEL ((1 << SL_LEVEL_NUM_BITS) - 1)
|
||||
COMPILE_TIME_ASSERT(SL_SIZE_NUM_BITS == SL_CAPACITY_NUM_BITS);
|
||||
COMPILE_TIME_ASSERT(SL_SIZE_NUM_BITS + SL_CAPACITY_NUM_BITS + SL_LEVEL_NUM_BITS == 32);
|
||||
|
||||
/**
|
||||
\brief Base (template) class for implementing skip-list like data-structures where
|
||||
entries are stored in buckets to improve cache behavior.
|
||||
|
||||
The Traits template parameter must provide:
|
||||
|
||||
- a definition for the class Traits::manager
|
||||
- a definition for the class Traits::entry which provides:
|
||||
- a definition for the types key and value
|
||||
- the methods:
|
||||
key const & begin_key() const
|
||||
key const & end_key() const
|
||||
value const & val() const
|
||||
void set_begin_key(key const & k)
|
||||
void set_end_key(key const & k)
|
||||
void set_val(value const & v)
|
||||
void display(ostream & out) const
|
||||
- the maximal number of levels Traits::max_level
|
||||
- the maximal capacity of each bucket Traits::max_capacity
|
||||
- the initial capacity of the first bucket Traits::initial_capacity
|
||||
- flag for reference counting support Traits::ref_count. If this flag is true
|
||||
the methods inc_ref_eh and dec_ref_eh in the manager object will be invoked.
|
||||
- the methods
|
||||
bool lt(key const & k1, key const & k2)
|
||||
bool eq(key const & k1, key const & k2)
|
||||
bool val_eq(value const & v1, value const & v2)
|
||||
key succ(key const & k)
|
||||
key pred(key const & k)
|
||||
*/
|
||||
template<typename Traits>
|
||||
class skip_list_base : protected Traits {
|
||||
protected:
|
||||
typedef typename Traits::entry entry;
|
||||
public:
|
||||
typedef typename Traits::manager manager;
|
||||
typedef typename entry::key key;
|
||||
typedef typename entry::value value;
|
||||
|
||||
struct bucket {
|
||||
unsigned m_size:SL_SIZE_NUM_BITS; //!< number of entries stored in the bucket.
|
||||
unsigned m_capacity:SL_CAPACITY_NUM_BITS; //!< capacity (number of entries) that can be stored in the bucket.
|
||||
unsigned m_level:SL_LEVEL_NUM_BITS;
|
||||
char m_extra[0];
|
||||
|
||||
static unsigned get_obj_size(unsigned num_lvls, unsigned capacity) {
|
||||
return sizeof(bucket) + num_lvls*sizeof(bucket*) + capacity*sizeof(entry);
|
||||
}
|
||||
|
||||
entry * get_entries() { return reinterpret_cast<entry*>(m_extra); }
|
||||
entry const * get_entries() const { return reinterpret_cast<entry const *>(m_extra); }
|
||||
|
||||
bucket ** next_vect() { return reinterpret_cast<bucket**>(get_entries() + m_capacity); }
|
||||
bucket * const * next_vect() const { return reinterpret_cast<bucket* const *>(get_entries() + m_capacity); }
|
||||
|
||||
bucket(unsigned lvl, unsigned capacity = Traits::max_capacity):
|
||||
m_size(0),
|
||||
m_capacity(capacity),
|
||||
m_level(lvl) {
|
||||
memset(next_vect(), 0, sizeof(bucket*)*lvl);
|
||||
}
|
||||
|
||||
unsigned level() const { return m_level; }
|
||||
unsigned size() const { return m_size; }
|
||||
unsigned capacity() const { return m_capacity; }
|
||||
bool empty() const { return size() == 0; }
|
||||
void set_size(unsigned sz) { m_size = sz; }
|
||||
void shrink(unsigned delta) { m_size -= delta; }
|
||||
void expand(unsigned delta) { m_size += delta; }
|
||||
entry & first_entry() { SASSERT(!empty()); return get_entries()[0]; }
|
||||
entry & last_entry() { SASSERT(!empty()); return get_entries()[size() - 1]; }
|
||||
entry const & first_entry() const { SASSERT(!empty()); return get_entries()[0]; }
|
||||
entry const & last_entry() const { SASSERT(!empty()); return get_entries()[size() - 1]; }
|
||||
entry const & get(unsigned idx) const { SASSERT(idx < size()); return get_entries()[idx]; }
|
||||
entry & get(unsigned idx) { SASSERT(idx < size()); return get_entries()[idx]; }
|
||||
void set(unsigned idx, entry const & e) { SASSERT(idx < capacity()); get_entries()[idx] = e; }
|
||||
bucket * get_next(unsigned idx) const { return next_vect()[idx]; }
|
||||
void set_next(unsigned idx, bucket * bt) { SASSERT(idx < level()); next_vect()[idx] = bt; }
|
||||
};
|
||||
|
||||
// Only the header bucket has zero entries.
|
||||
bucket * m_header;
|
||||
|
||||
bucket * first_bucket() const {
|
||||
return m_header->get_next(0);
|
||||
}
|
||||
|
||||
#ifdef Z3DEBUG
|
||||
/**
|
||||
\brief (debugging only) Return the predecessor bucket of the given bucket.
|
||||
|
||||
\pre bt != m_header, and bt is a bucket of the list.
|
||||
*/
|
||||
bucket * pred_bucket(bucket * bt) const {
|
||||
SASSERT(bt != m_header);
|
||||
bucket * curr = m_header;
|
||||
while (curr->get_next(0) != bt) {
|
||||
curr = curr->get_next(0);
|
||||
SASSERT(curr != 0); // bt is not in the list
|
||||
}
|
||||
return curr;
|
||||
}
|
||||
#endif
|
||||
|
||||
bool lt(key const & k1, key const & k2) const { return Traits::lt(k1, k2); }
|
||||
|
||||
bool gt(key const & k1, key const & k2) const { return lt(k2, k1); }
|
||||
|
||||
bool geq(key const & k1, key const & k2) const { return !lt(k1, k2); }
|
||||
|
||||
bool leq(key const & k1, key const & k2) const { return !gt(k1, k2); }
|
||||
|
||||
/**
|
||||
\brief Create a new bucket of the given level.
|
||||
*/
|
||||
static bucket * mk_bucket(manager & m, unsigned lvl, unsigned capacity = Traits::max_capacity) {
|
||||
void * mem = m.allocate(bucket::get_obj_size(lvl, capacity));
|
||||
return new (mem) bucket(lvl, capacity);
|
||||
}
|
||||
|
||||
static bucket * mk_header(manager & m, unsigned lvl) {
|
||||
return mk_bucket(m, lvl, 0);
|
||||
}
|
||||
|
||||
static void inc_ref(manager & m, value const & v) {
|
||||
if (Traits::ref_count)
|
||||
m.inc_ref_eh(v);
|
||||
}
|
||||
|
||||
static void dec_ref(manager & m, value const & v) {
|
||||
if (Traits::ref_count)
|
||||
m.dec_ref_eh(v);
|
||||
}
|
||||
|
||||
/**
|
||||
\brief Invoke dec_ref_eh for each value stored in the bucket.
|
||||
*/
|
||||
static void dec_ref(manager & m, bucket * bt) {
|
||||
if (Traits::ref_count) {
|
||||
unsigned sz = bt->size();
|
||||
for (unsigned i = 0; i < sz; i++)
|
||||
m.dec_ref_eh(bt->get(i).val());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
\brief Deallocate the given bucket.
|
||||
|
||||
\remark This method invokes dec_ref_eh for each value in the bucket.
|
||||
*/
|
||||
template<bool DecRef>
|
||||
static void deallocate_bucket(manager & m, bucket * bt) {
|
||||
if (DecRef)
|
||||
dec_ref(m, bt);
|
||||
unsigned sz = bucket::get_obj_size(bt->level(), bt->capacity());
|
||||
bt->~bucket();
|
||||
m.deallocate(sz, bt);
|
||||
}
|
||||
|
||||
/**
|
||||
\brief Deallocate all buckets in the skip list.
|
||||
|
||||
\remark This method invokes dec_ref_eh for each value in the list.
|
||||
*/
|
||||
template<bool DecRef>
|
||||
void deallocate_list(manager & m) {
|
||||
bucket * curr = m_header;
|
||||
while (curr != 0) {
|
||||
bucket * old = curr;
|
||||
curr = curr->get_next(0);
|
||||
deallocate_bucket<DecRef>(m, old);
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef Z3DEBUG
|
||||
/**
|
||||
\brief Check the following property
|
||||
|
||||
for all i \in [0, b->level()) . pred_vect[i]->get_next(i) == b
|
||||
*/
|
||||
bool check_pred_vect(bucket * bt, bucket * pred_vect[]) {
|
||||
if (bt == 0)
|
||||
return true;
|
||||
for (unsigned i = 0; i < bt->level(); i++) {
|
||||
SASSERT(pred_vect[i]->get_next(i) == bt);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
|
||||
/**
|
||||
\brief Delete the given buffer and update the forward/next pointer of the buckets in pred_vect.
|
||||
|
||||
\remark This method invokes dec_ref_eh for each value in the bucket.
|
||||
*/
|
||||
void del_bucket(manager & m, bucket * bt, bucket * pred_vect[]) {
|
||||
SASSERT(check_pred_vect(bt, pred_vect));
|
||||
for (unsigned i = 0; i < bt->level(); i++)
|
||||
pred_vect[i]->set_next(i, bt->get_next(i));
|
||||
deallocate_bucket<true>(m, bt);
|
||||
}
|
||||
|
||||
/**
|
||||
\brief Update the \c pred_vect vector from levels [0, bt->level()).
|
||||
That is, bt will be now the "predecessor" for these levels.
|
||||
*/
|
||||
static void update_predecessor_vector(bucket * pred_vect [], bucket * bt) {
|
||||
unsigned lvl = bt->level();
|
||||
for (unsigned i = 0; i < lvl; i++) {
|
||||
pred_vect[i] = bt;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
\brief Similar to the previous method, but the updated vector is stored in new_pred_vect.
|
||||
*/
|
||||
void update_predecessor_vector(bucket * pred_vect[], bucket * bt, bucket * new_pred_vect[]) {
|
||||
unsigned bt_lvl = bt->level();
|
||||
for (unsigned i = 0; i < bt_lvl; i++) {
|
||||
new_pred_vect[i] = bt;
|
||||
}
|
||||
unsigned list_lvl = level();
|
||||
for (unsigned i = bt_lvl; i < list_lvl; i++) {
|
||||
new_pred_vect[i] = pred_vect[i];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
\brief Return the list level.
|
||||
*/
|
||||
unsigned level() const {
|
||||
return m_header->level();
|
||||
}
|
||||
|
||||
/**
|
||||
\brief Expand/Increase the number of levels in the header.
|
||||
*/
|
||||
void expand_header(manager & m, unsigned new_lvl) {
|
||||
SASSERT(new_lvl > level());
|
||||
bucket * new_header = mk_header(m, new_lvl);
|
||||
// copy forward pointers of the old header.
|
||||
unsigned old_lvl = level();
|
||||
for (unsigned i = 0; i < old_lvl; i++)
|
||||
new_header->set_next(i, m_header->get_next(i));
|
||||
// update header
|
||||
deallocate_bucket<false>(m, m_header);
|
||||
m_header = new_header;
|
||||
}
|
||||
|
||||
/**
|
||||
\brief Increase list level to lvl if lvl > level()
|
||||
*/
|
||||
void update_list_level(manager & m, unsigned lvl) {
|
||||
if (lvl > level()) {
|
||||
expand_header(m, lvl);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
\brief Increase list level (and store m_header in the new levels in pred_vect) if lvl > level().
|
||||
*/
|
||||
void update_list_level(manager & m, unsigned lvl, bucket * pred_vect[]) {
|
||||
if (lvl > level()) {
|
||||
bucket * old_header = m_header;
|
||||
unsigned old_lvl = m_header->level();
|
||||
expand_header(m, lvl);
|
||||
for (unsigned i = 0; i < old_lvl; i++) {
|
||||
if (pred_vect[i] == old_header)
|
||||
pred_vect[i] = m_header;
|
||||
}
|
||||
for (unsigned i = old_lvl; i < lvl; i++) {
|
||||
pred_vect[i] = m_header;
|
||||
}
|
||||
SASSERT(level() == lvl);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
\brief Add first entry to the list.
|
||||
|
||||
\remark This method will invoke inc_ref_eh for e.val()
|
||||
*/
|
||||
void insert_first_entry(manager & m, entry const & e) {
|
||||
unsigned lvl = m.random_level(Traits::max_level);
|
||||
bucket * new_bucket = mk_bucket(m, lvl, Traits::initial_capacity);
|
||||
update_list_level(m, lvl);
|
||||
for (unsigned i = 0; i < lvl; i++) {
|
||||
m_header->set_next(i, new_bucket);
|
||||
}
|
||||
inc_ref(m, e.val());
|
||||
new_bucket->set_size(1);
|
||||
new_bucket->set(0, e);
|
||||
}
|
||||
|
||||
/**
|
||||
\brief Expand the capacity of the first-bucket in a skip-list with only one bucket.
|
||||
This method assumes the capacity of the first-bucket < Traits::max_capacity
|
||||
*/
|
||||
void expand_first_bucket(manager & m) {
|
||||
bucket * f = first_bucket();
|
||||
SASSERT(f != 0);
|
||||
SASSERT(f->get_next(0) == 0);
|
||||
SASSERT(f->capacity() < Traits::max_capacity);
|
||||
unsigned old_capacity = f->capacity();
|
||||
SASSERT(old_capacity > 0);
|
||||
unsigned new_capacity = old_capacity * 2;
|
||||
if (new_capacity > Traits::max_capacity)
|
||||
new_capacity = Traits::max_capacity;
|
||||
unsigned lvl = f->level();
|
||||
bucket * new_f = mk_bucket(m, lvl, new_capacity);
|
||||
unsigned sz = f->size();
|
||||
new_f->set_size(sz);
|
||||
for (unsigned i = 0; i < sz; i++)
|
||||
new_f->set(i, f->get(i));
|
||||
for (unsigned i = 0; i < lvl; i++)
|
||||
m_header->set_next(i, new_f);
|
||||
deallocate_bucket<false>(m, f);
|
||||
SASSERT(first_bucket() == new_f);
|
||||
}
|
||||
|
||||
/**
|
||||
\brief Create a new bucket and divide the elements in bt between bt and the new bucket.
|
||||
*/
|
||||
void splice(manager & m, bucket * bt, bucket * pred_vect[]) {
|
||||
SASSERT(bt->capacity() == Traits::max_capacity);
|
||||
unsigned bt_lvl = bt->level();
|
||||
unsigned new_bucket_lvl = m.random_level(Traits::max_level);
|
||||
bucket * new_bucket = mk_bucket(m, new_bucket_lvl);
|
||||
update_list_level(m, new_bucket_lvl, pred_vect);
|
||||
unsigned _lvl = std::min(bt_lvl, new_bucket_lvl);
|
||||
for (unsigned i = 0; i < _lvl; i++) {
|
||||
new_bucket->set_next(i, bt->get_next(i));
|
||||
bt->set_next(i, new_bucket);
|
||||
}
|
||||
for (unsigned i = bt_lvl; i < new_bucket_lvl; i++) {
|
||||
new_bucket->set_next(i, pred_vect[i]->get_next(i));
|
||||
pred_vect[i]->set_next(i, new_bucket);
|
||||
}
|
||||
unsigned old_size = bt->size();
|
||||
SASSERT(old_size >= 2);
|
||||
unsigned mid = old_size/2;
|
||||
new_bucket->set_size(old_size - mid);
|
||||
unsigned i = mid;
|
||||
unsigned j = 0;
|
||||
for (; i < old_size; i++, j++) {
|
||||
new_bucket->set(j, bt->get(i));
|
||||
}
|
||||
bt->set_size(mid);
|
||||
SASSERT(!bt->empty());
|
||||
SASSERT(!new_bucket->empty());
|
||||
}
|
||||
|
||||
/**
|
||||
\brief Open space at position idx. The number of entries in bt is increased by one.
|
||||
|
||||
\remark This method will *NOT* invoke inc_ref_eh
|
||||
*/
|
||||
void open_space(bucket * bt, unsigned idx) {
|
||||
SASSERT(bt->size() < bt->capacity());
|
||||
SASSERT(idx <= bt->size());
|
||||
unsigned i = bt->size();
|
||||
while (i > idx) {
|
||||
bt->set(i, bt->get(i-1));
|
||||
i--;
|
||||
}
|
||||
bt->expand(1);
|
||||
}
|
||||
|
||||
/**
|
||||
\brief Open two spaces at position idx. The number of entries in bt is increased by one.
|
||||
|
||||
\remark This method will *NOT* invoke inc_ref_eh
|
||||
*/
|
||||
void open_2spaces(bucket * bt, unsigned idx) {
|
||||
SASSERT(bt->size() < bt->capacity() - 1);
|
||||
SASSERT(idx <= bt->size());
|
||||
unsigned i = bt->size() + 1;
|
||||
unsigned end = idx + 1;
|
||||
while (i > end) {
|
||||
bt->set(i, bt->get(i-2));
|
||||
i--;
|
||||
}
|
||||
bt->expand(2);
|
||||
}
|
||||
|
||||
/**
|
||||
\brief Delete entry at position idx.
|
||||
|
||||
\remark This method will invoke dec_ref_eh for the value stored in entry at position idx.
|
||||
*/
|
||||
void del_entry(manager & m, bucket * bt, unsigned idx) {
|
||||
SASSERT(!bt->empty());
|
||||
SASSERT(idx < bt->size());
|
||||
dec_ref(m, bt->get(idx).val());
|
||||
unsigned sz = bt->size();
|
||||
for (unsigned i = idx; i < sz - 1; i++) {
|
||||
bt->set(i, bt->get(i+1));
|
||||
}
|
||||
bt->shrink(1);
|
||||
}
|
||||
|
||||
/**
|
||||
\brief Create a copy of the skip list.
|
||||
|
||||
\remark This method will invoke inc_ref_eh for all values copied.
|
||||
*/
|
||||
void clone_core(manager & m, skip_list_base * new_list) const {
|
||||
bucket * pred_vect[Traits::max_level];
|
||||
unsigned lvl = level();
|
||||
new_list->update_list_level(m, lvl);
|
||||
bucket * new_header = new_list->m_header;
|
||||
for (unsigned i = 0; i < lvl; i++)
|
||||
pred_vect[i] = new_header;
|
||||
bucket * curr = first_bucket();
|
||||
while (curr != 0) {
|
||||
unsigned curr_lvl = curr->level();
|
||||
bucket * new_bucket = new_list->mk_bucket(m, curr_lvl, curr->capacity());
|
||||
for (unsigned i = 0; i < curr_lvl; i++) {
|
||||
pred_vect[i]->set_next(i, new_bucket);
|
||||
pred_vect[i] = new_bucket;
|
||||
}
|
||||
unsigned curr_sz = curr->size();
|
||||
for (unsigned i = 0; i < curr_sz; i++) {
|
||||
entry const & curr_entry = curr->get(i);
|
||||
inc_ref(m, curr_entry.val());
|
||||
new_bucket->set(i, curr_entry);
|
||||
}
|
||||
new_bucket->set_size(curr_sz);
|
||||
curr = curr->get_next(0);
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
skip_list_base():
|
||||
m_header(0) {
|
||||
SASSERT(Traits::max_capacity >= 2);
|
||||
SASSERT(Traits::initial_capacity >= 2);
|
||||
SASSERT(Traits::initial_capacity <= Traits::max_capacity);
|
||||
SASSERT(Traits::max_level >= 1);
|
||||
SASSERT(Traits::max_capacity <= SL_MAX_CAPACITY);
|
||||
SASSERT(Traits::max_level <= SL_MAX_LEVEL);
|
||||
}
|
||||
|
||||
skip_list_base(manager & m):
|
||||
m_header(0) {
|
||||
SASSERT(Traits::max_capacity >= 2);
|
||||
SASSERT(Traits::initial_capacity >= 2);
|
||||
SASSERT(Traits::initial_capacity <= Traits::max_capacity);
|
||||
SASSERT(Traits::max_level >= 1);
|
||||
SASSERT(Traits::max_capacity <= SL_MAX_CAPACITY);
|
||||
SASSERT(Traits::max_level <= SL_MAX_LEVEL);
|
||||
init(m);
|
||||
}
|
||||
|
||||
~skip_list_base() {
|
||||
SASSERT(m_header == 0);
|
||||
}
|
||||
|
||||
void deallocate(manager & m) {
|
||||
deallocate_list<true>(m);
|
||||
m_header = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
\brief Deallocate the list but do not invoke dec_ref_eh.
|
||||
*/
|
||||
void deallocate_no_decref(manager & m) {
|
||||
deallocate_list<false>(m);
|
||||
m_header = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
\brief Initialize a list that was created using the default constructor.
|
||||
It can be used also to initialized a list deallocated using the method #deallocate.
|
||||
*/
|
||||
void init(manager & m) {
|
||||
SASSERT(m_header == 0);
|
||||
m_header = mk_header(m, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
\brief Remove all elements from the skip-list.
|
||||
*/
|
||||
void reset(manager & m) {
|
||||
deallocate_list<true>(m);
|
||||
m_header = mk_header(m, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
\brief Remove all elements from the skip-list without invoking dec_ref_eh.
|
||||
*/
|
||||
void reset_no_decref(manager & m) {
|
||||
deallocate_list<false>(m);
|
||||
m_header = mk_header(m, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
\brief Return true if the list is empty.
|
||||
*/
|
||||
bool empty() const {
|
||||
SASSERT(m_header != 0);
|
||||
return first_bucket() == 0;
|
||||
}
|
||||
|
||||
protected:
|
||||
/**
|
||||
\brief Return the position of the bucket in the skip list.
|
||||
*/
|
||||
unsigned get_bucket_idx(bucket const * bt) const {
|
||||
bucket * curr = m_header;
|
||||
unsigned pos = 0;
|
||||
while (curr != 0) {
|
||||
if (curr == bt)
|
||||
return pos;
|
||||
pos++;
|
||||
curr = curr->get_next(0);
|
||||
}
|
||||
UNREACHABLE();
|
||||
return pos;
|
||||
}
|
||||
|
||||
/**
|
||||
\brief Display the given entry.
|
||||
*/
|
||||
void display(std::ostream & out, entry const & e) const {
|
||||
e.display(out);
|
||||
}
|
||||
|
||||
/**
|
||||
\brief Display a reference to the given bucket.
|
||||
*/
|
||||
void display_bucket_ref(std::ostream & out, bucket const * bt) const {
|
||||
if (bt == 0)
|
||||
out << "NIL";
|
||||
else
|
||||
out << "#" << get_bucket_idx(bt);
|
||||
}
|
||||
|
||||
/**
|
||||
\brief Display the predecessor vector.
|
||||
*/
|
||||
void display_predecessor_vector(std::ostream & out, bucket const * const pred_vect[]) const {
|
||||
for (unsigned i = 0; i < level(); i++) {
|
||||
out << i << ": ";
|
||||
display_bucket_ref(out, pred_vect[i]);
|
||||
if (pred_vect[i]) {
|
||||
out << " -> ";
|
||||
display_bucket_ref(out, pred_vect[i]->get_next(i));
|
||||
}
|
||||
out << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
\brief Display the successors of the given bucket.
|
||||
*/
|
||||
void display_successors(std::ostream & out, bucket const * bt) const {
|
||||
out << "[";
|
||||
for (unsigned i = 0; i < bt->level(); i++) {
|
||||
if (i > 0) out << ", ";
|
||||
display_bucket_ref(out, bt->get_next(i));
|
||||
}
|
||||
out << "]";
|
||||
}
|
||||
|
||||
/**
|
||||
\brief Display the given bucket.
|
||||
*/
|
||||
void display(std::ostream & out, bucket const * bt) const {
|
||||
if (bt == 0) {
|
||||
out << "NIL\n";
|
||||
return;
|
||||
}
|
||||
out << "bucket ";
|
||||
display_bucket_ref(out, bt);
|
||||
out << ", capacity: " << bt->capacity() << "\n";
|
||||
out << "successors: ";
|
||||
display_successors(out, bt);
|
||||
out << "\n";
|
||||
out << "entries:\n";
|
||||
for (unsigned i = 0; i < bt->size(); i++) {
|
||||
display(out, bt->get(i));
|
||||
out << "\n";
|
||||
}
|
||||
out << "----------\n";
|
||||
}
|
||||
|
||||
public:
|
||||
/**
|
||||
\brief Dump the skip list for debugging purposes.
|
||||
It assumes that key and value types implement operator <<.
|
||||
*/
|
||||
void display_physical(std::ostream & out) const {
|
||||
out << "{\nskip-list level: " << m_header->level() << "\n";
|
||||
bucket * curr = m_header;
|
||||
while (curr != 0) {
|
||||
display(out, curr);
|
||||
curr = curr->get_next(0);
|
||||
}
|
||||
out << "}\n";
|
||||
}
|
||||
|
||||
void display(std::ostream & out) const {
|
||||
bucket * curr = m_header;
|
||||
while (curr != 0) {
|
||||
unsigned sz = curr->size();
|
||||
for (unsigned i = 0; i < sz; i++) {
|
||||
if (i > 0)
|
||||
out << " ";
|
||||
curr->get(i).display(out);
|
||||
}
|
||||
curr = curr->get_next(0);
|
||||
}
|
||||
}
|
||||
|
||||
protected:
|
||||
/**
|
||||
\brief Return true if bucket b2 can be reached from b1 following get_next(i) pointers
|
||||
*/
|
||||
bool is_reachable_at_i(bucket const * bt1, bucket const * bt2, unsigned i) const {
|
||||
bucket * curr = bt1->get_next(i);
|
||||
while (curr != 0) {
|
||||
if (curr == bt2)
|
||||
return true;
|
||||
curr = curr->get_next(i);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
protected:
|
||||
static void display_size_info_core(std::ostream & out, unsigned cls_size) {
|
||||
out << "sizeof root: " << cls_size << "\n";
|
||||
out << "bucket max capacity: " << Traits::max_capacity << "\n";
|
||||
out << "bucket max level: " << Traits::max_level << "\n";
|
||||
out << "sizeof(bucket): " << sizeof(bucket) << " + " << sizeof(bucket*) << "*lvl + " << sizeof(entry) << "*capacity\n";
|
||||
out << "sizeof(usual bucket): " << (sizeof(bucket) + sizeof(entry)*Traits::max_capacity) << " + " << sizeof(bucket*) << "*lvl\n";
|
||||
out << "sizeof(max. bucket): " << (sizeof(bucket) + sizeof(entry)*Traits::max_capacity + sizeof(bucket*)*Traits::max_level) << "\n";
|
||||
out << "sizeof(entry): " << sizeof(entry) << "\n";
|
||||
out << "sizeof empty: " << cls_size + bucket::get_obj_size(1, 0) << "\n";;
|
||||
out << "sizeof singleton: ["
|
||||
<< (cls_size + bucket::get_obj_size(1, 0) + bucket::get_obj_size(1, Traits::initial_capacity)) << ", "
|
||||
<< (cls_size +
|
||||
bucket::get_obj_size(Traits::max_level, 0) +
|
||||
bucket::get_obj_size(Traits::max_level, Traits::max_capacity)) << "]\n";
|
||||
}
|
||||
|
||||
public:
|
||||
/**
|
||||
\brief Return true if skip-list has more than k buckets (not considering the header).
|
||||
|
||||
\remark This method is for debugging purposes.
|
||||
*/
|
||||
bool has_more_than_k_buckets(unsigned k) const {
|
||||
bucket * curr = first_bucket();
|
||||
while (curr != 0 && k > 0) {
|
||||
curr = curr->get_next(0);
|
||||
k--;
|
||||
}
|
||||
return curr != 0;
|
||||
}
|
||||
|
||||
/**
|
||||
\brief Return true if the skip-list has more than k entries.
|
||||
*/
|
||||
bool has_more_than_k_entries(unsigned k) const {
|
||||
bucket * curr = first_bucket();
|
||||
while (curr != 0 && k >= curr->size()) {
|
||||
k -= curr->size();
|
||||
curr = curr->get_next(0);
|
||||
}
|
||||
SASSERT(curr == 0 || curr->size() > k);
|
||||
return curr != 0;
|
||||
}
|
||||
|
||||
protected:
|
||||
/**
|
||||
\brief Return the amount of memory consumed by the list.
|
||||
*/
|
||||
unsigned memory_core(unsigned cls_size) const {
|
||||
unsigned r = 0;
|
||||
r += cls_size;
|
||||
bucket * curr = m_header;
|
||||
while (curr != 0) {
|
||||
r += bucket::get_obj_size(curr->level(), curr->capacity());
|
||||
curr = curr->get_next(0);
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
public:
|
||||
/**
|
||||
\brief Compress the buckets of the skip-list.
|
||||
Make sure that all, but the last bucket, have at least \c load entries.
|
||||
|
||||
\remark If load > Traits::max_capacity, then it assumes load = Traits::max_capacity.
|
||||
*/
|
||||
void compress(manager & m, unsigned load = Traits::max_capacity/2) {
|
||||
if (load > Traits::max_capacity)
|
||||
load = Traits::max_capacity;
|
||||
bucket * pred_vect[Traits::max_level];
|
||||
update_predecessor_vector(pred_vect, m_header);
|
||||
bucket * curr = first_bucket();
|
||||
while (curr != 0) {
|
||||
update_predecessor_vector(pred_vect, curr);
|
||||
bucket * next = curr->get_next(0);
|
||||
while (curr->size() < load && next != 0) {
|
||||
// steal entries of the successor bucket.
|
||||
unsigned deficit = load - curr->size();
|
||||
unsigned next_size = next->size();
|
||||
if (next_size <= deficit) {
|
||||
for (unsigned i = 0, j = curr->size(); i < next_size; i++, j++) {
|
||||
curr->set(j, next->get(i));
|
||||
}
|
||||
curr->expand(next_size);
|
||||
bucket * new_next = next->get_next(0);
|
||||
del_bucket(m, next, pred_vect);
|
||||
next = new_next;
|
||||
SASSERT(curr->size() <= load);
|
||||
}
|
||||
else {
|
||||
for (unsigned i = 0, j = curr->size(); i < deficit; i++, j++) {
|
||||
curr->set(j, next->get(i));
|
||||
}
|
||||
curr->expand(deficit);
|
||||
for (unsigned i = deficit, j = 0; i < next_size; i++, j++) {
|
||||
next->set(j, next->get(i));
|
||||
}
|
||||
next->set_size(next_size - deficit);
|
||||
SASSERT(curr->size() == load);
|
||||
}
|
||||
}
|
||||
curr = curr->get_next(0);
|
||||
}
|
||||
}
|
||||
|
||||
void swap(skip_list_base & other) {
|
||||
bucket * tmp = m_header;
|
||||
m_header = other.m_header;
|
||||
other.m_header = tmp;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
#endif
|
Loading…
Add table
Add a link
Reference in a new issue