CIRCT 23.0.0git
Loading...
Searching...
No Matches
Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
circt::IndexedMaxHeap< T, ScoreFn > Class Template Reference

A max-heap of ids into caller-owned storage. More...

#include <SATSolver.h>

Collaboration diagram for circt::IndexedMaxHeap< T, ScoreFn >:
Collaboration graph
[legend]

Public Member Functions

 IndexedMaxHeap (llvm::SmallVectorImpl< T > &storage)
 
void resize (unsigned size)
 
bool empty () const
 
bool contains (unsigned id) const
 
void clear ()
 Remove all heap entries while keeping the underlying storage untouched.
 
void insert (unsigned id)
 Insert id if it is not already present.
 
void increase (unsigned id)
 Restore heap order after the score for an existing id increased.
 
unsigned pop ()
 Remove and return the highest-scoring id.
 

Private Member Functions

double score (unsigned id) const
 Compute the ordering score for one heap entry.
 
void swapHeapEntries (unsigned lhs, unsigned rhs)
 Swap two heap slots and keep the reverse index in sync.
 
void siftUp (unsigned index)
 Bubble one entry toward the root until the heap property is restored.
 
void siftDown (unsigned index)
 Push one entry toward the leaves until the heap property is restored.
 

Private Attributes

llvm::SmallVectorImpl< T > & storage
 The caller-owned objects indexed by heap ids.
 
llvm::SmallVector< unsigned, 0 > heap
 Binary heap of ids ordered by descending score.
 
llvm::SmallVector< unsigned, 0 > positions
 Reverse map from id to heap slot, or InvalidIndex if absent.
 

Static Private Attributes

static constexpr unsigned kInvalidIndex = ~0u
 

Detailed Description

template<typename T, typename ScoreFn = HeapScore<T>>
class circt::IndexedMaxHeap< T, ScoreFn >

A max-heap of ids into caller-owned storage.

The heap stores only integer ids and keeps a reverse map from each id to its heap slot. That lets callers update an existing entry without creating duplicates. Scores come from a stateless policy type. If a caller changes an element in storage in a way that raises its score, it must call increase(id) to restore heap order.

This data structure design is inspired by MiniSat's minisat/mtl/Heap.h.

Definition at line 33 of file SATSolver.h.

Constructor & Destructor Documentation

◆ IndexedMaxHeap()

template<typename T , typename ScoreFn = HeapScore<T>>
circt::IndexedMaxHeap< T, ScoreFn >::IndexedMaxHeap ( llvm::SmallVectorImpl< T > &  storage)
inlineexplicit

Definition at line 37 of file SATSolver.h.

Member Function Documentation

◆ clear()

template<typename T , typename ScoreFn = HeapScore<T>>
void circt::IndexedMaxHeap< T, ScoreFn >::clear ( )
inline

Remove all heap entries while keeping the underlying storage untouched.

Definition at line 52 of file SATSolver.h.

References circt::IndexedMaxHeap< T, ScoreFn >::heap, circt::IndexedMaxHeap< T, ScoreFn >::kInvalidIndex, and circt::IndexedMaxHeap< T, ScoreFn >::positions.

◆ contains()

template<typename T , typename ScoreFn = HeapScore<T>>
bool circt::IndexedMaxHeap< T, ScoreFn >::contains ( unsigned  id) const
inline

◆ empty()

template<typename T , typename ScoreFn = HeapScore<T>>
bool circt::IndexedMaxHeap< T, ScoreFn >::empty ( ) const
inline

◆ increase()

template<typename T , typename ScoreFn = HeapScore<T>>
void circt::IndexedMaxHeap< T, ScoreFn >::increase ( unsigned  id)
inline

Restore heap order after the score for an existing id increased.

This only moves the entry upward. Callers should use this when the score policy is monotonic in the "better" direction, such as SAT variable activity bumps. Mutating a stored element without calling increase(id) leaves the heap order stale.

Definition at line 76 of file SATSolver.h.

