Skip to content

Commit

Permalink
Add optionable size of std::hashmap
Browse files Browse the repository at this point in the history
  • Loading branch information
Ttimofeyka committed Jul 22, 2024
1 parent 7cd04fe commit 09113f2
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 21 deletions.
34 changes: 21 additions & 13 deletions std/map.rave
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ namespace std {
}
}

alias mapSize = 64;

int hash(void* data, int size) {
return = 5381;
int idx = 0;
Expand All @@ -25,26 +23,36 @@ namespace std {
}

if(return < 0) return = -return;
} => return % std::mapSize;
}

struct hashmap<K, V> {
std::hashmap::Entry<K, V>** table;
int length;
int mapSize;

std::hashmap<K, V> this {
std::hashmap<K, V> this;
this.mapSize = 64;
this.length = 0;
this.table = cast(std::hashmap::Entry<K, V>**)std::malloc(sizeof(std::hashmap::Entry<K, V>*) * this.mapSize);
for(int i=0; i<this.mapSize; i++) this.table[i] = null;
} => this;

std::hashmap<K, V> this(int mapSize) {
std::hashmap<K, V> this;
this.mapSize = mapSize;
this.length = 0;
this.table = cast(std::hashmap::Entry<K, V>**)std::malloc(sizeof(std::hashmap::Entry<K, V>*) * std::mapSize);
for(int i=0; i<std::mapSize; i++) this.table[i] = null;
this.table = cast(std::hashmap::Entry<K, V>**)std::malloc(sizeof(std::hashmap::Entry<K, V>*) * mapSize);
for(int i=0; i<mapSize; i++) this.table[i] = null;
} => this;

void set(K key, V value) {
@if(@hasMethod(K, hash)) {
uint index = key.hash();
uint index = key.hash() % mapSize;
};

@if(!@hasMethod(K, hash)) {
uint index = std::hash(cast(void*)&key, sizeof(K));
uint index = std::hash(cast(void*)&key, sizeof(K)) % mapSize;
};

std::hashmap::Entry<K, V>* entry = table[index];
Expand Down Expand Up @@ -85,11 +93,11 @@ namespace std {
V get(K key) {
return = cast(V)null;
@if(@hasMethod(K, hash)) {
uint index = key.hash();
uint index = key.hash() % mapSize;
};

@if(!@hasMethod(K, hash)) {
uint index = std::hash(cast(void*)&key, sizeof(K));
uint index = std::hash(cast(void*)&key, sizeof(K)) % mapSize;
};

std::hashmap::Entry<K, V>* entry = table[index];
Expand All @@ -106,11 +114,11 @@ namespace std {
bool remove(K key) {
return = false;
@if(@hasMethod(K, hash)) {
uint index = key.hash();
uint index = key.hash() % mapSize;
};

@if(!@hasMethod(K, hash)) {
uint index = std::hash(cast(void*)&key, sizeof(K));
uint index = std::hash(cast(void*)&key, sizeof(K)) % mapSize;
};

std::hashmap::Entry<K, V>* entry = table[index];
Expand All @@ -132,7 +140,7 @@ namespace std {
}

void clear {
for(int i=0; i<std::mapSize; i++) {
for(int i=0; i<mapSize; i++) {
std::hashmap::Entry<K, V>* entry = table[i];
std::hashmap::Entry<K, V>* old = null;
while(entry != null) {
Expand All @@ -147,7 +155,7 @@ namespace std {

void ~this {
if(this.table != null) {
for(int i=0; i<std::mapSize; i++) {
for(int i=0; i<this.mapSize; i++) {
std::hashmap::Entry<K, V>* entry = this.table[i];
std::hashmap::Entry<K, V>* old = null;
while(entry != null) {
Expand Down
2 changes: 1 addition & 1 deletion std/matrix.rave
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ namespace std {
return = 1295;
for(int i=0; i<(this.rows * this.columns); i++) return = ((return <. 5) + return) + cast(int)(this.data[i]);
if(return < 0) return = -return;
} => return * std::mapSize;
}

void ~this {
if(this.data != null) {
Expand Down
2 changes: 1 addition & 1 deletion std/memory.rave
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,6 @@ namespace std {
@if(!@hasMethod(P2, hash)) {
return *= std::hash(cast(void*)this.second, sizeof(P2));
};
} => return % std::mapSize;
}
}
}
2 changes: 1 addition & 1 deletion std/string.rave
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ namespace std {
}

if(return < 0) return = -return;
} => return % std::mapSize;
}
}

namespace string {
Expand Down
4 changes: 2 additions & 2 deletions std/thread.rave
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import <std/process> <std/map>

int join => pthread::join(this.id, cast(void**)null);

int hash => (9437 * cast(int)(this.id)) % std::mapSize;
int hash => (9437 * cast(int)(this.id));
}
}
};
Expand Down Expand Up @@ -52,7 +52,7 @@ import <std/process> <std/map>

int join => std::thread::__WaitForSingleObject(this.__ptr, -1);

int hash => (9437 * this.id) % std::mapSize;
int hash => (9437 * this.id);
}
}
};
Expand Down
2 changes: 1 addition & 1 deletion std/utf32.rave
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ namespace std {
}

if(return < 0) return = -return;
} => return % std::mapSize;
}

//bool operator==(std::u32string one, std::u32string two) => one.compare(two);
//bool operator==(std::u32string one, uint* two) => one.compare(two);
Expand Down
2 changes: 1 addition & 1 deletion std/utf8.rave
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ namespace std {
}

if(return < 0) return = -return;
} => return % std::mapSize;
}
}

namespace u8string {
Expand Down
3 changes: 2 additions & 1 deletion std/vector.rave
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ namespace std {
for(int i=0; i<this.length; i++) {
return = return * i + 7588 + (return <. 6) + (return >. 2);
}
} => return % std::mapSize;
if(return < 0) return = -return;
}
}
}

0 comments on commit 09113f2

Please sign in to comment.