From 085c54b980a2f62c7c475d32b5d0ce9c6f97904f Mon Sep 17 00:00:00 2001 From: Frank Busse Date: Mon, 9 Dec 2019 11:12:14 +0000 Subject: DiscretePDF: use IDs instead of pointers (see PR #739) --- include/klee/ADT/DiscretePDF.h | 12 +++--- include/klee/ADT/DiscretePDF.inc | 92 ++++++++++++++++++++-------------------- 2 files changed, 54 insertions(+), 50 deletions(-) (limited to 'include') diff --git a/include/klee/ADT/DiscretePDF.h b/include/klee/ADT/DiscretePDF.h index 74a1ce9e..713b46c1 100644 --- a/include/klee/ADT/DiscretePDF.h +++ b/include/klee/ADT/DiscretePDF.h @@ -10,8 +10,10 @@ #ifndef KLEE_DISCRETEPDF_H #define KLEE_DISCRETEPDF_H +#include + namespace klee { - template + template > class DiscretePDF { // not perfectly parameterized, but float/double/int should work ok, // although it would be better to have choose argument range from 0 @@ -28,21 +30,21 @@ namespace klee { void remove(T item); bool inTree(T item); weight_type getWeight(T item); - + /* pick a tree element according to its * weight. p should be in [0,1). */ T choose(double p); - + private: class Node; Node *m_root; - + Node **lookup(T item, Node **parent_out); void split(Node *node); void rotate(Node *node); void lengthen(Node *node); - void propogateSumsUp(Node *n); + void propagateSumsUp(Node *n); }; } diff --git a/include/klee/ADT/DiscretePDF.inc b/include/klee/ADT/DiscretePDF.inc index 743a69b5..7b23b69b 100644 --- a/include/klee/ADT/DiscretePDF.inc +++ b/include/klee/ADT/DiscretePDF.inc @@ -10,8 +10,8 @@ #include namespace klee { -template -class DiscretePDF::Node +template +class DiscretePDF::Node { private: bool m_mark; @@ -41,8 +41,8 @@ public: /// -template -DiscretePDF::Node::Node(T key_, weight_type weight_, Node *parent_) { +template +DiscretePDF::Node::Node(T key_, weight_type weight_, Node *parent_) { m_mark = false; key = key_; @@ -52,31 +52,32 @@ DiscretePDF::Node::Node(T key_, weight_type weight_, Node *parent_) { parent = parent_; } -template -DiscretePDF::Node::~Node() { +template +DiscretePDF::Node::~Node() { delete left; delete right; } // -template -DiscretePDF::DiscretePDF() { +template +DiscretePDF::DiscretePDF() { m_root = 0; } -template -DiscretePDF::~DiscretePDF() { +template +DiscretePDF::~DiscretePDF() { delete m_root; } -template -bool DiscretePDF::empty() const { +template +bool DiscretePDF::empty() const { return m_root == 0; } -template -void DiscretePDF::insert(T item, weight_type weight) { +template +void DiscretePDF::insert(T item, weight_type weight) { + Comparator lessThan; Node *p=0, *n=m_root; while (n) { @@ -87,7 +88,7 @@ void DiscretePDF::insert(T item, weight_type weight) { if (n->key==item) { assert(0 && "insert: argument(item) already in tree"); } else { - n = (itemkey)?n->left:n->right; + n = lessThan(item, n->key) ? n->left : n->right; } } @@ -96,7 +97,7 @@ void DiscretePDF::insert(T item, weight_type weight) { if (!p) { m_root = n; } else { - if (itemkey) { + if (lessThan(item, p->key)) { p->left = n; } else { p->right = n; @@ -105,11 +106,11 @@ void DiscretePDF::insert(T item, weight_type weight) { split(n); } - propogateSumsUp(n); + propagateSumsUp(n); } -template -void DiscretePDF::remove(T item) { +template +void DiscretePDF::remove(T item) { Node **np = lookup(item, 0); Node *child, *n = *np; @@ -142,27 +143,27 @@ void DiscretePDF::remove(T item) { } } - propogateSumsUp(n->parent); + propagateSumsUp(n->parent); n->left = n->right = 0; delete n; } } -template -void DiscretePDF::update(T item, weight_type weight) { +template +void DiscretePDF::update(T item, weight_type weight) { Node *n = *lookup(item, 0); if (!n) { assert(0 && "update: argument(item) not in tree"); } else { n->weight = weight; - propogateSumsUp(n); + propagateSumsUp(n); } } -template -T DiscretePDF::choose(double p) { +template +T DiscretePDF::choose(double p) { assert (!((p < 0.0) || (p >= 1.0)) && "choose: argument(p) outside valid range"); if (!m_root) @@ -174,10 +175,10 @@ T DiscretePDF::choose(double p) { while (1) { if (n->left) { if (wleft->sumWeights) { - n = n->left; - continue; + n = n->left; + continue; } else { - w -= n->left->sumWeights; + w -= n->left->sumWeights; } } if (wweight || !n->right) { @@ -190,15 +191,15 @@ T DiscretePDF::choose(double p) { return n->key; } -template -bool DiscretePDF::inTree(T item) { +template +bool DiscretePDF::inTree(T item) { Node *n = *lookup(item, 0); return !!n; } -template -typename DiscretePDF::weight_type DiscretePDF::getWeight(T item) { +template +typename DiscretePDF::weight_type DiscretePDF::getWeight(T item) { Node *n = *lookup(item, 0); assert(n); return n->weight; @@ -206,9 +207,10 @@ typename DiscretePDF::weight_type DiscretePDF::getWeight(T item) { // -template -typename DiscretePDF::Node ** -DiscretePDF::lookup(T item, Node **parent_out) { +template +typename DiscretePDF::Node ** +DiscretePDF::lookup(T item, Node **parent_out) { + Comparator lessThan; Node *n, *p=0, **np=&m_root; while ((n = *np)) { @@ -216,7 +218,7 @@ DiscretePDF::lookup(T item, Node **parent_out) { break; } else { p = n; - if (itemkey) { + if (lessThan(item, n->key)) { np = &n->left; } else { np = &n->right; @@ -229,8 +231,8 @@ DiscretePDF::lookup(T item, Node **parent_out) { return np; } -template -void DiscretePDF::split(Node *n) { +template +void DiscretePDF::split(Node *n) { if (n->left) n->left->markBlack(); if (n->right) n->right->markBlack(); @@ -255,8 +257,8 @@ void DiscretePDF::split(Node *n) { } } -template -void DiscretePDF::rotate(Node *n) { +template +void DiscretePDF::rotate(Node *n) { Node *p=n->parent, *pp=p->parent; n->parent = pp; @@ -274,7 +276,7 @@ void DiscretePDF::rotate(Node *n) { n->setSum(); p->setSum(); - + if (!pp) { m_root = n; } else { @@ -286,8 +288,8 @@ void DiscretePDF::rotate(Node *n) { } } -template -void DiscretePDF::lengthen(Node *n) { +template +void DiscretePDF::lengthen(Node *n) { if (!n->isBlack()) { n->markBlack(); } else if (n->parent) { @@ -337,8 +339,8 @@ void DiscretePDF::lengthen(Node *n) { } } -template -void DiscretePDF::propogateSumsUp(Node *n) { +template +void DiscretePDF::propagateSumsUp(Node *n) { for (; n; n=n->parent) n->setSum(); } -- cgit 1.4.1