Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Utils;
- import java.util.Random;
- public class HashVec
- {
- private int FreeAddrKey = -1;
- private int FreeSpace = 100;
- private int FreeSpaceMultiplier = 3;
- private int[] HashTable = new int[FreeSpace * FreeSpaceMultiplier];
- private int StaticValues[] = { 383, 487, 235, 527, 510, 320, 203, 108, 563, 500, 646, 103 };
- public void InitializeVector()
- {
- for (int iV = 0; iV < this.FreeSpace * FreeSpaceMultiplier; iV++)
- {
- this.HashTable[iV] = FreeAddrKey;
- }
- }
- public int GetAddr(int Value)
- {
- return (Value % this.FreeSpace) + 1;
- }
- public void FreeAddr(int Addr)
- {
- this.HashTable[Addr] = FreeAddrKey;
- }
- public int FindAddr(int Value)
- {
- int Addr = GetAddr(Value);
- if (this.HashTable[Addr] == Value)
- {
- return Addr;
- } else
- {
- int NextAddr = 1;
- while (true)
- {
- if ((Addr + NextAddr) <= ((this.FreeSpace * this.FreeSpaceMultiplier) - 1))
- {
- if (this.HashTable[Addr + NextAddr] == Value)
- {
- // Found.
- break;
- } else
- {
- NextAddr++;
- continue;
- }
- } else
- {
- this.PrintLine("Memory Allocation failed: insufficient space.");
- break;
- }
- }
- }
- return 0;
- }
- public void FillStatic()
- {
- for (int sT = 0; sT < StaticValues.length; sT++)
- {
- int Value = StaticValues[sT];
- int Addr = GetAddr(Value);
- if (this.HashTable[Addr] == FreeAddrKey)
- {
- this.HashTable[Addr] = Value;
- this.LogInsertion(Addr, Value, 0);
- } else
- {
- int NextAddr = 1;
- while (true)
- {
- if ((Addr + NextAddr) <= ((this.FreeSpace * this.FreeSpaceMultiplier) - 1))
- {
- if (this.HashTable[Addr + NextAddr] != FreeAddrKey)
- {
- this.HashTable[Addr + NextAddr] = Value;
- this.LogInsertion(Addr, Value, NextAddr - 1);
- break;
- } else
- {
- NextAddr++;
- continue;
- }
- } else
- {
- this.PrintLine("Memory Allocation failed: insufficient space.");
- break;
- }
- }
- }
- }
- }
- public void FillRandom(int MaxValueRange)
- {
- int zV = this.FreeSpace;
- while (zV != 0)
- {
- Random RandomInt = new Random();
- int Value = RandomInt.nextInt(MaxValueRange);
- int Addr = GetAddr(Value);
- if (this.HashTable[Addr] == FreeAddrKey)
- {
- this.HashTable[Addr] = Value;
- this.LogInsertion(Addr, Value, 0);
- } else
- {
- int NextAddr = 1;
- while (true)
- {
- if ((Addr + NextAddr) <= ((this.FreeSpace * this.FreeSpaceMultiplier) - 1))
- {
- if (this.HashTable[Addr + NextAddr] != FreeAddrKey)
- {
- this.HashTable[Addr + NextAddr] = Value;
- this.LogInsertion(Addr, Value, NextAddr - 1);
- break;
- } else
- {
- NextAddr++;
- continue;
- }
- } else
- {
- this.PrintLine("Memory Allocation failed: insufficient space.");
- break;
- }
- }
- }
- zV--;
- }
- }
- public void PrintLine(String Line)
- {
- System.out.println(Line);
- }
- public void LogInsertion(int Addr, int Value, int Colisions)
- {
- PrintLine("Addr: " + Addr + " Value: " + Value + " Colisions: " + Colisions);
- }
- public void ShowVector(int[] Vector)
- {
- for (int sV = 0; sV < (FreeSpace * FreeSpaceMultiplier) - 1; sV++)
- {
- PrintLine("Addr: " + sV + " Value: " + Vector[sV]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement