[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

7.0

Help with uniform hash distribution

Asked by rllibby in Delphi Programming

Tags: hash


I am working on a project that requires me to store and later validate/search for a set of pointers, and I could use a hand with the hash function for calculating the index.

First, the requirements:

1.) I only need comments/assistance with the hash function itself, nothing else. Do not suggest things like sorted lists, binary trees, etc.

2.) My hash table size needs to be kept in the ballpark of 16384 items; I will be using bucket chaining for collisions resolution. The 16384 is designed to be optimal with the storage of appx 256K items (a perfect distribution will mean a maximum of 16 checks per lookup, vs the 18 max checks required by a binary search)

3.) The keys for the hash are memory pointers, and more specifically, will come from either a GetMem or AllocMem call. This does give them (the keys) some traits, such as being 4 byte aligned, etc.

4.) 500 pts will be given to each answer that is an improvement on what I already have.

Here is the test frame work that I currently have set up. Again, I am only looking for enhancements to the following function.

function TForm1.HashIndex(Value: Pointer): Integer;

Thanks to all in advance,
Russell



--- Form Source Code ---

unit main;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls;

// Prime number for hash size
const
  HASH_SIZE      =  16349;

// Hash definition
type
  THashArray     =  Array [0..Pred(HASH_SIZE)] of Integer;

type
  TForm1         =  class(TForm)
     Button1:    TButton;
     ListBox1:   TListBox;
     procedure   Button1Click(Sender: TObject);
     procedure   FormCreate(Sender: TObject);
     procedure   ListBox1DrawItem(Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState);
  private
     // Private declarations
     FMaxCount:  Integer;
     FHashArray: THashArray;
  public
     // Public declarations
     function    HashIndex(Value: Pointer): Integer;
  end;

var
  Form1:         TForm1;

implementation
{$R *.DFM}

function TForm1.HashIndex(Value: Pointer): Integer;
var Key:      Integer;
begin

  // Mod the value with the hash size
  result:=(Integer(Value) shr 6) mod HASH_SIZE;

end;

procedure TForm1.Button1Click(Sender: TObject);
var  List:       TList;
     i:          Integer;
     j:          Integer;
     p:          Pointer;
     dwStats:    Array [0..2] of Integer;
     dwSlots:    Integer;
     arrInts:    Array of Integer;
begin


  // Setup for test
  List:=TList.Create;
  ListBox1.Items.Clear;
  ZeroMemory(@FHashArray, SizeOf(THashArray));
  Randomize;

  try
     // Allocate memory blocks and increment the hash slot of where the item falls
     for i:=0 to 262144 do
     begin
        p:=AllocMem(Succ(Random(32)));
        Inc(FHashArray[HashIndex(p)]);
        List.Add(p);
     end;
     // Calculate the hash stats
     dwStats[0]:=0;       // Max value
     dwStats[1]:=MaxInt;  // Min value
     dwStats[2]:=0;       // Count of empty slots
     for i:=0 to High(FHashArray) do
     begin
        if (FHashArray[i] > dwStats[0]) then dwStats[0]:=FHashArray[i];
        if (FHashArray[i] < dwStats[1]) then dwStats[1]:=FHashArray[i];
        if (FHashArray[i] = 0) then Inc(dwStats[2]);
     end;
     // Allocate memory for the number of slots
     dwSlots:=Succ(dwStats[0]-dwStats[1]);
     SetLength(arrInts, dwSlots);
     // Fill in the count for each slot
     for i:=dwStats[1] to dwStats[0] do
     begin
        for j:=0 to High(FHashArray) do
        begin
           if (FHashArray[j] = i) then Inc(arrInts[i-dwStats[1]]);
        end;
     end;
     // Graph the hash statistics
     ListBox1.Items.BeginUpdate;
     FMaxCount:=0;
     for i:=dwStats[1] to dwStats[0] do
     begin
        j:=arrInts[i-dwStats[1]];
        if (j > FMaxCount) then FMaxCount:=j;
        ListBox1.Items.AddObject(Format('%d bucket(s) with %d items', [j, i]), Ptr(j));
     end;
     ListBox1.Items.EndUpdate;
     Caption:=Format('Min bucket count = %d, Max bucket count = %d', [dwStats[1], dwStats[0]]);
     // Free memory
     SetLength(arrInts, 0);
  finally
     // Free test memory
     for i:=Pred(List.Count) downto 0 do FreeMem(List[i]);
     // Tear down test
     List.Clear;
     List.Free;
  end;

end;

procedure TForm1.FormCreate(Sender: TObject);
begin

  FMaxCount:=0;

end;

procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState);
var  dwCount:       Integer;
     rcFill:        TRect;
     dblPct:        Double;
begin

  // Calculate the percentage based on max and count
  dwCount:=Integer(ListBox1.Items.Objects[Index]);
  dblPct:=(dwCount / Succ(FMaxCount));

  // Get the rect to draw
  with ListBox1.Canvas do
  begin
     FillRect(Rect);
     rcFill:=Rect;
     rcFill.Right:=rcFill.Left+Trunc((rcFill.Right-rcFill.Left) * dblPct);
     Brush.Color:=clYellow;
     FillRect(rcFill);
     Brush.Color:=clWindow;
     SetBkMode(Handle, TRANSPARENT);
     TextOut(Rect.Left+2, Rect.Top+2, ListBox1.Items[Index]);
  end;

end;

end.

--- form ---

object Form1: TForm1
  Left = 80
  Top = 117
  Width = 638
  Height = 373
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object Button1: TButton
    Left = 8
    Top = 8
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 0
    OnClick = Button1Click
  end
  object ListBox1: TListBox
    Left = 136
    Top = 0
    Width = 494
    Height = 339
    Align = alRight
    ItemHeight = 16
    Style = lbOwnerDrawFixed
    TabOrder = 1
    OnDrawItem = ListBox1DrawItem
  end
end



[+][-]02/16/04 10:50 AM, ID: 10374530Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02/16/04 12:28 PM, ID: 10375477Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zone: Delphi Programming
Tags: hash
Sign Up Now!
Solution Provided By: sftweng
Participating Experts: 3
Solution Grade: A
 
[+][-]02/16/04 12:33 PM, ID: 10375524Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02/16/04 12:43 PM, ID: 10375617Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02/16/04 01:09 PM, ID: 10375838Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]02/16/04 01:24 PM, ID: 10375971Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02/16/04 01:36 PM, ID: 10376086Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02/16/04 01:50 PM, ID: 10376198Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02/16/04 02:00 PM, ID: 10376278Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]02/16/04 02:05 PM, ID: 10376333Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02/16/04 02:07 PM, ID: 10376364Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02/16/04 02:08 PM, ID: 10376367Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02/16/04 02:12 PM, ID: 10376407Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02/16/04 03:55 PM, ID: 10377376Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]02/16/04 04:09 PM, ID: 10377439Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02/17/04 12:01 AM, ID: 10379933Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02/17/04 07:30 AM, ID: 10383019Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]02/17/04 07:47 AM, ID: 10383224Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02/17/04 10:54 AM, ID: 10385028Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]02/17/04 10:58 AM, ID: 10385084Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02/17/04 11:03 AM, ID: 10385126Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-89