SystemEX  Lacking
Additional generic collection types missing in .net
Examples.SensorData Struct Reference

Simple example showing how a struct can participate in the SystemEx hashing system using IHashable<SensorData>. More...

Inheritance diagram for Examples.SensorData:
Collaboration diagram for Examples.SensorData:

Public Member Functions

 SensorData ()
 Initializes the struct with example values.
FixedVector< byte > ToBytes ()
 Converts this struct into a deterministic byte sequence.
override int GetHashCode ()
 Computes a 32‑bit hash for this struct.
ulong GetHashCodeLong ()
 Computes a 64‑bit hash for this struct.

Public Attributes

int Id
 Example fields that will be included in the hash.
float Value

Detailed Description

Simple example showing how a struct can participate in the SystemEx hashing system using IHashable<SensorData>.

This demonstrates:

  • How to attach a hash algorithm using the HashAlgorithm attribute
  • How to provide a deterministic byte representation via ToBytes()
  • How HashFactory computes 32‑bit and 64‑bit hashes for structs

Constructor & Destructor Documentation

◆ SensorData()

Examples.SensorData.SensorData ( )

Initializes the struct with example values.

91 {
92 Id = 748;
93 Value = 1.00928f;
94 }

Member Function Documentation

◆ GetHashCode()

override int Examples.SensorData.GetHashCode ( )

Computes a 32‑bit hash for this struct.

How it works:

  1. HashFactory reads the HashAlgorithm attribute on this struct.
  2. It creates the specified hasher (BernsteinHash).
  3. It calls ToBytes() to get the raw data.
  4. It computes a 32‑bit hash using the given seed.

If the hash result is non‑zero, it is returned. Otherwise, the fallback is the default .NET hash code.

132 {
133 var x = HashFactory.Hash32(this, RandUtils.RandUInt(uint.MinValue, uint.MaxValue, Endian.System) );
134 if ( x.Value != 0 ) return (int)x.Value;
135
136 return base.GetHashCode();
137 }
Endian
Specifies the byte order used when converting values to and from raw byte sequences.
Definition Conversion.cs:29

◆ GetHashCodeLong()

ulong Examples.SensorData.GetHashCodeLong ( )

Computes a 64‑bit hash for this struct.

This works exactly like GetHashCode(), but produces a 64‑bit value. If the computed hash is zero, the method returns 0 as a fallback.

144 {
145 var x = HashFactory.Hash64(this, RandUtils.RandUInt(uint.MinValue, uint.MaxValue, Endian.System) );
146 if ( x.Value != 0 ) return x.Value;
147
148 return 0;
149 }

◆ ToBytes()

FixedVector< byte > Examples.SensorData.ToBytes ( )

Converts this struct into a deterministic byte sequence.

Important notes for beginners:

  • The order of fields must never change.
  • The endianness must be chosen intentionally.
  • The size must always stay the same (here: 8 bytes).

HashFactory will use these bytes as input for the selected hash algorithm.

Implements SystemEx.Hash.IHashable< T >.

105 {
106 var b = new FixedVector<byte>(8);
107
108
109 // Convert the integer field into 4 bytes.
110 // Endian.System means: use the machine's native endianness.
111 b.ReplaceRange(0, Id.ToBytes(Endian.System));
112
113 // Convert the float field into 4 bytes.
114 // Here we intentionally use BigEndian to show that each field
115 // can choose its own byte order if needed.
116 b.ReplaceRange(4, Value.ToBytes(Endian.BigEndian));
117
118 return b;
119 }

Member Data Documentation

◆ Id

int Examples.SensorData.Id

Example fields that will be included in the hash.


The documentation for this struct was generated from the following file:
  • ExampleHasher.cs