VTK  9.3.20240418
Public Types | Public Member Functions | Static Public Member Functions | Protected Attributes | List of all members
vtkStringToken Class Reference

Represent a string by its integer hash. More...

#include <vtkStringToken.h>

Public Types

using Hash = std::uint32_t
 

Public Member Functions

VTK_WRAPEXCLUDE vtkStringToken (const char *data=nullptr, std::size_t size=std::string::npos)
 Construct a token from a string literal. More...
 
 vtkStringToken (const std::string &data)
 Construct a token from a std::string. More...
 
constexpr vtkStringToken (Hash tokenId) noexcept
 Construct a token given its hash value. More...
 
Hash GetId () const
 Return the token's ID (usually its hash but possibly not in the case of collisions). More...
 
unsigned int GetHash () const
 A Python-wrappable (but less strongly typed) alternative to GetId() More...
 
const std::string & Data () const
 Return the string corresponding to the token. More...
 
bool IsValid () const
 Return whether the token is valid or not. More...
 
bool HasData () const
 Return whether a string is available for the token's hash ID. More...
 
bool operator== (const vtkStringToken &other) const
 Fast equality comparison (compares hashes, not strings). More...
 
bool operator!= (const vtkStringToken &other) const
 Fast inequality comparison (compares hashes, not strings). More...
 
bool operator< (const vtkStringToken &other) const
 Slow, but unsurprising string comparison (preserves lexical string ordering). More...
 
bool operator> (const vtkStringToken &other) const
 
bool operator<= (const vtkStringToken &other) const
 
bool operator>= (const vtkStringToken &other) const
 

Static Public Member Functions

static constexpr Hash StringHash (const char *data, std::size_t size) noexcept
 Return the hash of a string This is used internally but also by the ""_token() literal operator. More...
 
static Hash InvalidHash ()
 Return the hash code used to indicate an invalid (empty) token. More...
 

Protected Attributes

Hash Id
 
bool AddChild (vtkStringToken member)
 Methods to manage groups of tokens underneath a parent. More...
 
bool RemoveChild (vtkStringToken member)
 Remove a member from this token's children. More...
 
std::unordered_set< vtkStringTokenChildren (bool recursive=true)
 Return all the children of this token. More...
 
static std::unordered_set< vtkStringTokenAllGroups ()
 Return all the tokens that have children. More...
 

Detailed Description

Represent a string by its integer hash.

This class does not inherit vtkObject; it is a lightweight object for representing a string as a 32-bit integer token. Tokens can be constructed at compile-time (via the ""_token string-literal operator below) or run-time (via the constructor).

Equality comparisons are simple integer tests, while inequality operators attempt to locate the original source strings and compare them alphanumerically to preserve lexicographic ordering.

This class can be used inside ordered and unordered STL containers.

Tests:
vtkStringToken (Tests)

Definition at line 48 of file vtkStringToken.h.

Member Typedef Documentation

◆ Hash

using vtkStringToken::Hash = std::uint32_t

Definition at line 51 of file vtkStringToken.h.

Constructor & Destructor Documentation

◆ vtkStringToken() [1/3]

VTK_WRAPEXCLUDE vtkStringToken::vtkStringToken ( const char *  data = nullptr,
std::size_t  size = std::string::npos 
)

Construct a token from a string literal.

◆ vtkStringToken() [2/3]

vtkStringToken::vtkStringToken ( const std::string &  data)

Construct a token from a std::string.

◆ vtkStringToken() [3/3]

constexpr vtkStringToken::vtkStringToken ( Hash  tokenId)
inlineconstexprnoexcept

Construct a token given its hash value.

NOTE: This will NOT insert a string into the manager as other constructors do.

Definition at line 59 of file vtkStringToken.h.

Member Function Documentation

◆ GetId()

Hash vtkStringToken::GetId ( ) const
inline

Return the token's ID (usually its hash but possibly not in the case of collisions).

Definition at line 65 of file vtkStringToken.h.

◆ GetHash()

unsigned int vtkStringToken::GetHash ( ) const
inline

A Python-wrappable (but less strongly typed) alternative to GetId()

Definition at line 67 of file vtkStringToken.h.

◆ Data()

const std::string& vtkStringToken::Data ( ) const

Return the string corresponding to the token.

◆ IsValid()

bool vtkStringToken::IsValid ( ) const

Return whether the token is valid or not.

Valid tokens are those whose hash is not equal to the hash of an empty string.

◆ HasData()

bool vtkStringToken::HasData ( ) const

Return whether a string is available for the token's hash ID.

◆ operator==()

bool vtkStringToken::operator== ( const vtkStringToken other) const

Fast equality comparison (compares hashes, not strings).

◆ operator!=()

bool vtkStringToken::operator!= ( const vtkStringToken other) const

Fast inequality comparison (compares hashes, not strings).

◆ operator<()

bool vtkStringToken::operator< ( const vtkStringToken other) const

Slow, but unsurprising string comparison (preserves lexical string ordering).

◆ operator>()

bool vtkStringToken::operator> ( const vtkStringToken other) const

◆ operator<=()

bool vtkStringToken::operator<= ( const vtkStringToken other) const

◆ operator>=()

bool vtkStringToken::operator>= ( const vtkStringToken other) const

◆ StringHash()

static constexpr Hash vtkStringToken::StringHash ( const char *  data,
std::size_t  size 
)
inlinestaticconstexprnoexcept

Return the hash of a string This is used internally but also by the ""_token() literal operator.

Definition at line 91 of file vtkStringToken.h.

◆ InvalidHash()

static Hash vtkStringToken::InvalidHash ( )
static

Return the hash code used to indicate an invalid (empty) token.

◆ AddChild()

bool vtkStringToken::AddChild ( vtkStringToken  member)

Methods to manage groups of tokens underneath a parent.

Grouping tokens provides applications a way to create and discover dynamic enumerations or sets of strings. For example, you might tokenize car and add tokens for car parts such body, motor, and wheels as children of car. Another library might then add more children such as windshield and hood; the application can present all of these children by asking for all the children of car.

Add member as a child of this token.

If true is returned, the member was added. If false is returned, the member was already part of the group or one of the tokens (this token or member) was invalid.

◆ RemoveChild()

bool vtkStringToken::RemoveChild ( vtkStringToken  member)

Remove a member from this token's children.

If this returns false, member was not a child of this token.

◆ Children()

std::unordered_set<vtkStringToken> vtkStringToken::Children ( bool  recursive = true)

Return all the children of this token.

◆ AllGroups()

static std::unordered_set<vtkStringToken> vtkStringToken::AllGroups ( )
static

Return all the tokens that have children.

Member Data Documentation

◆ Id

Hash vtkStringToken::Id
protected

Definition at line 126 of file vtkStringToken.h.


The documentation for this class was generated from the following file: