Objectively
Ultra-lightweight object oriented framework for GNU C.
Loading...
Searching...
No Matches
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.
 
int HashForCharacters (int hash, const char *chars, const Range range)
 Accumulates the hash value of chars into hash.
 
int HashForCString (int hash, const char *string)
 Accumulates the hash value of the null-terminated string into hash.
 
int HashForDecimal (int hash, const double decimal)
 Accumulates the hash value of decimal into hash.
 
int HashForInteger (int hash, const long integer)
 Accumulates the hash value of integer into hash.
 
int HashForObject (int hash, const ident obj)
 Accumulates the hash value of object into hash.
 

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 unsigned int uhash = (unsigned int) hash;
31
32 for (size_t i = range.location; i < range.location + range.length; i++) {
33
34 int shift;
35 if (i & 1) {
36 shift = 16 + (i % 16);
37 } else {
38 shift = (i % 16);
39 }
40
41 uhash += 31U * ((unsigned int) bytes[i]) << shift;
42 }
43
44 return (int) uhash;
45}
static int hash(const Object *self)
Definition Array.c:129
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 47 of file Hash.c.

47 {
48 return HashForBytes(hash, (const uint8_t *) chars, range);
49}
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 51 of file Hash.c.

51 {
52
53 if (string) {
54 return HashForCharacters(hash, string, (Range) { 0, strlen(string) });
55 }
56
57 return 0;
58}
int HashForCharacters(int hash, const char *chars, const Range range)
Accumulates the hash value of chars into hash.
Definition Hash.c:47
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 60 of file Hash.c.

60 {
61 unsigned int uhash = (unsigned int) hash;
62 uhash += 31U * (unsigned int) decimal;
63 return (int) uhash;
64}

◆ 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 66 of file Hash.c.

66 {
67 unsigned int uhash = (unsigned int) hash;
68 uhash += 31U * (unsigned int) integer;
69 return (int) uhash;
70}

◆ 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 72 of file Hash.c.

72 {
73
74 if (obj) {
75 unsigned int uhash = (unsigned int) hash;
76 uhash += 31U * (unsigned int) $(cast(Object, obj), hash);
77 return (int) uhash;
78 }
79
80 return 0;
81}
#define obj
#define cast(type, obj)
Safely cast obj to type.
Definition Class.h:182
Object is the root Class of The Objectively Class hierarchy.
Definition Object.h:46