Skip to content

Commit

Permalink
Switch to unordered map for the field
Browse files Browse the repository at this point in the history
  • Loading branch information
cwesson committed May 10, 2023
1 parent de6c6ca commit cd6d7f0
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/FungeMultiverse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ FungeUniverse* FungeMultiverse::operator[](std::string name){
return nullptr;
}

std::map<std::string, FungeUniverse*>::const_iterator FungeMultiverse::cbegin() const{
std::unordered_map<std::string, FungeUniverse*>::const_iterator FungeMultiverse::cbegin() const{
return universes.cbegin();
}
std::map<std::string, FungeUniverse*>::const_iterator FungeMultiverse::cend() const{
std::unordered_map<std::string, FungeUniverse*>::const_iterator FungeMultiverse::cend() const{
return universes.cend();
}

Expand Down
8 changes: 8 additions & 0 deletions src/Vector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,12 @@ std::istream& operator>>(std::istream& is, Vector& rhs){
return is;
}

size_t Vector::hash::operator()(const Funge::Vector& v) const noexcept{
size_t hash = 0;
for(dim_t i : v.values){
hash = (hash << 8) ^ i;
}
return hash;
}

}
1 change: 1 addition & 0 deletions src/fingerprint/include/FingerprintBASE.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#pragma once

#include "Fingerprint.h"
#include <map>
#include <string>

namespace Funge {
Expand Down
2 changes: 1 addition & 1 deletion src/include/Defunge.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Defunge {
std::recursive_mutex mutex;
FungeDebugger* debugger;
FungeRunner* runner;
std::map<std::string, debugger_command_t> cmdMap;
std::unordered_map<std::string, debugger_command_t> cmdMap;

Defunge();

Expand Down
4 changes: 2 additions & 2 deletions src/include/Field.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "Vector.h"
#include "funge_types.h"
#include <iostream>
#include <map>
#include <unordered_map>
#include <vector>
#include <functional>

Expand Down Expand Up @@ -124,7 +124,7 @@ class Field {
friend std::ostream& operator<<(std::ostream& os, const Field& rhs);

private:
std::map<const Vector, inst_t> field;
std::unordered_map<const Vector, inst_t, Vector::hash> field;
std::vector<dim_t> maxs;
std::vector<dim_t> mins;
std::vector<inst_t> planes;
Expand Down
8 changes: 4 additions & 4 deletions src/include/FungeMultiverse.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#pragma once

#include "FungeUniverse.h"
#include <map>
#include <unordered_map>
#include <mutex>

namespace Funge {
Expand Down Expand Up @@ -62,8 +62,8 @@ class FungeMultiverse {
*/
FungeUniverse* operator[](std::string name);

std::map<std::string, FungeUniverse*>::const_iterator cbegin() const;
std::map<std::string, FungeUniverse*>::const_iterator cend() const;
std::unordered_map<std::string, FungeUniverse*>::const_iterator cbegin() const;
std::unordered_map<std::string, FungeUniverse*>::const_iterator cend() const;

/**
* Get the size of the multiverse.
Expand All @@ -73,7 +73,7 @@ class FungeMultiverse {

private:
FungeUniverse* prime;
std::map<std::string, FungeUniverse*> universes;
std::unordered_map<std::string, FungeUniverse*> universes;
mutable std::mutex mutex;

FungeMultiverse();
Expand Down
5 changes: 5 additions & 0 deletions src/include/Vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ class Vector {
* Input stream operator.
*/
friend std::istream& operator>>(std::istream& is, Vector& rhs);

struct hash{
size_t operator()(const Funge::Vector& v) const noexcept;
};


private:
std::vector<dim_t> values;
Expand Down

0 comments on commit cd6d7f0

Please sign in to comment.