Objectively 1.0.0
Ultra-lightweight object oriented framework for GNU C.
Functions
Hash.c File Reference
#include <string.h>
#include "Hash.h"

Go to the source code of this file.

Functions

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

Function Documentation

◆ HashForBytes()

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()

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()

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()

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()

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()

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