Skip to content

Commit

Permalink
Use template for key type
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Marr <[email protected]>
  • Loading branch information
smarr committed Jan 12, 2024
1 parent ca4e29a commit 9f8c376
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 56 deletions.
15 changes: 8 additions & 7 deletions benchmarks/C++/src/deltablue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ const Sym Strength::DEFAULT(5);
const Sym Strength::WEAK_DEFAULT(6);
const Sym Strength::ABSOLUTE_WEAKEST(7);

IdentityDictionary<int32_t>* Strength::_strengthTable;
IdentityDictionary<const Strength*>* Strength::_strengthConstant;
IdentityDictionary<const Sym, int32_t>* Strength::_strengthTable;
IdentityDictionary<const Sym, const Strength*>* Strength::_strengthConstant;
const Strength* Strength::_absoluteWeakest;
const Strength* Strength::_required;

Expand All @@ -28,8 +28,8 @@ const Strength* Strength::required() {
return _required;
}

IdentityDictionary<int32_t>* Strength::createStrengthTable() {
auto* strengthTable = new IdentityDictionary<int32_t>();
IdentityDictionary<const Sym, int32_t>* Strength::createStrengthTable() {
auto* strengthTable = new IdentityDictionary<const Sym, int32_t>();
strengthTable->atPut(&Strength::ABSOLUTE_STRONGEST, -10000);
strengthTable->atPut(&Strength::REQUIRED, -800);
strengthTable->atPut(&Strength::STRONG_PREFERRED, -600);
Expand All @@ -41,10 +41,11 @@ IdentityDictionary<int32_t>* Strength::createStrengthTable() {
return strengthTable;
}

IdentityDictionary<const Strength*>* Strength::createStrengthConstants() {
auto* strengthConstant = new IdentityDictionary<const Strength*>();
IdentityDictionary<const Sym, const Strength*>*
Strength::createStrengthConstants() {
auto* strengthConstant = new IdentityDictionary<const Sym, const Strength*>();
auto* keys = _strengthTable->getKeys();
keys->forEach([&strengthConstant](const CustomHash* const key) -> void {
keys->forEach([&strengthConstant](const Sym* const key) -> void {
const Sym* keySym = dynamic_cast<const Sym*>(key);
strengthConstant->atPut(keySym, new Strength(keySym));
});
Expand Down
14 changes: 7 additions & 7 deletions benchmarks/C++/src/deltablue.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@

enum Direction { FORWARD, BACKWARD, NONE };

class Sym : public CustomHash {
class Sym {
private:
uint32_t hash;

public:
explicit constexpr Sym(uint32_t hash_value) noexcept : hash(hash_value) {}
~Sym() override = default;

[[nodiscard]] uint32_t customHash() const override { return hash; }
[[nodiscard]] uint32_t customHash() const { return hash; }
};

class Strength {
Expand All @@ -36,8 +35,9 @@ class Strength {
int32_t const _arithmeticValue;
const Sym* const _symbolicValue;

static IdentityDictionary<int32_t>* createStrengthTable();
static IdentityDictionary<const Strength*>* createStrengthConstants();
static IdentityDictionary<const Sym, int32_t>* createStrengthTable();
static IdentityDictionary<const Sym, const Strength*>*
createStrengthConstants();
static void releaseStrengthConstants();

public:
Expand Down Expand Up @@ -79,8 +79,8 @@ class Strength {
static const Strength* _absoluteWeakest;
static const Strength* _required;

static IdentityDictionary<int32_t>* _strengthTable;
static IdentityDictionary<const Strength*>* _strengthConstant;
static IdentityDictionary<const Sym, int32_t>* _strengthTable;
static IdentityDictionary<const Sym, const Strength*>* _strengthConstant;
};

class Planner;
Expand Down
14 changes: 8 additions & 6 deletions benchmarks/C++/src/havlak.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,21 @@

using std::cout;

class BasicBlock : public CustomHash {
class BasicBlock {
private:
Vector<BasicBlock*> _inEdges{2};
Vector<BasicBlock*> _outEdges{2};
uint32_t _name;

public:
explicit BasicBlock(int32_t name) : _name(name) {}

Vector<BasicBlock*>& getInEdges() { return _inEdges; }
Vector<BasicBlock*>& getOutEdges() { return _outEdges; }
int32_t getNumPred() { return static_cast<int32_t>(_inEdges.size()); }
void addOutEdge(BasicBlock* to) { _outEdges.append(to); }
void addInEdge(BasicBlock* from) { _inEdges.append(from); }
[[nodiscard]] uint32_t customHash() const override { return _name; }
[[nodiscard]] uint32_t customHash() const { return _name; }

bool equal(BasicBlock* other) const { return _name == other->_name; }
};
Expand Down Expand Up @@ -209,8 +210,9 @@ class UnionFindNode {
}

// Path Compression, all nodes' parents point to the 1st level parent.
nodeList.forEach(
[this](UnionFindNode* const& iter) -> void { iter->unionSet(_parent); });
nodeList.forEach([this](UnionFindNode* const& iter) -> void {
iter->unionSet(_parent);
});
return node;
}

Expand Down Expand Up @@ -241,7 +243,7 @@ class HavlakLoopFinder {

Vector<Set<int32_t>*> _nonBackPreds{};
Vector<Vector<int32_t>*> _backPreds{};
IdentityDictionary<int32_t> _number{};
IdentityDictionary<BasicBlock, int32_t> _number{};

int32_t _maxSize{0};
int32_t* _header{nullptr};
Expand Down Expand Up @@ -343,7 +345,7 @@ class HavlakLoopFinder {
_number.atPut(currentNode, current);

int32_t lastId = current;
Vector<BasicBlock*>& outerBlocks = currentNode->getOutEdges();
const Vector<BasicBlock*>& outerBlocks = currentNode->getOutEdges();

for (int32_t i = 0; i < static_cast<int32_t>(outerBlocks.size()); i += 1) {
BasicBlock* target = *outerBlocks.at(i);
Expand Down
41 changes: 17 additions & 24 deletions benchmarks/C++/src/som/dictionary.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,7 @@
#include <memory>
#include "vector.h"

class CustomHash {
public:
CustomHash() = default;
virtual ~CustomHash() = default;
[[nodiscard]] virtual uint32_t customHash() const = 0;
};

template <typename V>
template <typename K, typename V>
class IdentityDictionary;

/**
Expand All @@ -19,31 +12,31 @@ class IdentityDictionary;
* The memory of Entry objects is managed by ownership through the _buckets
* field. Thus, they are anchored in the _buckets, and only freed from there.
*/
template <typename V>
template <typename K, typename V>
class Dictionary {
friend class IdentityDictionary<V>;
friend class IdentityDictionary<K, V>;

private:
class Entry {
friend class IdentityDictionary<V>;
friend class Dictionary<V>;
friend class IdentityDictionary<K, V>;
friend class Dictionary<K, V>;

private:
uint32_t _hash;
const CustomHash* const _key;
const K* const _key;
V _value;
Entry* _next;

public:
Entry(uint32_t h, const CustomHash* k, const V& v, Entry* n)
Entry(uint32_t h, const K* k, const V& v, Entry* n)
: _hash(h), _key(k), _value(v), _next(n) {}
virtual ~Entry() = default;

virtual bool match(uint32_t h, const CustomHash* const k) {
virtual bool match(uint32_t h, const K* const k) {
return _hash == h && _key == k;
}

[[nodiscard]] const CustomHash* getKey() const { return _key; }
[[nodiscard]] const K* getKey() const { return _key; }

[[nodiscard]] uint32_t getHash() const { return _hash; }
};
Expand All @@ -67,15 +60,15 @@ class Dictionary {

[[nodiscard]] bool isEmpty() const { return _size == 0; }

[[nodiscard]] uint32_t hash(const CustomHash* key) const {
[[nodiscard]] uint32_t hash(const K* key) const {
if (key == nullptr) {
return 0;
}
const uint32_t h = key->customHash();
return h ^ (h >> 16U);
}

[[nodiscard]] bool containsKey(const CustomHash* key) const {
[[nodiscard]] bool containsKey(const K* key) const {
uint32_t h = hash(key);
Entry* e = getBucket(h);

Expand All @@ -88,7 +81,7 @@ class Dictionary {
return false;
}

V* at(const CustomHash* key) const {
V* at(const K* key) const {
const uint32_t h = this->hash(key);
Entry* e = getBucket(h);

Expand All @@ -102,7 +95,7 @@ class Dictionary {
return nullptr;
}

void atPut(const CustomHash* const key, const V& value) {
void atPut(const K* const key, const V& value) {
const uint32_t h = hash(key);
const uint32_t i = getBucketIdx(h);

Expand Down Expand Up @@ -134,8 +127,8 @@ class Dictionary {
_size = 0;
}

[[nodiscard]] Vector<const CustomHash*>* getKeys() {
auto* keys = new Vector<const CustomHash*>();
[[nodiscard]] Vector<const K*>* getKeys() {
auto* keys = new Vector<const K*>();
for (uint32_t i = 0; i < _capacity; i += 1) {
Entry* current = _buckets[i];
while (current != nullptr) {
Expand Down Expand Up @@ -169,7 +162,7 @@ class Dictionary {
}
}

virtual Entry* newEntry(const CustomHash* key, V value, uint32_t hash) {
virtual Entry* newEntry(const K* key, V value, uint32_t hash) {
return new Entry(hash, key, value, nullptr);
}

Expand All @@ -182,7 +175,7 @@ class Dictionary {
return _buckets[getBucketIdx(hash)];
}

void insertBucketEntry(const CustomHash* key,
void insertBucketEntry(const K* key,
const V& value,
uint32_t hash,
Entry* head) {
Expand Down
24 changes: 12 additions & 12 deletions benchmarks/C++/src/som/identity_dictionary.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,30 @@
#include <memory>
#include "dictionary.h"

template <typename V>
class IdentityDictionary : public Dictionary<V> {
template <typename K, typename V>
class IdentityDictionary : public Dictionary<K, V> {
private:
class IdEntry : public Dictionary<V>::Entry {
class IdEntry : public Dictionary<K, V>::Entry {
public:
IdEntry(uint32_t hash,
const CustomHash* key,
const K* key,
const V& value,
typename Dictionary<V>::Entry* next)
: Dictionary<V>::Entry(hash, key, value, next) {}
typename Dictionary<K, V>::Entry* next)
: Dictionary<K, V>::Entry(hash, key, value, next) {}
~IdEntry() override = default;

bool match(uint32_t h, const CustomHash* k) override {
bool match(uint32_t h, const K* k) override {
return this->getHash() == h && this->getKey() == k;
}
};

public:
explicit IdentityDictionary(const uint32_t size) : Dictionary<V>(size) {}
IdentityDictionary() : Dictionary<V>(Dictionary<V>::INITIAL_CAPACITY) {}
explicit IdentityDictionary(const uint32_t size) : Dictionary<K, V>(size) {}
IdentityDictionary() : Dictionary<K, V>(Dictionary<K, V>::INITIAL_CAPACITY) {}

typename Dictionary<V>::Entry* newEntry(const CustomHash* key,
V value,
uint32_t hash) override {
typename Dictionary<K, V>::Entry* newEntry(const K* key,
V value,
uint32_t hash) override {
return new IdEntry(hash, key, value, nullptr);
}
};

0 comments on commit 9f8c376

Please sign in to comment.