BDE 4.14.0 Production release
Loading...
Searching...
No Matches

Macros

#define BSLSTL_HASH_DEPRECATED_CPP17
 

Detailed Description

Outline

Purpose

Provide a namespace for hash functions.

Classes

Canonical header: bsl_functional.h

See also
package bos+stdhdrs in the bos package group

Description

This component provides a template unary functor, bsl::hash, implementing the std::hash functor. bsl::hash applies a C++ standard compliant, implementation defined, hash function to fundamental types returning the result of such application.

Standard Hash Function

According to the C++ standard the requirements of a standard hash function h are:

  1. Return a size_t value between 0 and numeric_limits<std::size_t>::max().
  2. The value returned must depend only on the argument k. For multiple evaluations with the same argument k, the value returned must be always the same.
  3. The function should not modify its argument.

Usage

This section illustrates intended usage of this component.

Example 1: Creating and Using a Hash Cross Reference

Suppose we already have an array of unique values of type TYPE, for which operator== is defined, and we want to be able to quickly look up whether an element is in the array, without exhaustively applying operator== to all the elements in sequence. The array itself is guaranteed not to change for the duration of our interest in it.

The problem is much simpler than building a general-purpose hash table, because we know how many elements our cross reference will contain in advance, so we will never have to dynamically grow the number of buckets. We do not need to copy the values into our own area, so we don't have to create storage for them, or require that a copy constructor or destructor be available. We only require that they have a transitive, symmetric equivalence operation bool operator== and that a hash function be provided.

We will need a hash function – the hash function is a function that will take as input an object of the type stored in our array, and yield a size_t value that will be very randomized. Ideally, the slightest change in the value of the TYPE object will result in a large change in the value returned by the hash function. In a good hash function, typically half the bits of the return value will change for a 1-bit change in the hashed value. We then use the result of the hash function to index into our array of buckets. Each bucket is simply a pointer to a value in our original array of TYPE objects. We will resolve hash collisions in our array through linear probing, where we will search consecutive buckets following the bucket where the collision occurred, testing occupied buckets for equality with the value we are searching on, and concluding that the value is not in the table if we encounter an empty bucket before we encounter one referring to an equal element.

An important quality of the hash function is that if two values are equivalent, they must yield the same hash value.

First, we define our HashCrossReference template class, with the two type parameters TYPE" (the type being referenced</tt> and <tt>HASHER</tt>, which defaults to <tt>bsl::hash\<TYPE\></tt>. For common types of <tt>TYPE</tt> such as <tt>int</tt>, a specialization of <tt>bsl::hash</tt> is already defined: @code template <class TYPE, class HASHER = bsl::hash<TYPE> > class HashCrossReference { // This table leverages a hash table to provide a fast lookup of an // external, non-owned, array of values of configurable type. // // The only requirement for 'TYPE' is that it have a transitive, // symmetric 'operator==' function. There is no requirement that it // have any kind of creator defined. // // The 'HASHER' template parameter type must be a functor with a // function of the following signature: //.. // size_t operator()(const TYPE) const; or // size_t operator()(const TYPE&) const; or //.. // and 'HASHER' must have a publicly available default constructor and // destructor. // DATA const TYPE *d_values; // Array of values table is to // cross-reference. Held, not // owned. size_t d_numValues; // Length of 'd_values'. const TYPE **d_bucketArray; // Contains pointers into // 'd_values'. size_t d_bucketArrayMask; // Will always be '2^N - 1'. HASHER d_hasher; bool d_valid; // Object was properly // initialized. bslma::Allocator *d_allocator_p; // Held, not owned. private: // PRIVATE ACCESSORS /// Look up the specified `value`, having hash value `hashValue`, /// and return its index in `d_bucketArray` stored in the specified /// `index`. If not found, return the vacant entry in /// `d_bucketArray` where it should be inserted. Return `true` if /// `value` is found and `false` otherwise. bool lookup(size_t *index, const TYPE& value, size_t hashValue) const { const TYPE *ptr; for (*index = hashValue & d_bucketArrayMask; static_cast<bool>(ptr = d_bucketArray[*index]); *index = (*index + 1) & d_bucketArrayMask) { if (value == *ptr) { return true; // RETURN } } // value was not found in table return false; } // NOT IMPLEMENTED HashCrossReference(const HashCrossReference&); HashCrossReference& operator=(const HashCrossReference&); public: // CREATORS /// Create a hash table refering to the specified `valuesArray` /// containing the specified `numValues` elements. Optionally /// specify `basicAllocator` or the default allocator will be used. HashCrossReference(const TYPE *valuesArray, size_t numValues, bslma::Allocator *basicAllocator = 0) : d_values(valuesArray) , d_numValues(numValues) , d_hasher() , d_valid(true) , d_allocator_p(bslma::Default::allocator(allocator)) { size_t bucketArrayLength = 4; while (bucketArrayLength < numValues * 4) { bucketArrayLength *= 2; BSLS_ASSERT_OPT(bucketArrayLength); } d_bucketArrayMask = bucketArrayLength - 1; d_bucketArray = (const TYPE **) d_allocator_p->allocate( bucketArrayLength * sizeof(TYPE **)); memset(d_bucketArray, 0, bucketArrayLength * sizeof(TYPE *)); for (unsigned i = 0; i < numValues; ++i) { const TYPE& value = d_values[i]; size_t idx; if (lookup(&idx, value, d_hasher(value))) { // Duplicate value. Fail. printf("Error: entries u and u have the same value
", i, unsigned(d_bucketArray[idx] - d_values)); d_valid = false;