References circt::IndexedMaxHeap< T, ScoreFn >::contains(), circt::IndexedMaxHeap< T, ScoreFn >::positions, and circt::IndexedMaxHeap< T, ScoreFn >::siftUp().

◆ insert()

template<typename T , typename ScoreFn = HeapScore<T>>
void circt::IndexedMaxHeap< T, ScoreFn >::insert ( unsigned  id)
inline

Insert id if it is not already present.

This heap treats ids as stable identities; duplicates are ignored instead of creating multiple entries for the same object.

Definition at line 61 of file SATSolver.h.

References circt::IndexedMaxHeap< T, ScoreFn >::contains(), circt::IndexedMaxHeap< T, ScoreFn >::heap, circt::IndexedMaxHeap< T, ScoreFn >::positions, circt::IndexedMaxHeap< T, ScoreFn >::resize(), and circt::IndexedMaxHeap< T, ScoreFn >::siftUp().

◆ pop()

template<typename T , typename ScoreFn = HeapScore<T>>
unsigned circt::IndexedMaxHeap< T, ScoreFn >::pop ( )
inline

◆ resize()

template<typename T , typename ScoreFn = HeapScore<T>>
void circt::IndexedMaxHeap< T, ScoreFn >::resize ( unsigned  size)
inline

◆ score()

template<typename T , typename ScoreFn = HeapScore<T>>
double circt::IndexedMaxHeap< T, ScoreFn >::score ( unsigned  id) const
inlineprivate

Compute the ordering score for one heap entry.

Definition at line 103 of file SATSolver.h.

References circt::IndexedMaxHeap< T, ScoreFn >::storage.

Referenced by circt::IndexedMaxHeap< T, ScoreFn >::siftDown(), and circt::IndexedMaxHeap< T, ScoreFn >::siftUp().

◆ siftDown()

template<typename T , typename ScoreFn = HeapScore<T>>
void circt::IndexedMaxHeap< T, ScoreFn >::siftDown ( unsigned  index)
inlineprivate

Push one entry toward the leaves until the heap property is restored.

Definition at line 127 of file SATSolver.h.

References assert(), circt::IndexedMaxHeap< T, ScoreFn >::heap, circt::IndexedMaxHeap< T, ScoreFn >::score(), and circt::IndexedMaxHeap< T, ScoreFn >::swapHeapEntries().

Referenced by circt::IndexedMaxHeap< T, ScoreFn >::pop().

◆ siftUp()

template<typename T , typename ScoreFn = HeapScore<T>>
void circt::IndexedMaxHeap< T, ScoreFn >::siftUp ( unsigned  index)
inlineprivate

◆ swapHeapEntries()

template<typename T , typename ScoreFn = HeapScore<T>>
void circt::IndexedMaxHeap< T, ScoreFn >::swapHeapEntries ( unsigned  lhs,
unsigned  rhs 
)
inlineprivate

Member Data Documentation

◆ heap

template<typename T , typename ScoreFn = HeapScore<T>>
llvm::SmallVector<unsigned, 0> circt::IndexedMaxHeap< T, ScoreFn >::heap
private

◆ kInvalidIndex

template<typename T , typename ScoreFn = HeapScore<T>>
constexpr unsigned circt::IndexedMaxHeap< T, ScoreFn >::kInvalidIndex = ~0u
staticconstexprprivate

◆ positions

template<typename T , typename ScoreFn = HeapScore<T>>
llvm::SmallVector<unsigned, 0> circt::IndexedMaxHeap< T, ScoreFn >::positions
private

◆ storage

template<typename T , typename ScoreFn = HeapScore<T>>
llvm::SmallVectorImpl<T>& circt::IndexedMaxHeap< T, ScoreFn >::storage
private

The caller-owned objects indexed by heap ids.

Definition at line 146 of file SATSolver.h.

Referenced by circt::IndexedMaxHeap< T, ScoreFn >::score().


The documentation for this class was generated from the following file: