3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2025-08-25 20:46:01 +00:00

Merge branch 'master' into polysat

This commit is contained in:
Jakob Rath 2023-05-26 15:58:09 +02:00
commit f54f33551e
308 changed files with 6606 additions and 18485 deletions

104
src/util/distribution.h Normal file
View file

@ -0,0 +1,104 @@
/*++
Copyright (c) 2023 Microsoft Corporation
Module Name:
distribution.h
Abstract:
Probabiltiy distribution
Author:
Nikolaj Bjorner (nbjorner) 2023-4-12
Notes:
Distribution class works by pushing identifiers with associated scores.
After they have been pushed, you can access a random element using choose
or you can enumerate the elements in random order, sorted by the score probability.
Only one iterator can be active at a time because the iterator reshuffles the registered elements.
The requirement is not checked or enforced.
--*/
#pragma once
#include "vector.h"
class distribution {
random_gen m_random;
svector<std::pair<unsigned, unsigned>> m_elems;
unsigned m_sum = 0;
unsigned choose(unsigned sum) {
unsigned s = m_random(sum);
unsigned idx = 0;
for (auto const& [j, score] : m_elems) {
if (s < score)
return idx;
s -= score;
++idx;
}
UNREACHABLE();
return 0;
}
public:
distribution(unsigned seed): m_random(seed) {}
void reset() {
m_elems.reset();
m_sum = 0;
}
bool empty() const {
return m_elems.empty();
}
void push(unsigned id, unsigned score) {
SASSERT(score > 0);
if (score > 0) {
m_elems.push_back({id, score});
m_sum += score;
}
}
/**
\brief choose an element at random with probability proportional to the score
relative to the sum of scores of other.
*/
unsigned choose() {
return m_elems[choose(m_sum)].first;
}
class iterator {
distribution& d;
unsigned m_sz = 0;
unsigned m_sum = 0;
unsigned m_index = 0;
void next_index() {
if (0 != m_sz)
m_index = d.choose(m_sum);
}
public:
iterator(distribution& d, bool start): d(d), m_sz(start?d.m_elems.size():0), m_sum(d.m_sum) {
next_index();
}
unsigned operator*() const { return d.m_elems[m_index].first; }
iterator operator++() {
m_sum -= d.m_elems[m_index].second;
--m_sz;
std::swap(d.m_elems[m_index], d.m_elems[m_sz]);
next_index();
return *this;
}
bool operator==(iterator const& other) const { return m_sz == other.m_sz; }
bool operator!=(iterator const& other) const { return m_sz != other.m_sz; }
};
iterator begin() { return iterator(*this, true); }
iterator end() { return iterator(*this, false); }
};

View file

@ -87,6 +87,8 @@ void reslimit::push_child(reslimit* r) {
void reslimit::pop_child() {
lock_guard lock(*g_rlimit_mux);
m_count += m_children.back()->m_count;
m_children.back()->m_count = 0;
m_children.pop_back();
}

View file

@ -19,26 +19,28 @@ Revision History:
#pragma once
#include <cstdint>
#include "util/machine.h"
#include <cstdint>
#define TAG_SHIFT PTR_ALIGNMENT
#define ALIGNMENT_VALUE (1 << PTR_ALIGNMENT)
#define TAG_MASK (ALIGNMENT_VALUE - 1)
#define PTR_MASK (~TAG_MASK)
#define ALIGN(T, PTR) reinterpret_cast<T>(((reinterpret_cast<size_t>(PTR) >> PTR_ALIGNMENT) + \
static_cast<size_t>((reinterpret_cast<size_t>(PTR) & TAG_MASK) != 0)) << PTR_ALIGNMENT)
#define ALIGN(T, PTR) reinterpret_cast<T>(((reinterpret_cast<uintptr_t>(PTR) >> PTR_ALIGNMENT) + \
static_cast<uintptr_t>((reinterpret_cast<uintptr_t>(PTR) & TAG_MASK) != 0)) << PTR_ALIGNMENT)
#define UNTAG(T, PTR) reinterpret_cast<T>(reinterpret_cast<size_t>(PTR) & PTR_MASK)
#define UNTAG(T, PTR) reinterpret_cast<T>(reinterpret_cast<uintptr_t>(PTR) & PTR_MASK)
#define TAG(T, PTR, TAG_VAL) reinterpret_cast<T>(reinterpret_cast<size_t>(PTR) | static_cast<size_t>(TAG_VAL))
#define TAG(T, PTR, TAG_VAL) reinterpret_cast<T>(reinterpret_cast<uintptr_t>(PTR) | static_cast<uintptr_t>(TAG_VAL))
#define GET_TAG(PTR) (reinterpret_cast<size_t>(PTR) & TAG_MASK)
#define GET_TAG(PTR) (reinterpret_cast<uintptr_t>(PTR) & TAG_MASK)
#define BOXINT(T, VAL) reinterpret_cast<T>(static_cast<size_t>(VAL) << PTR_ALIGNMENT)
#define BOXINT(T, VAL) reinterpret_cast<T>(static_cast<uintptr_t>(VAL) << PTR_ALIGNMENT)
#define BOXTAGINT(T, VAL, TAG_VAL) reinterpret_cast<T>((static_cast<size_t>(VAL) << PTR_ALIGNMENT) | static_cast<size_t>(TAG_VAL))
#define BOXTAGINT(T, VAL, TAG_VAL) reinterpret_cast<T>((static_cast<uintptr_t>(VAL) << PTR_ALIGNMENT) | static_cast<uintptr_t>(TAG_VAL))
#define UNBOXINT(PTR) static_cast<int>(reinterpret_cast<size_t>(PTR) >> PTR_ALIGNMENT)
#define UNBOXINT(PTR) static_cast<int>(reinterpret_cast<uintptr_t>(PTR) >> PTR_ALIGNMENT)

View file

@ -78,7 +78,7 @@ zstring::zstring(char const* s) {
m_buffer.push_back(ch);
}
else {
m_buffer.push_back(*s);
m_buffer.push_back((unsigned char)*s);
++s;
}
}