Admin Production ni-theme
Current Publication

HashTableInsertItem

LabWindows/CVI

HashTableInsertItem

int HashTableInsertItem (HashTableType table, const void *key, const void *value);

Purpose

Adds a key-value pair to a hash table.

This function allocates storage for the key and the value and adds the pair to the table. If there is already a value associated with the key, the function replaces the old value with the new value.

If adding the pair increases the load of the table beyond the grow load of the table, LabWindows/CVI resizes the table to twice its size. Resizing can be very slow because it requires rehashing all keys stored in the table.

Parameters

Input
Name Type Description
table HashTableType Hash table into which to insert the item.
key const void * Pointer to the first byte of the key data.

The following code demonstrates the appropriate ways to pass various types of keys:

{

HashTableType table;
int intKey = 1;
char charKey = 'A';
char *strKey = "Hello, World.";
double value = 0.0;

HashTableCreate(10, FIXED_SIZE_KEY, sizeof(int), sizeof(double), &table);
HashTableInsertItem(table, &intKey, &value);
HashTableDispose(table);

HashTableCreate(10, FIXED_SIZE_KEY, sizeof(char), sizeof(double), &table);
HashTableInsertItem(table, &charKey, &value);
HashTableDispose(table);

HashTableCreate(10, C_STRING_KEY, 0, sizeof(double), &table);
HashTableInsertItem(table, strKey, &value);
HashTableDispose(table);

}

value const void * Pointer to the first byte of the value data.

The following code demonstrates the appropriate ways to pass various types of values.

{

HashTableType table;
int key;
double dblVal = 0;
char charVal = 'A';
char *strVal;

HashTableCreate(10, FIXED_SIZE_KEY, sizeof(int), sizeof(double), &table);
HashTableInsertItem(table, &key, &dblVal);
HashTableDispose(table);

HashTableCreate(10, FIXED_SIZE_KEY, sizeof(int), sizeof(char), &table);
HashTableInsertItem(table, &key, &charVal);
HashTableDispose(table);

/* storing fixed length arrays (strings in this case) */
HashTableCreate(10, FIXED_SIZE_KEY, sizeof(int), 5 * sizeof(char), &table);
strVal = "0000";
HashTableInsertItem(table, &key, strVal);
strVal = "0001";
HashTableInsertItem(table, &key, strVal);
strVal = "0002";
HashTableInsertItem(table, &key, strVal);
HashTableDispose(table);

/* storing variable length data */
HashTableCreate(10, FIXED_SIZE_KEY, sizeof(int), sizeof(char *), &table);
strVal = "0";
HashTableInsertItem(table, &key, &strVal);
strVal = "01";
HashTableInsertItem(table, &key, &strVal);
strVal = "002";
HashTableInsertItem(table, &key, &strVal);
HashTableDispose(table);

}

Note Note  In the last case, the string data is not actually stored in the hash table; rather, a pointer to the data is stored.

Return Value

Name Type Description
status int Returns a value indicating if the function was successful.

A negative number means an error occurred.

For a detailed error message, pass the returned value to GetGeneralErrorString.

Additional Information

Library: Programmer's Toolbox

Include file: toolbox\toolbox.h

LabWindows/CVI compatibility: LabWindows/CVI 8.5 and later

Example

Refer to toolbox\hashtable.cws for an example of using the HashTableInsertItem function.