mirror of
https://github.com/Yours3lf/rpi-vk-driver.git
synced 2024-11-28 10:24:15 +01:00
added simple map implementation for descriptor sets
This commit is contained in:
parent
9596275498
commit
0203426f6b
65
driver/map.c
Normal file
65
driver/map.c
Normal file
@ -0,0 +1,65 @@
|
||||
#include "map.h"
|
||||
|
||||
uint32_t getIndex(uint32_t key, uint32_t maxData)
|
||||
{
|
||||
//multiplicative hash
|
||||
//with power of 2 W
|
||||
uint32_t a = 0x678DDE6F;
|
||||
return ((a * key) >> (32 - maxData)) % maxData;
|
||||
}
|
||||
|
||||
void* getMapElement(map m, uint32_t key)
|
||||
{
|
||||
assert(m.maxData > 0);
|
||||
assert(m.elements);
|
||||
uint32_t index = getIndex(key, m.maxData);
|
||||
//linear open addressing
|
||||
while(m.elements[index].key != key && m.elements[index].data != 0){index = (index + 1) % m.maxData;}
|
||||
return m.elements[index].data;
|
||||
}
|
||||
|
||||
void setMapElement(map* m, uint32_t key, void* data)
|
||||
{
|
||||
assert(m);
|
||||
assert(m->elements);
|
||||
assert(m->maxData > 0);
|
||||
uint32_t index = getIndex(key, m->maxData);
|
||||
while(m->elements[index].key != key && m->elements[index].data != 0){index = (index + 1) % m->maxData;}
|
||||
m->elements[index].data = data;
|
||||
m->elements[index].key = key;
|
||||
}
|
||||
|
||||
void deleteMapElement(map* m, uint32_t key)
|
||||
{
|
||||
assert(m);
|
||||
assert(m->elements);
|
||||
assert(m->maxData > 0);
|
||||
uint32_t index = getIndex(key, m->maxData);
|
||||
while(m->elements[index].key != key){++index;}
|
||||
m->elements[index].data = 0;
|
||||
}
|
||||
|
||||
map createMap(void* buf, uint32_t maxData)
|
||||
{
|
||||
map m =
|
||||
{
|
||||
.elements = buf,
|
||||
.maxData = maxData
|
||||
};
|
||||
|
||||
//lazy hashing
|
||||
//0 means bucket is empty
|
||||
for(uint32_t c = 0; c < m.maxData; ++c)
|
||||
{
|
||||
m.elements[c].data = 0;
|
||||
}
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
void destroyMap(map* m)
|
||||
{
|
||||
//actual memory freeing is done by caller
|
||||
m->elements = 0;
|
||||
m->maxData = 0;
|
||||
}
|
34
driver/map.h
Normal file
34
driver/map.h
Normal file
@ -0,0 +1,34 @@
|
||||
#pragma once
|
||||
|
||||
#if defined (__cplusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdint.h>
|
||||
#include "CustomAssert.h"
|
||||
|
||||
typedef struct
|
||||
{
|
||||
void* data;
|
||||
uint32_t key;
|
||||
} mapElem;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
mapElem* elements;
|
||||
uint32_t maxData;
|
||||
} map;
|
||||
|
||||
void* getMapElement(map m, uint32_t key);
|
||||
void setMapElement(map* m, uint32_t key, void* data);
|
||||
void deleteMapElement(map* m, uint32_t key);
|
||||
map createMap(void* buf, uint32_t maxData);
|
||||
void destroyMap(map* m);
|
||||
|
||||
|
||||
#if defined (__cplusplus)
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user