Objectively 1.0.0
Ultra-lightweight object oriented framework for GNU C.
Macros | Functions
Hash.h File Reference

Utilities for calculating hash values. More...

#include <Objectively/Object.h>

Go to the source code of this file.

Macros

#define HASH_SEED   13
 The hash seed value. More...
 

Functions

OBJECTIVELY_EXPORT int HashForBytes (int hash, const uint8_t *bytes, const Range range)
 Accumulates the hash value of bytes into hash. More...
 
OBJECTIVELY_EXPORT int HashForCharacters (int hash, const char *chars, const Range range)
 Accumulates the hash value of chars into hash. More...
 
OBJECTIVELY_EXPORT int HashForCString (int hash, const char *chars)
 Accumulates the hash value of the null-terminated string into hash. More...
 
OBJECTIVELY_EXPORT int HashForDecimal (int hash, const double decimal)
 Accumulates the hash value of decimal into hash. More...
 
OBJECTIVELY_EXPORT int HashForInteger (int hash, const long integer)
 Accumulates the hash value of integer into hash. More...
 
OBJECTIVELY_EXPORT int HashForObject (int hash, const ident obj)
 Accumulates the hash value of object into hash. More...
 

Detailed Description

Utilities for calculating hash values.

Definition in file Hash.h.

Macro Definition Documentation

◆ HASH_SEED

#define HASH_SEED   13

The hash seed value.

Definition at line 37 of file Hash.h.

Function Documentation

◆ HashForBytes()

OBJECTIVELY_EXPORT int HashForBytes ( int  hash,
const uint8_t *  bytes,
const Range  range 
)

Accumulates the hash value of bytes into hash.

Parameters
hashThe hash accumulator.
bytesThe bytes to hash.
rangeThe Range to hash.
Returns
The accumulated hash value.

Definition at line 28 of file Hash.c.

28 {
29
30 for (size_t i = range.location; i < range.length; i++) {
31
32 int shift;
33 if (i & 1) {
34 shift = 16 + (i % 16);
35 } else {
36 shift = (i % 16);
37 }
38
39 hash += 31 * ((int) bytes[i]) << shift;
40 }
41
42 return hash;
43}
int hash(const Object *self)
Definition: Array.c:80
ssize_t location
The location.
Definition: Types.h:59
size_t length
The length.
Definition: Types.h:64

◆ HashForCharacters()

OBJECTIVELY_EXPORT int HashForCharacters ( int  hash,
const char *  chars,
const Range  range 
)

Accumulates the hash value of chars into hash.

Parameters
hashThe hash accumulator.
charsThe characters to hash.
rangeThe Range to hash.
Returns
The accumulated hash value.

Definition at line 45 of file Hash.c.

45 {
46 return HashForBytes(hash, (const uint8_t *) chars, range);
47}
int HashForBytes(int hash, const uint8_t *bytes, const Range range)
Accumulates the hash value of bytes into hash.
Definition: Hash.c:28

◆ HashForCString()

OBJECTIVELY_EXPORT int HashForCString ( int  hash,
const char *  chars 
)

Accumulates the hash value of the null-terminated string into hash.

Parameters
hashThe hash accumulator.
charsThe null-terminated C string.
Returns
The accumulated hash value.

Definition at line 49 of file Hash.c.

49 {
50
51 if (string) {
52 return HashForCharacters(hash, string, (Range) { 0, strlen(string) });
53 }
54
55 return 0;
56}
int HashForCharacters(int hash, const char *chars, const Range range)
Accumulates the hash value of chars into hash.
Definition: Hash.c:45
A location and length into contiguous collections.
Definition: Types.h:54

◆ HashForDecimal()

OBJECTIVELY_EXPORT int HashForDecimal ( int  hash,
const double  decimal 
)

Accumulates the hash value of decimal into hash.

Parameters
hashThe hash accumulator.
decimalThe decimal to hash.
Returns
The accumulated hash value.

Definition at line 58 of file Hash.c.

58 {
59 return hash + 31 * (int) decimal;
60}

◆ HashForInteger()

OBJECTIVELY_EXPORT int HashForInteger ( int  hash,
const long  integer 
)

Accumulates the hash value of integer into hash.

Parameters
hashThe hash accumulator.
integerThe integer to hash.
Returns
The accumulated hash value.

Definition at line 62 of file Hash.c.

62 {
63 return hash + 31 * (int) integer;
64}

◆ HashForObject()

OBJECTIVELY_EXPORT int HashForObject ( int  hash,
const ident  obj 
)

Accumulates the hash value of object into hash.

Parameters
hashThe hash accumulator.
objThe Object to hash.
Returns
The accumulated hash value.

Definition at line 66 of file Hash.c.

66 {
67
68 if (obj) {
69 return hash + 31 * $(cast(Object, obj), hash);
70 }
71
72 return 0;
73}
#define obj
#define cast(type, obj)
Safely cast obj to type.
Definition: Class.h:165
Object is the root Class of The Objectively Class hierarchy.
Definition: Object.h:46