3D PLM Enterprise Architecture

Middleware Abstraction

Using Hash Tables

Creating and managing hash tables
Use Case

Abstract

This article shows how to create and manage a hash table, illustrated by a hash table of pointers to the CAASysPoint class.


What You Will Learn With This Use Case

This use case is intended to show you how to create and manage a hash table.

[Top]

The CAASysCollections Case

CAASysCollections is a set of use cases of the CAASystem.edu framework that illustrates the collection management capabilities.

[Top]

What Does CAASysCollections Do

This use case shows summarizes the collection management capabilities:

This article describes the hash table capabilities, taking a hash table of pointers to instances of the CAASysPoint class as example.

[Top]

How to Launch CAASysCollections

To launch CAASysCollections, you will need to set up the build time environment, then compile CAASysCollections along with its prerequisites, set up the run time environment, and then execute the use case [1].

[Top]

Where to Find the CAASysCollections Code

The CAASysCollections use case is made of a several classes located in the CAASysCollections.m module of the CAASystem.edu framework:

Windows InstallRootDirectory\CAASystem.edu\CAASysCollections.m\
Unix InstallRootDirectory/CAASystem.edu/CAASysCollections.m/

where InstallRootDirectory is the directory where the CAA CD-ROM is installed.

[Top]

Step-by-Step

The following capabilities offered by lists of integers are described in the following steps:

#

Step

1 Create a hash table
2 Fill in a hash table
3 Count the items
4 Insert items
5 Remove items
6 Retrieve the bucket and position of an item
7 Access an item using its bucket and location
8 Retrieve the first item with a given hash value

The CATSysPoint class used represents 2D points. To enable its pointers to be used by a hash table, the CATSysPoint class must feature a hash method and a comparison method. These methods are named Hash and Compare respectively in CATSysPoint.

[Top]

Creating a Hash Table

A hash table of pointers to instances of the CATSysPoint class is created as a class using macros. The class header file named CAASysHashTableToSysPointPtr.h is as follows:

#ifndef CAASysHashTableToSysPointPtr_h
#define CAASysHashTableToSysPointPtr_h

#include "CAASysPoint.h"

#include "CATHTAB_Clean.h"
#include "CATHTAB_AllFunct.h"

#include "CATHTAB_Declare.h"
CATHTAB_DECLARE(CAASysPoint)

#endif

The CATHTAB_Clean.h file undefines all possible previously defined methods, while the CATHTAB_AllFunct.h defines for the hash table class to create all the available methods for hash table classes. The CATHTAB_DECLARE macro creates the class header file.

The source file named CAASysHashTableToSysPointPtr.cpp is as follows:

#include "CAASysHashTableToSysPointPtr.h"

#include  "CATHTAB_Define.h"
CATHTAB_DEFINE(CAASysPoint)

The CATHTAB_DEFINE macro creates the class source file. The class for a hash table of pointers to the CAASysPoint class is now created. Its name is CATHTAB(CAASysPoint).

[Top]

Filling in a Hash Table

void CAAHashtablesSample()
{
  ...
  CATHTAB(CAASysPoint) hashtable(&CAASysPoint::Hash, &CAASysPoint::Compare);

  CAASysPoint pt1(1.0f, 2.0f);
  CAASysPoint pt2(-4.0f, 3.0f);
  CAASysPoint pt3(6.0, 7.0f);

  hashtable.Insert( &pt1 );
  hashtable.Insert( &pt2 );
  hashtable.Insert( &pt3 );
  ...

The CATHTAB(CAASysPoint) class is first instantiated, and its constructor takes the pointers to the hash and comparison methods of the class whose instance pointers are to be used. Since no size is passed as the third parameter, the table can accommodate ten items by default. Then, some CAASysPoint class instances are created. The Insert method inserts these instances in the hash table.

[Top]

Counting the Items

  ...
  cout << "hashtable.Size() = " << hashtable.Size() << endl;
  ...

The Size method returns the number of items in the hash table. Note that this size represents the actual number of items in the hash table, and not the possible number the hash table can accommodate. This code provides the following output:
hashtable.Size() = 3

[Top]

Inserting Items

  ...
  hashtable.Insert( &pt1 );

  CAASysPoint pt4(7.0, 8.0f);
  hashtable.Insert( &pt4 );
  ...

The Insert method inserts items into the hash table. Note that no duplicate items can exist in a hash table. As a consequence, the first call to Insert has no effect, since an item containing &pt1 already exists in the set. The second call to Insert appends &pt4 to the hash table. The items are not ordered.

[Top]

Removing Items

  ...
  hashtable.Remove( &pt1 );
  ...

The Remove method removes the pointer passed as parameter from the hash table. The hash table now includes three pointers to points p1, p3, and p4.

[Top]

Retrieving the Bucket and Position of an Item

  ...
  int	oBucket = 0;
  int	oPosition = 0;
  hashtable.LocatePosition( &pt3, oBucket, oPosition );
  cout << "Bucket:"   << oBucket   << endl;
  cout << "Position:" << oPosition << endl;
  ...

The LocatePosition method retrieves the bucket and the position of a given pointer in the hash table. This code provides the following output:
Bucket:2
Position:1

[Top]

Accessing an Item Using its Bucket and Location

  ...
  CAASysPoint *pt5 = NULL;
  pt5 = hashtable.Retrieve(oBucket, oPosition);
  ...

The Retrieve method retrieves the item that has oBucket as bucket and oPosition as position. Since these parameters are initialized to the bucket and position of &pt3, this pointer is copied in pt5. If Retrieve fails, a NULL pointer is returned .

[Top]

Retrieving the First Item with a Given Hash Value

  ...
  CAASysPoint *pt6 = NULL;
  pt6 = hashtable.KeyLocate(CAASysPoint::Hash(&pt3));
  ...

The KeyLocate retrieves the first item that has the same hash value than &pt3. Of course, in this simple example, &pt3 is returned.

[Top]


In Short

This use case shows how to create a class for a hash table to pointers to a given class, and how to use it.

[Top]


References

[2] Building and Launching a CAA V5 Use Case
[Top]

History

Version: 1 [Mar 2000] Document created
[Top]

Copyright © 2000, Dassault Systèmes. All rights reserved.