Quick Links:

bal | bbl | bdl | bsl

Namespaces

Component bdlb_caselessstringviewhash
[Package bdlb]

Provide a case-insensitive hash functor for string views. More...

Namespaces

namespace  bdlb

Detailed Description

Outline
Purpose:
Provide a case-insensitive hash functor for string views.
Classes:
bdlb::CaselessStringViewHash case-insensive hash functor
See also:
Component bdlb_caselessstringviewequalto, Component bdlb_caselessstringviewless
Description:
This component provides a struct, bdlb::CaselessStringViewHash, that defines a functor to generate a case-insensitivee hash code for a string view. This hash functor is suitable for supporting bsl::strings and bsl::string_views as keys in unordered associative containers.
Note that using this component to hash keys into a container and then using the bdlb_caselessstringviewequalto component to compare them is less efficient than converting all the keys to the same case prior to insertion and then just doing straight hashes and string compares.
Usage:
This section illustrates intended use of this component.
Example 1: Basic Use of bdlb::CaselessStringViewHash:
Suppose we need an associative container to store, for any stock name, the number of shares of that stock we own:
  typedef bsl::unordered_map<bsl::string, int> SecuritiesOwnedCS;
                                                      // case sensitive


  typedef bsl::unordered_map<bsl::string,
                             int,
                             bdlb::CaselessStringViewHash,
                             bdlb::CaselessStringViewEqualTo>
                                                           SecuritiesOwnedCI;
                                                      // case insensitive
This type of container stores quantities of shares and allows access to them by their names, in a case-insensitive manner.
First, we create a container for securities holdings, case-sensitive, and fill it:
  SecuritiesOwnedCS securitiesOwnedCS;

  securitiesOwnedCS["IBM"]       = 616;
  securitiesOwnedCS["Microsoft"] = 6160000;

  assert(2 == securitiesOwnedCS.size());
Then, we create a container for securities holdings, case-insensitive, and fill it:
  SecuritiesOwnedCI securitiesOwnedCI;

  securitiesOwnedCI["IBM"]       = 616;
  securitiesOwnedCI["Microsoft"] = 6160000;

  assert(2 == securitiesOwnedCI.size());
Now, we try accessing the case-sensitive securitiesc:
  assert(1   == securitiesOwnedCS.count("IBM"));
  assert(616 == securitiesOwnedCS[      "IBM"]);

  assert(0   == securitiesOwnedCS.count("ibm"));
  assert(0   == securitiesOwnedCS.count("Ibm"));
  assert(0   == securitiesOwnedCS.count("iBm"));

  assert(1       == securitiesOwnedCS.count("Microsoft"));
  assert(6160000 == securitiesOwnedCS[      "Microsoft"]);

  assert(0       == securitiesOwnedCS.count("MICROSOFT"));
  assert(0       == securitiesOwnedCS.count("microsoft"));
  assert(0       == securitiesOwnedCS.count("MICROSOFT"));

  assert(0 == securitiesOwnedCS.count("Google"));
Finally, we access the case-insensitive securitiesci:
  assert(1   == securitiesOwnedCI.count("IBM"));
  assert(616 == securitiesOwnedCI[      "IBM"]);
  assert(1   == securitiesOwnedCI.count("ibm"));
  assert(616 == securitiesOwnedCI[      "ibm"]);
  assert(1   == securitiesOwnedCI.count("Ibm"));
  assert(616 == securitiesOwnedCI[      "Ibm"]);
  assert(1   == securitiesOwnedCI.count("iBm"));
  assert(616 == securitiesOwnedCI[      "iBm"]);

  assert(1       == securitiesOwnedCI.count("Microsoft"));
  assert(6160000 == securitiesOwnedCI[      "Microsoft"]);
  assert(1       == securitiesOwnedCI.count("MICROSOFT"));
  assert(6160000 == securitiesOwnedCI[      "MICROSOFT"]);
  assert(1       == securitiesOwnedCI.count("microsoft"));
  assert(6160000 == securitiesOwnedCI[      "microsoft"]);
  assert(1       == securitiesOwnedCI.count("MICROSOFT"));
  assert(6160000 == securitiesOwnedCI[      "MICROSOFT"]);

  assert(0 == securitiesOwnedCI.count("Google"));