VTK  9.4.20241221
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.
 
 vtkStringToken (const std::string &data)
 Construct a token from a std::string.
 
constexpr vtkStringToken (Hash tokenId) noexcept
 Construct a token given its hash value.
 
Hash GetId () const
 Return the token's ID (usually its hash but possibly not in the case of collisions).
 
unsigned int GetHash () const
 A Python-wrappable (but less strongly typed) alternative to GetId()
 
const std::string & Data () const
 Return the string corresponding to the token.
 
bool IsValid () const
 Return whether the token is valid or not.
 
bool HasData () const
 Return whether a string is available for the token's hash ID.
 
bool operator== (const vtkStringToken &other) const
 Fast equality comparison (compares hashes, not strings).
 
bool operator!= (const vtkStringToken &other) const
 Fast inequality comparison (compares hashes, not strings).
 
bool operator< (const vtkStringToken &other) const
 Slow, but unsurprising string comparison (preserves lexical string ordering).
 
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.
 
static Hash InvalidHash ()
 Return the hash code used to indicate an invalid (empty) token.
 

Protected Attributes

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

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.

Examples:
vtkStringToken (Examples)
Tests:
vtkStringToken (Tests)

Definition at line 56 of file vtkStringToken.h.

Member Typedef Documentation

◆ Hash

using vtkStringToken::Hash = std::uint32_t

Definition at line 59 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 67 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 73 of file vtkStringToken.h.

◆ GetHash()

unsigned int vtkStringToken::GetHash ( ) const
inline

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

Definition at line 75 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 99 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 134 of file vtkStringToken.h.


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