mirror of
https://github.com/Fennix-Project/Kernel.git
synced 2025-08-30 23:42:10 +00:00
.github
.vscode
Architecture
Core
FileSystem
Library
SystemCalls
Tasking
include
boot
filesystem
assert.h
atomic.hpp
bitmap.hpp
cargs.h
convert.h
cpu.hpp
cstring
cwalk.h
debug.h
display.hpp
filesystem.hpp
hashmap.hpp
interrupts.hpp
io.h
ipc.hpp
kconfig.hpp
limits.h
lock.hpp
memory.hpp
pci.hpp
power.hpp
printf.h
rand.hpp
smartptr.hpp
smp.hpp
symbols.hpp
sys.h
syscalls.hpp
task.hpp
time.hpp
types.h
uart.hpp
vector.hpp
.gitignore
Doxyfile
Fennix Kernel.code-workspace
KConfig.cpp
KThread.cpp
Kernel.cpp
LICENSE
Makefile
README.md
kernel.h
94 lines
2.0 KiB
C++
94 lines
2.0 KiB
C++
#pragma once
|
|
|
|
template <typename K, typename V>
|
|
class HashNode
|
|
{
|
|
public:
|
|
V Value;
|
|
K Key;
|
|
|
|
HashNode(K Key, V Value)
|
|
{
|
|
this->Value = Value;
|
|
this->Key = Key;
|
|
}
|
|
};
|
|
|
|
template <typename K, typename V>
|
|
class HashMap
|
|
{
|
|
int HashMapSize;
|
|
int HashMapCapacity;
|
|
HashNode<K, V> **Nodes;
|
|
HashNode<K, V> *DummyNode;
|
|
|
|
public:
|
|
HashMap()
|
|
{
|
|
HashMapCapacity = 20;
|
|
HashMapSize = 0;
|
|
Nodes = new HashNode<K, V> *[HashMapCapacity];
|
|
for (int i = 0; i < HashMapCapacity; i++)
|
|
Nodes[i] = nullptr;
|
|
DummyNode = new HashNode<K, V>(-1, -1);
|
|
}
|
|
|
|
int HashCode(K Key) { return Key % HashMapCapacity; }
|
|
|
|
void AddNode(K Key, V Value)
|
|
{
|
|
HashNode<K, V> *tmp = new HashNode<K, V>(Key, Value);
|
|
int Index = HashCode(Key);
|
|
|
|
while (Nodes[Index] != nullptr && Nodes[Index]->Key != Key && Nodes[Index]->Key != (K)-1)
|
|
{
|
|
Index++;
|
|
Index %= HashMapCapacity;
|
|
}
|
|
|
|
if (Nodes[Index] == nullptr || Nodes[Index]->Key == (K)-1)
|
|
HashMapSize++;
|
|
Nodes[Index] = tmp;
|
|
}
|
|
|
|
V DeleteNode(int Key)
|
|
{
|
|
int Index = HashCode(Key);
|
|
|
|
while (Nodes[Index] != nullptr)
|
|
{
|
|
if (Nodes[Index]->Key == Key)
|
|
{
|
|
HashNode<K, V> *tmp = Nodes[Index];
|
|
Nodes[Index] = DummyNode;
|
|
HashMapSize--;
|
|
return tmp->Value;
|
|
}
|
|
Index++;
|
|
Index %= HashMapCapacity;
|
|
}
|
|
return 0xdeadbeef;
|
|
}
|
|
|
|
V Get(int Key)
|
|
{
|
|
int Index = HashCode(Key);
|
|
int Iterate = 0;
|
|
|
|
while (Nodes[Index] != nullptr)
|
|
{
|
|
if (Iterate++ > HashMapCapacity)
|
|
return 0xdeadbeef;
|
|
|
|
if (Nodes[Index]->Key == (K)Key)
|
|
return Nodes[Index]->Value;
|
|
Index++;
|
|
Index %= HashMapCapacity;
|
|
}
|
|
return 0xdeadbeef;
|
|
}
|
|
|
|
int Size() { return HashMapSize; }
|
|
bool IsEmpty() { return HashMapSize == 0; }
|
|
};
|