Class: AutoC::Hasher

Inherits:
Object
  • Object
show all
Includes:
Entity, STD, Singleton
Defined in:
lib/autoc/hashers.rb

Overview

Basic xor-shift incremental hasher

Constant Summary

Constants included from Entity

Entity::ReferenceSet

Constants included from STD

STD::ASSERT_H, STD::BOOL, STD::CHAR, STD::COMPLEX, STD::COMPLEX_H, STD::DOUBLE, STD::DOUBLE_COMPLEX, STD::DOUBLE_T, STD::FLOAT, STD::FLOAT_COMPLEX, STD::FLOAT_T, STD::INT, STD::INTMAX_T, STD::INTPTR_T, STD::INTTYPES_H, STD::LONG, STD::LONG_DOUBLE, STD::LONG_DOUBLE_COMPLEX, STD::LONG_LONG, STD::MALLOC_H, STD::MATH_H, STD::PTRDIFF_T, STD::SHORT, STD::SIGNED_CHAR, STD::SIZE_T, STD::STDBOOL_H, STD::STDDEF_H, STD::STDLIB_H, STD::STRING_H, STD::UINTMAX_T, STD::UINTPTR_T, STD::UNSIGNED, STD::UNSIGNED_CHAR, STD::UNSIGNED_LONG, STD::UNSIGNED_LONG_LONG, STD::UNSIGNED_SHORT, STD::WCHAR_T

Instance Method Summary collapse

Methods included from Entity

#<=>, #complexity, #dependencies, #forward_declarations, #implementation, #interface, #position, #references, #total_dependencies, #total_references

Constructor Details

#initializeHasher

Returns a new instance of Hasher.



28
# File 'lib/autoc/hashers.rb', line 28

def initialize = dependencies << STDLIB_H

Instance Method Details

#create(hasher) ⇒ Object



20
# File 'lib/autoc/hashers.rb', line 20

def create(hasher) = "#{hasher} = AUTOC_HASHER_SEED"

#destroy(hasher) ⇒ Object



22
# File 'lib/autoc/hashers.rb', line 22

def destroy(hasher) = nil

#render_forward_declarations(stream) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/autoc/hashers.rb', line 30

def render_forward_declarations(stream)
  stream << %{
    #include <limits.h>
    #include <stddef.h>
    #ifndef AUTOC_HASHER_SEED /* no seed is specified, using randomly generated one */
      #define _AUTOC_RANDOMIZE_SEED
      #define AUTOC_HASHER_SEED _autoc_hasher_seed
      AUTOC_EXTERN void
      #ifdef __POCC__
        __cdecl
      #endif
      _autoc_hasher_randomize_seed(void); /* invoke default seed randomizer */
      AUTOC_EXTERN size_t _autoc_hasher_seed;
    #elif ~(~AUTOC_HASHER_SEED + 1) == 1 /* if macro value is unspecified on the command line it is implicitly set to 1 */
      #undef AUTOC_HASHER_SEED
      #define AUTOC_HASHER_SEED 0 /* set seed's default value */
    #endif
  }
end

#render_implementation(stream) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/autoc/hashers.rb', line 50

def render_implementation(stream)
  # Predefined C compiler macros datasheet: https://sourceforge.net/p/predef/wiki/Compilers/
  stream << %{
    #ifdef _AUTOC_RANDOMIZE_SEED
      size_t _autoc_hasher_seed = 0; /* fallback default until _autoc_hasher_randomize_seed() is called */
      #if defined(__cplusplus)
        extern "C" void _autoc_hasher_randomize_seed(void);
      #elif defined(__GNUC__) || defined(__clang__)
        void _autoc_hasher_randomize_seed(void) __attribute__((__constructor__));
      #elif defined(__POCC__)
        #pragma startup _autoc_hasher_randomize_seed
      #elif defined(_MSC_VER) || defined(__POCC__)
        #pragma message("WARNING: _autoc_hasher_randomize_seed() will not be called automatically; either call it manually or compile this source as C++ in order to actually yield random seed")
      #else
        #warning _autoc_hasher_randomize_seed() will not be be called automatically; either call it manually or compile this source as C++ in order to actually yield random seed
      #endif
      #include <time.h>
      #ifdef __POCC__
        #include <stdlib.h>
      #endif
      void
      #ifdef __POCC__
        __cdecl
      #endif
      _autoc_hasher_randomize_seed(void) {
        #if defined(__POCC__)
          /* Pelles C check comes first as it might set _MSC_VER as well */
          unsigned r;
          _rand_s(&r);
          _autoc_hasher_seed = r;
        #elif defined(_MSC_VER)
          unsigned r;
          rand_s(&r);
          _autoc_hasher_seed = r;
        #else
          srand(time(NULL));
          _autoc_hasher_seed = rand();
        #endif
      }
      #ifdef __cplusplus
        static struct _hasher {
          _hasher() {_autoc_hasher_randomize_seed();}
        } _hasher;
      #endif

    #endif
  }
end

#result(hasher) ⇒ Object



26
# File 'lib/autoc/hashers.rb', line 26

def result(hasher) = hasher

#to_sObject



18
# File 'lib/autoc/hashers.rb', line 18

def to_s = :size_t

#update(hasher, value) ⇒ Object



24
# File 'lib/autoc/hashers.rb', line 24

def update(hasher, value) = "#{hasher} = ((#{hasher} << 1) | (#{hasher} >> (sizeof(#{hasher})*CHAR_BIT - 1))) ^ (#{value})"