Repository navigation
Hashvalue
Home › Miscellaneous Keywords › HashValue
Returns the hash Map and Set use for a key: any built-in, derive hash or Hashable type.
declare function hashvalue( byref k as any_type ) as ulongintresult = hashvalue( k )-
k: the value to hash.
The hash of k, as a ulongint. Equal values give equal hashes; different values may share one.
hashvalue is the hash Map and Set use for their keys. It is built into the compiler, so it needs no #import, and it works inside a generic body for whatever type the body is instantiated with.
What it does depends on the type of k:
Type of k
|
Hash |
|---|---|
| an integer type | the value itself, as an unsigned number of its own size: an int8 of -1 hashes as &hFF
|
boolean |
1 for true, 0 for false |
| an enum | its value, as a 32-bit integer |
single, double
|
from the value's bits; -0.0 hashes like 0.0, because they compare equal |
string |
its characters, up to the first NUL |
ustring |
all of its code units |
zstring ptr, wstring ptr, zstring * N, wstring * N
|
the characters, up to the terminating NUL |
| any other pointer or procedure pointer | the address, mixed through containers.HashInt
|
a type with derive equal, hash
|
its fields, folded as Derive describes |
a type with a HashOf( ) as ulongint member, such as one that extends containers.Hashable
|
a call to that member |
Anything else is an error (TKO1266), naming the type:
type Cell
as int32 row, col
end type
dim as Cell c
print hashvalue( c )error TKO1266: hashvalue( ) cannot hash 'cell': it is not a built-in type, has no 'derive hash' and no HashOf( ) method
The values are the ones containers.HashOf gives for the same value, and they are the same on every target, except that a wstring is 4 bytes on Linux and 2 on Windows.
A string stops at its first NUL, so chr( 0 ) & "x" hashes like "". That is a collision only: = still tells them apart, so a Map keeps both. A ustring hashes all of its code units.
#import "std/containers"
type Point
derive equal, hash
as int32 x, y
end type
type Tag extends containers.Hashable
as int32 id
declare function HashOf( ) as ulongint override
end type
function Tag.HashOf( ) as ulongint
return containers.HashInt( id )
end function
dim as string s = "abc"
dim as Point p = ( 2, 3 ), q = ( 2, 3 )
dim as Tag t
t.id = 7
print hex( hashvalue( 42 ) ) '' an integer hashes as its value
print hex( hashvalue( s ) ) '' a string by its characters
print cbool( hashvalue( s ) = hashvalue( "abc" ) ) '' ...so a literal finds it
print cbool( hashvalue( 0.0 ) = hashvalue( -0.0 ) ) '' equal floats, equal hashes
print cbool( hashvalue( p ) = hashvalue( q ) ) '' derive hash: from the fields
print cbool( hashvalue( t ) = t.HashOf( ) ) '' a Hashable: its HashOf( )
print cbool( hashvalue( chr( 0 ) & "x" ) = hashvalue( "" ) ) '' a string stops at its first NUL2A
E71FA2190541574B
true
true
true
true
true
-
A key type of your own needs
derive equal, hash, or extendscontainers.Hashableand overridesHashOf( )(with anoperator =of its own). Either way, equal keys hash equal; withderivethat holds by construction. -
Not a reserved word. A procedure, variable or other symbol of your own named
hashvaluewins in your own code, and the built-in is then out of reach there. Map and Set are unaffected: their bodies belong tostd/containers, and inside a module's code the name is the built-in unless the module declares its own. -
__TIKO_HASHVALUE__is defined when the compiler hashashvalue; see__TIKO_COMPILER__. -
hashvalueis a tiko addition.
- Reflection Tutorial
Derive- Map · Set
- Hash — hashes for checksums and passwords, which are a different job
Category: Miscellaneous Keywords Kind: function
tiko-lang · Home · Feature Index · Keyword Index · License
FreeBASIC manual pages reproduced under the GNU FDL 1.3.
- Home
- What Is tiko-lang
- Quick Start
- Installing on macOS
- Android (experimental)
- Debugging
- Feature Index
- Compatibility
- FAQ
- AI Cheatsheet
- Generics
- Interfaces
- For Each
- Iterators
- Tuples
- Array Results
- Defer
- Move Semantics
- Match
- Lambdas
- Block Strings
- String Interpolation
- Partial Types
- String Slices
- Array Slices
- Embed
- Typed Variadics
- C Header Import
- Foreign Library Linking
- UString
- Fixed-Width Types
- Nullptr
- String Library
- Format
- Array
- Map
- Set
- LinkedList
- OrderedMap
- OrderedSet
- Sorting
- Optional
- Result
- Smart Pointers
- Date and Time (Chrono)
- Time Zones
- Filesystem
- File Streams
- Config (INI and TOML)
- Codec
- Hash
- UUID
- OS
- Hardware
- Process
- Net
- HTTP and TLS
- Math Library
- Finance
- Logging
- Terminal TUI Library
- tkControls
- sys/win
- Goroutines and Channels
- LLVM Backend
- tiko build
- tiko test
- tiko doc
- TIKO_COMPILER
- Variable Declarations
- Standard Data Types
- User Defined Types
- Procedures
- Operators
- Control Flow Statements
- Converting Data Types
- Array Functions
- String Functions
- Mathematical Functions
- Bit Manipulation
- Date and Time Functions
- File I/O Functions
- Console Functions
- User Input (Runtime Library)
- User Input Functions (Graphics Library)
- Screen Functions
- 2D Drawing Functions
- Graphics Library Reference
- Graphics Keyword List
- Memory Functions
- Threading Support Functions
- Error Handling Functions
- Operating System Functions
- Preprocessor
- Intrinsic Defines
- Miscellaneous Keywords
- Modularizing