-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashlib.cpp
204 lines (171 loc) · 5.09 KB
/
hashlib.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include "hashlib.h"
#include <string>
#include <iterator>
#include <math.h>
#include <stdlib.h>
#define HAVE_HASH_MUTATION 1
namespace Hashlib
{
using namespace Bang;
void BangHash::set( Stack& s ) // , CLOSURE_CREF running )
{
const Value& vkey = s.pop();
if (vkey.isstr())
{
const auto& key = vkey.tostr();
const Value& v = s.pop();
this->set( key, v );
}
}
DLLEXPORT void BangHash::set( const bangstring& key, const Value& v )
{
auto loc = this->find( key );
if (loc == hash_.end())
#if LCFG_HASHLIB_SIMPLEVEC
hash_.push_back( kvp_t( key, v ) );
#else
hash_.insert( kvp_t( key, v ) );
#endif
else
loc->second = v;
}
template <class T>
class BangMemFun : public Bang::Function
{
gcptr<T> cppfun_;
void (T::* memfun_)(Bang::Stack&);
public:
BangMemFun( T* bangfun, void (T::* memfun)(Bang::Stack&) )
: cppfun_( bangfun ),
memfun_( memfun )
{
}
void apply( Bang::Stack& s ) // , CLOSURE_CREF rc )
{
((*cppfun_).*memfun_)( s );
}
};
typedef BangMemFun<BangHash> BangHashMemFun;
#if LCFG_HASHLIB_SIMPLEVEC
std::vector< BangHash::kvp_t >::iterator BangHash::find( const bangstring& key )
{
for (auto it = hash_.begin(); it != hash_.end(); ++it)
{
if (it->first == key)
return it;
}
return hash_.end();
}
#endif
void BangHash::has( Stack& s ) // , CLOSURE_CREF running )
{
const Value& key = s.pop();
if (key.isstr())
{
const auto& keystr = key.tostr(); // .gethash();
auto loc = this->find( keystr );
s.push( loc != hash_.end() );
}
else
s.push( false );
}
void BangHash::keys( Stack& s ) // , CLOSURE_CREF running )
{
for ( auto hashel = hash_.begin(); hashel != hash_.end(); ++hashel ) // int i = 0; i < hash_.size(); ++i )
{
s.push( hashel->first ); // need to fix hashlib to store actual strings
}
}
void BangHash::indexOperator( const Bang::Value& msg, Stack& stack, const RunContext& )
{
if (msg.isstr())
{
const auto& key = msg.tostr();
{
// auto hash = key.gethash();
auto loc = this->find( key );
if (loc != hash_.end())
stack.push( loc->second );
}
}
}
DLLEXPORT void BangHash::apply( Stack& s ) // , CLOSURE_CREF running )
{
const Bang::Value& msg = s.pop();
Bang::RunContext* pctx = nullptr;
this->indexOperator( msg, s, *pctx );
}
void BangHash::customOperator( const bangstring& theOperator, Stack& s)
{
const static Bang::bangstring opHas("/has");
const static Bang::bangstring opKeys("/keys");
const static Bang::bangstring opSet("/set");
if (theOperator == opSet)
{
this->set(s);
}
else if (theOperator == opHas)
{
this->has(s);
}
else if (theOperator == opKeys)
{
this->keys(s);
}
else if (theOperator[0] == '>' && theOperator[1] == '>')
{
bangstring key( &theOperator.front(), theOperator.size()-2 );
const Value& v = s.pop();
this->set( key, v );
}
}
// struct HashOps : public Bang::Operators
// {
// HashOps()
// {
// customOperator = &BangHash::customOperator;
// }
// } gHashOperators;
DLLEXPORT BangHash::BangHash()
{
// operators = &gHashOperators;
}
void hashNew( Stack& s, const RunContext& rc )
{
auto hash = NEW_BANGFUN(BangHash);
#if LCFG_GCPTR_STD
hash->myself_ = hash;
#endif
s.push( STATIC_CAST_TO_BANGFUN(hash) );
}
void hashOfString( Stack& s, const RunContext& rc )
{
const Value& msg = s.pop();
if (msg.isstr())
{
const auto& key = msg.tostr();
s.push( (double)key.gethash() ); // ( StringHashFunction()( key ) ) );
}
}
void banghash_lookup( Bang::Stack& s, const Bang::RunContext& ctx )
{
const Bang::Value& v = s.pop();
if (!v.isstr())
throw std::runtime_error("Array library . operator expects string");
const auto& str = v.tostr();
const Bang::tfn_primitive p =
( str == "of-string" ? &hashOfString
: str == "new" ? &hashNew // test
: nullptr
);
if (p)
s.push( p );
else
throw std::runtime_error("Hash library does not implement: " + std::string(str));
}
} // end namespace Hashlib
extern "C" DLLEXPORT
void bang_hashlib_open( Bang::Stack* stack, const Bang::RunContext* )
{
stack->push( &Hashlib::banghash_lookup );
}