// don't return, continue reporting other redundant // entries. } else { d_bucketArray[idx] = &d_values[i]; } } }

/// Free up memory used by this cross-reference. ~HashCrossReference() { d_allocator_p->deallocate(d_bucketArray); }

// ACCESSORS

/// Return 1 if the specified `value` is found in the cross /// reference and 0 otherwise. int count(const TYPE& value) const { BSLS_ASSERT_OPT(d_valid);

size_t idx; return lookup(&idx, value, d_hasher(value)); }

/// Return `true` if this cross reference was successfully /// constructed and `false` otherwise. bool isValid() const { return d_valid; } }; Then, In main, we will first use our cross-reference to cross-reference a collection of integer values. We define our array and take its length:

const int ints[] = { 23, 42, 47, 56, 57, 61, 62, 63, 70, 72, 79 };
enum { NUM_INTS = sizeof ints / sizeof *ints };

Now, we create our cross-reference hcri and verify it constructed properly. Note that we don't specify the second template parameter HASHER and let it default to bsl::hash<int>, which is already defined by bslstl_hash:

HashCrossReference<int> hcri(ints, NUM_INTS);
assert(hcri.isValid());

Finally, we use hcri to verify numbers that were and were not in the collection:

assert(1 == hcri.count(23));
assert(1 == hcri.count(42));
assert(1 == hcri.count(47));
assert(1 == hcri.count(56));
assert(0 == hcri.count( 3));
assert(0 == hcri.count(31));
assert(0 == hcri.count(37));
assert(0 == hcri.count(58));

Example 2: Using hashAppend from bslh with HashCrossReference

We want to specialize bsl::hash for a custom class. We can use the modular hashing system implemented in bslh rather than explicitly specializing bsl::hash. We will re-use the HashCrossReference template class defined in Example 1.

First, we declare Point, a class that allows us to identify a location on a two dimensional Cartesian plane.

/// This class is a value-semantic type that represents a two-
/// dimensional location on a Cartesian plane.
class Point {
private:
int d_x;
int d_y;
double d_distToOrigin; // This value will be accessed a lot, so we
// cache it rather than recalculating every
// time.
public:
/// Create a `Point` with the specified `x` and `y` coordinates
Point (int x, int y);
/// Return the distance from the origin (0, 0) to this point.
double distanceToOrigin();

Then, we declare operator== as a friend so that we will be able to compare two points.

friend bool operator==(const Point &left, const Point &right);

Next, we declare hashAppend as a friend so that we will be able hash a Point.

template <class HASH_ALGORITHM>
friend
void hashAppend(HASH_ALGORITHM &hashAlg, const Point &point);
// Apply the specified 'hashAlg' to the specified 'point'
};
Point::Point(int x, int y)
: d_x(x)
, d_y(y)
{
d_distToOrigin = sqrt(static_cast<double>(d_x) * d_x +
static_cast<double>(d_y) * d_y);
}
double Point::distanceToOrigin()
{
return d_distToOrigin;
}

Then, we define operator==. Notice how it only checks salient attributes

Macro Definition Documentation

◆ BSLSTL_HASH_DEPRECATED_CPP17

#define BSLSTL_HASH_DEPRECATED_CPP17
Value:
"bsl", "deprecated_cpp17_standard_library_features", "do not use")
#define BSLS_DEPRECATE_FEATURE(UOR, FEATURE, MESSAGE)
Definition bsls_deprecatefeature.h:319