Skip to main content

Map

The Map template class provides a hash table implementation for arbitrary key-value pairs.

Template parameters

K
typename
required
Key type
D
typename
required
Data (value) type
H
typename
default:"Hash<K>"
Hash functor type
E
typename
default:"Equal<K>"
Equality comparator type

Nested types

Pair
struct
Contains K key and D data members representing a key-value pair

Constructors

Map()
Map(const H& h, const E& e)
h
const H&
Hash function instance
e
const E&
Equality comparator instance

Core operations

operator[]

D& operator[](const K& k)
const D& operator[](const K& k) const
Accesses the value associated with a key. Precondition: The key must exist in the map.
k
const K&
required
Key to look up (must already exist)
This operator requires the key to already exist. Use insert() first or check with has().

insert

void insert(const K& k, const D& d)
Inserts a new key-value pair. Precondition: The key must NOT already exist.
k
const K&
required
Key to insert (must not exist)
d
const D&
required
Data to associate with the key
The map automatically rehashes when needed to maintain performance.

has

bool has(const K& k) const
Checks whether a key exists in the map.
k
const K&
required
Key to check

peek

bool peek(const K& k, D& d) const
Looks up a key and stores its value if found.
k
const K&
required
Key to look up
d
D&
required
Reference to store the value if found
Returns: true if the key exists, false otherwise

remove

void remove(const K& k)
Removes a key-value pair. Precondition: The key must exist.
k
const K&
required
Key to remove (must exist)

Utility operations

elems

int elems() const
Returns the number of elements in the map.

bucket_count

int bucket_count() const
Returns the number of hash table buckets.

clear

void clear()
Removes all elements and deallocates the hash table.

moveTo

void moveTo(Map& other)
Moves this map’s contents to another map, leaving this map empty.
other
Map&
required
Destination map
The hash and equality objects are not moved by this method.

bucket

const vec<Pair>& bucket(int i) const
Accesses a specific hash table bucket for iteration.
i
int
required
Bucket index

Hash and equality functors

MiniSat provides default hash functions for integer types:
static inline uint32_t hash(uint32_t x) { return x; }
static inline uint32_t hash(uint64_t x) { return (uint32_t)x; }
static inline uint32_t hash(int32_t x)  { return (uint32_t)x; }
static inline uint32_t hash(int64_t x)  { return (uint32_t)x; }
For pointer types, use DeepHash<K> and DeepEqual<K> to hash/compare pointed-to values.

Usage example

#include "minisat/mtl/Map.h"

using namespace Minisat;

Map<int, const char*> names;

names.insert(1, "Alice");
names.insert(2, "Bob");
names.insert(3, "Charlie");

if (names.has(2)) {
    printf("Name: %s\n", names[2]); // Prints: Name: Bob
}

names.remove(2);

IntMap

The IntMap template class provides an efficient map for keys that can be converted to small integers. It uses a vector for O(1) access.

Template parameters

K
typename
required
Key type (must be convertible to integer index)
V
typename
required
Value type
MkIndex
typename
default:"MkIndexDefault<K>"
Functor that converts keys to vector indices

Constructor

explicit IntMap(MkIndex _index = MkIndex())
_index
MkIndex
default:"MkIndex()"
Index conversion functor

Operations

has

bool has(K k) const
Checks whether a key exists (has been reserved).
k
K
required
Key to check

operator[]

V& operator[](K k)
const V& operator[](K k) const
Accesses the value for a key. Precondition: The key must have been reserved.
k
K
required
Key to access (must exist)

reserve

void reserve(K key)
void reserve(K key, V pad)
Ensures space exists for the key, growing the internal vector if needed.
key
K
required
Key to reserve space for
pad
V
Value to use for any new slots created

insert

void insert(K key, V val)
void insert(K key, V val, V pad)
Reserves space for the key and assigns the value.
key
K
required
Key to insert
val
V
required
Value to assign
pad
V
Padding value for intermediate slots

Iteration

const V* begin() const
const V* end() const
V* begin()
V* end()
Provides iterator-style access to all values in key order.

Utility methods

void clear(bool dispose = false)
void moveTo(IntMap& to)
void copyTo(IntMap& to) const
dispose
bool
default:"false"
Whether to deallocate storage
to
IntMap&
required
Destination map for move/copy

Usage example

#include "minisat/mtl/IntMap.h"

using namespace Minisat;

// Map from Var (which is just an int) to activity scores
IntMap<Var, double> activity;

Var v0 = 0, v1 = 1, v2 = 2;

activity.insert(v0, 1.0);
activity.insert(v1, 2.5);
activity.insert(v2, 0.8);

if (activity.has(v1)) {
    activity[v1] += 0.5; // Increase activity
}

IntSet

The IntSet template class provides a set implementation for integer-indexable keys.

Template parameters

K
typename
required
Key type
MkIndex
typename
default:"MkIndexDefault<K>"
Index conversion functor

Operations

insert

void insert(K k)
Adds a key to the set (no effect if already present).

has

bool has(K k)
Checks whether a key is in the set.

size

int size() const
Returns the number of elements in the set.

clear

void clear(bool free = false)
Removes all elements from the set.

toVec

const vec<K>& toVec() const
Returns the internal vector of keys.

operator[]

K operator[](int index) const
Accesses the key at the given position in insertion order.

Performance characteristics

Map

  • Insert: O(1) amortized
  • Lookup: O(1) average case
  • Remove: O(1) average case
  • Space: O(n) plus overhead for hash table buckets

IntMap

  • Insert: O(1) amortized
  • Lookup: O(1)
  • Remove: Not supported
  • Space: O(max_key) - sparse for large key ranges

Build docs developers (and LLMs) love