Hashmap new & newEx

git-svn-id: https://spexeah.com:8443/svn/Asuro@1311 6dbc8c32-bb84-406f-8558-d1cf31a0ab0c
This commit is contained in:
kieron 2020-07-18 00:34:30 +00:00
parent e89b2e05b3
commit 06c50df49d
2 changed files with 17 additions and 16 deletions

View File

@ -22,7 +22,12 @@ type
Table : DPHashItem; Table : DPHashItem;
end; end;
function new(size : uint32) : PHashMap; const
HASHMAP_DEFAULT_SIZE = 16;
HASHMAP_DEFAULT_LOADFACTOR = 0.75;
function new : PHashMap;
function newEx(size : uint32; loadFactor : Single) : PHashMap;
procedure add(map : PHashMap; key : pchar; value : void); procedure add(map : PHashMap; key : pchar; value : void);
function get(map : PHashMap; key : pchar) : void; function get(map : PHashMap; key : pchar) : void;
procedure delete(map : PHashMap; key : pchar; freeItem : boolean); procedure delete(map : PHashMap; key : pchar; freeItem : boolean);
@ -66,22 +71,15 @@ var
c : uint32; c : uint32;
begin begin
tracer.push_trace('hashmap.putItem.1');
ExistingItem:= table[idx]; ExistingItem:= table[idx];
tracer.push_trace('hashmap.putItem.2');
if ExistingItem = nil then begin if ExistingItem = nil then begin
tracer.push_trace('hashmap.putItem.3');
table[idx]:= item; table[idx]:= item;
end else begin end else begin
tracer.push_trace('hashmap.putItem.4');
c:=0; c:=0;
while ExistingItem^.Next <> nil do begin while ExistingItem^.Next <> nil do begin
tracer.push_trace('hashmap.putItem.5');
ExistingItem:= ExistingItem^.Next; ExistingItem:= ExistingItem^.Next;
end; end;
tracer.push_trace('hashmap.putItem.6');
ExistingItem^.Next:= item; ExistingItem^.Next:= item;
tracer.push_trace('hashmap.putItem.6');
end; end;
end; end;
@ -182,7 +180,7 @@ begin
end; end;
end; end;
function new(size : uint32) : PHashMap; function newEx(size : uint32; loadFactor : Single) : PHashMap;
var var
Map : PHashMap; Map : PHashMap;
i : uint32; i : uint32;
@ -191,12 +189,17 @@ begin
Map:= PHashMap(kalloc(sizeof(THashMap))); Map:= PHashMap(kalloc(sizeof(THashMap)));
Map^.size:= size; Map^.size:= size;
Map^.Table:= DPHashItem(Kalloc(sizeof(PHashItem) * Size)); Map^.Table:= DPHashItem(Kalloc(sizeof(PHashItem) * Size));
Map^.LoadFactor:= 0.75; Map^.LoadFactor:= loadFactor;
Map^.Count:= 0; Map^.Count:= 0;
for i:=0 to size-1 do begin for i:=0 to size-1 do begin
Map^.Table[i]:= nil; Map^.Table[i]:= nil;
end; end;
new:= Map; newEx:= Map;
end;
function new : PHashMap;
begin
new:= newEx(HASHMAP_DEFAULT_SIZE, HASHMAP_DEFAULT_LOADFACTOR);
end; end;
procedure delete(map : PHashMap; key : pchar; freeItem : boolean); procedure delete(map : PHashMap; key : pchar; freeItem : boolean);

View File

@ -212,7 +212,7 @@ begin
{ Bus Drivers } { Bus Drivers }
tracer.push_trace('kmain.BUSDRV'); tracer.push_trace('kmain.BUSDRV');
console.outputln('KERNEL', 'BUS DRIVERS: INIT BEGIN.'); console.outputln('KERNEL', 'BUS DRIVERS: INIT BEGIN.');
//USB.init(); USB.init();
pci.init(); pci.init();
console.outputln('KERNEL', 'BUS DRIVERS: INIT END.'); console.outputln('KERNEL', 'BUS DRIVERS: INIT END.');
@ -240,11 +240,11 @@ begin
writestringln(' '); writestringln(' ');
tracer.push_trace('kmain.END'); tracer.push_trace('kmain.END');
rand.srand((getDateTime.Seconds SHR 24) OR (getDateTime.Minutes SHR 16) OR (getDateTime.Hours SHR 8) OR (getDateTime.Day)); rand.srand((getDateTime.Seconds SHL 24) OR (getDateTime.Minutes SHL 16) OR (getDateTime.Hours SHL 8) OR (getDateTime.Day));
tracer.push_trace('kmain.TICK'); tracer.push_trace('kmain.TICK');
HM:= hashmap.new(16); HM:= hashmap.newEx(16, 0.1);
hashmap.add(HM, 'testificate', void(1)); hashmap.add(HM, 'testificate', void(1));
hashmap.add(HM, 'asuro', void(10)); hashmap.add(HM, 'asuro', void(10));
hashmap.add(HM, 'myfirstos', void(100)); hashmap.add(HM, 'myfirstos', void(100));
@ -280,9 +280,7 @@ begin
hashmap.add(HM, 'Laptop', void(50000000)); hashmap.add(HM, 'Laptop', void(50000000));
hashmap.add(HM, 'Salmon', void(500000000)); hashmap.add(HM, 'Salmon', void(500000000));
hashmap.add(HM, 'OnionBurger', void(60)); hashmap.add(HM, 'OnionBurger', void(60));
hashmap.printmap(HM); hashmap.printmap(HM);
writeintln(uint32(hashmap.get(HM, 'Ana'))); writeintln(uint32(hashmap.get(HM, 'Ana')));
// Testing getting into userspace // Testing getting into userspace