-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtamgumapl.h
executable file
·676 lines (525 loc) · 18 KB
/
tamgumapl.h
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
/*
* Tamgu (탐구)
*
* Copyright 2019-present NAVER Corp.
* under BSD 3-clause
*/
/* --- CONTENTS ---
Project : Tamgu (탐구)
Version : See tamgu.cxx for the version number
filename : tamgumapl.h
Date : 2017/09/01
Purpose : map implementation
Programmer : Claude ROUX (claude.roux@naverlabs.com)
Reviewer :
*/
#ifndef Tamgumapl_h
#define Tamgumapl_h
//#prime
#include "tamguint.h"
#include "tamgustring.h"
#include "tamguvector.h"
#include "tamguconstants.h"
#include "tamgulvector.h"
//We create a map between our methods, which have been declared in our class below. See MethodInitialization for an example
//of how to declare a new method.
class Tamgumapl;
//This typedef defines a type "maplMethod", which expose the typical parameters of a new Tamgu method implementation
typedef Tamgu* (Tamgumapl::*maplMethod)(Tamgu* contextualpattern, short idthread, TamguCall* callfunc);
//---------------------------------------------------------------------------------------------------------------------
class Tamgumapl : public TamguObjectLockContainer {
public:
//We export the methods that will be exposed for our new object
//this is a static object, which is common to everyone
//We associate the method pointers with their names in the linkedmethods map
static Exchanging basebin_hash<maplMethod> methods;
static Exchanging short idtype;
//---------------------------------------------------------------------------------------------------------------------
//This SECTION is for your specific implementation...
//Your personal variables here...
hmap<BLONG, Tamgu*> values;
bool isconst;
//---------------------------------------------------------------------------------------------------------------------
Tamgumapl(TamguGlobal* g, Tamgu* parent = NULL) : TamguObjectLockContainer(g, parent) {
//Do not forget your variable initialisation
isconst = false;
}
Tamgumapl() {
//Do not forget your variable initialisation
isconst = false;
}
//----------------------------------------------------------------------------------------------------------------------
Exporting Tamgu* Loopin(TamguInstruction*, Tamgu* context, short idthread);
Exporting Tamgu* Put(Tamgu* index, Tamgu* value, short idthread);
Tamgu* EvalWithSimpleIndex(Tamgu* key, short idthread, bool sign);
Exporting Tamgu* Eval(Tamgu* context, Tamgu* value, short idthread);
void SetConst() { isconst = true;}
short Type() {
return Tamgumapl::idtype;
}
void Setidtype(TamguGlobal* global);
string Typename() {
return "mapl";
}
bool isObjectContainer() {
return true;
}
bool isContainerClass() {
return true;
}
bool isMapContainer() {
return true;
}
bool duplicateForCall() {
return isconst;
}
void Setprotect(bool n) {
if (!lockingmark())
return;
protect = n;
hmap<BLONG, Tamgu*>::iterator it;
for (it = values.begin(); it != values.end(); it++)
it->second->Setprotect(n);
unlockingmark();
}
void Popping() {
if (!lockingmark())
return;
protect = false;
if (Reference() <= 0)
protect = true;
hmap<BLONG, Tamgu*>::iterator it;
for (it = values.begin(); it != values.end(); it++)
it->second->Popping();
unlockingmark();
}
void Protectcontainervalues() {
if (!lockingmark())
return;
protect = true;
hmap<BLONG, Tamgu*>::iterator it;
for (it = values.begin(); it != values.end(); it++)
it->second->Setprotect(true);
unlockingmark();
}
Tamgu* Atom(bool forced) {
if (forced) {
Tamgumapl* m = new Tamgumapl;
locking();
Tamgu* v;
for (const auto& it : values) {
v = it.second->Atom(true);
m->values[it.first] = v;
v->Setreference();
}
unlocking();
return m;
}
return this;
}
double Sum() {
locking();
double v = 0;
hmap<BLONG, Tamgu*>::iterator itx;
for (itx = values.begin(); itx != values.end(); itx++)
v += itx->second->Sum();
unlocking();
return v;
}
double Product() {
locking();
if (values.size() == 0) {
unlocking();
return 0;
}
double v = 1;
for (const auto& itx : values)
v *= itx.second->Product();
unlocking();
return v;
}
//---------------------------------------------------------------------------------------------------------------------
//Declaration
//All our methods must have been declared in tamguexportedmethods... See MethodInitialization below
bool isDeclared(short n) {
return methods.check(n);
}
Tamgu* Newvalue(Tamgu* a, short idthread) {
Tamgumapl* m = new Tamgumapl;
if (a->isContainer()) {
TamguIteration* it = a->Newiteration(false);
for (it->Begin(); it->End() == aFALSE; it->Next()) {
m->Push(it->Keylong(),it->IteratorValue());
}
it->Release();
return m;
}
hmap<BLONG,Tamgu*>::iterator ist;
for (ist=values.begin(); ist!=values.end();ist++)
m->Push(ist->first,a);
return m;
}
Tamgu* Newinstance(short idthread, Tamgu* f = NULL) {
return new Tamgumapl();
}
Exporting TamguIteration* Newiteration(bool direction);
static void AddMethod(TamguGlobal* g, string name, maplMethod func, unsigned long arity, string infos);
static bool InitialisationModule(TamguGlobal* g, string version);
void Methods(Tamgu* v) {
for (const auto& it : globalTamgu->infomethods[idtype])
v->storevalue(it.first);
}
string Info(string n) {
if (globalTamgu->infomethods[idtype].find(n) != globalTamgu->infomethods[idtype].end())
return globalTamgu->infomethods[idtype][n];
return "Unknown method";
}
//---------------------------------------------------------------------------------------------------------------------
void unmark() {
if (!lockingmark())
return;
usermark=false;
for (const auto& it : values)
it.second->unmark();
unlockingmark();
}
Exporting void Cleanreference(short inc);
Exporting void Setreference(short r);
Exporting void Setreference();
Exporting void Resetreference(short r = 1);
//---------------------------------------------------------------------------------------------------------------------
//This SECTION is for your specific implementation...
//This is an example of a function that could be implemented for your needs.
Tamgu* MethodInvert(Tamgu* contextualpattern, short idthread, TamguCall* callfunc) {
if (contextualpattern == this || !contextualpattern->isMapContainer() || !contextualpattern->isAffectation())
contextualpattern = Newinstance(idthread);
else
contextualpattern->Clear();
Tamgu* a;
for (const auto& it : values) {
a = globalTamgu->Providelong(it.first);
contextualpattern->Push(it.second, a);
a->Release();
}
return contextualpattern;
}
Exporting Tamgu* MethodFind(Tamgu* contextualpattern, short idthread, TamguCall* callfunc);
Tamgu* MethodClear(Tamgu* contextualpattern, short idthread, TamguCall* callfunc) {
Clear();
return aTRUE;
}
Tamgu* MethodItems(Tamgu* contextualpattern, short idthread, TamguCall* callfunc) {
Tamgumapl* item;
Tamgu* vect = Selectavector(contextualpattern);
for (const auto& itr : values) {
item=new Tamgumapl;
item->Push(itr.first, itr.second);
vect->Push(item);
}
return vect;
}
Tamgu* MethodMerge(Tamgu* contextualpattern, short idthread, TamguCall* callfunc) {
Tamgu* v = callfunc->Evaluate(0, contextualpattern, idthread);
Merging(v);
return this;
}
Tamgu* MethodSum(Tamgu* contextualpattern, short idthread, TamguCall* callfunc) {
double v = Sum();
return globalTamgu->ProvideConstfloat(v);
}
Tamgu* MethodKeys(Tamgu* contextualpattern, short idthread, TamguCall* callfunc) {
locking();
Tamgulvector* vstr = (Tamgulvector*)Selectalvector(contextualpattern);
hmap<BLONG, Tamgu*>::iterator it;
for (it = values.begin(); it != values.end(); it++)
vstr->values.push_back(it->first);
unlocking();
return vstr;
}
Tamgu* MethodValues(Tamgu* contextualpattern, short idthread, TamguCall* callfunc) {
locking();
Tamguvector* v = (Tamguvector*)Selectavector(contextualpattern);
hmap<BLONG, Tamgu*>::iterator it;
for (it = values.begin(); it != values.end(); it++)
v->Push(it->second);
unlocking();
return v;
}
Tamgu* MethodTest(Tamgu* contextualpattern, short idthread, TamguCall* callfunc) {
locking();
BLONG v = callfunc->Evaluate(0, contextualpattern, idthread)->Long();
bool res = values.find(v) != values.end();
unlocking();
return booleantamgu[res];
}
Tamgu* MethodProduct(Tamgu* contextualpattern, short idthread, TamguCall* callfunc) {
double v = Product();
return globalTamgu->ProvideConstfloat(v);
}
Tamgu* MethodPop(Tamgu* contextualpattern, short idthread, TamguCall* callfunc) {
Tamgu* pos = callfunc->Evaluate(0, contextualpattern, idthread);
return Pop(pos);
}
Tamgu* MethodJoin(Tamgu* contextualpattern, short idthread, TamguCall* callfunc) {
locking();
//The separator between keys
string keysep = callfunc->Evaluate(0, contextualpattern, idthread)->String();
//The separator between values
string sep = callfunc->Evaluate(1, contextualpattern, idthread)->String();
bool beg = true;
stringstream res;
for (const auto& it : values) {
if (beg == false)
res << sep;
beg = false;
res << it.first << keysep << it.second->String();
}
unlocking();
return globalTamgu->Providestring(res.str());
}
//------------------------------------------------------------------------------------------
Exporting Tamgu* Push(Tamgu* k, Tamgu* v);
Exporting Tamgu* Pop(Tamgu* k);
Tamgu* Push(BLONG k, Tamgu* a) {
locking();
Tamgu* v = values[k];
if (v != NULL) {
if (v == a)
return this;
v->Removereference(reference + 1);
}
a = a->Atom();
values[k] = a;
a->Addreference(investigate,reference + 1);
unlocking();
return this;
}
Tamgu* Combine(Tamgu* ke) {
//Three cases:
if (!ke->isContainer())
return this;
((TamguObject*)ke)->locking();
Tamguvector* v;
BLONG n;
TamguIteration* itr = ke->Newiteration(false);
for (itr->Begin(); itr->End() == aFALSE; itr->Next()) {
n = itr->Keylong();
if (values.find(n) == values.end())
Push(n, itr->Value());
else {
v = globalTamgu->Providevector();
v->Push(values[n]);
v->Push(itr->Value());
Push(n, v);
}
}
itr->Release();
((TamguObject*)ke)->unlocking();
return this;
}
Tamgu* Merging(Tamgu* ke) {
//Three cases:
if (!ke->isContainer())
return this;
((TamguObject*)ke)->locking();
BLONG n;
TamguIteration* itr = ke->Newiteration(false);
for (itr->Begin(); itr->End() == aFALSE; itr->Next()) {
n = itr->Keylong();
if (values.find(n) == values.end())
Push(n, itr->Value());
}
itr->Release();
((TamguObject*)ke)->unlocking();
return this;
}
//---------------------------------------------------------------------------------------------------------------------
//ExecuteMethod must be implemented in order to execute our new Tamgu methods. This method is called when a TamguCallMethodMethod object
//is returned by the Declaration method.
Tamgu* CallMethod(short idname, Tamgu* contextualpattern, short idthread, TamguCall* callfunc) {
//This call is a bit cryptic. It takes the method (function) pointer that has been associated in our map with "name"
//and run it with the proper parameters. This is the right call which should be invoked from within a class definition
return (this->*Tamgumapl::methods.get(idname))(contextualpattern, idthread, callfunc);
}
Exporting void Clear();
Exporting string String();
Exporting string JSonString();
Tamgu* Value(string& val) {
BLONG n = convertlong(val);
Tamgu* v;
if (globalTamgu->threadMODE) {
locking();
v = values[n];
if (v == NULL) {
values.erase(n);
v = aNOELEMENT;
}
unlocking();
return v;
}
v = values[n];
if (v == NULL) {
values.erase(n);
return aNOELEMENT;
}
return v;
}
Tamgu* Value(Tamgu* a) {
BLONG n = a->Long();
Tamgu* v;
if (globalTamgu->threadMODE) {
locking();
v = values[n];
if (v == NULL) {
values.erase(n);
v = aNOELEMENT;
}
unlocking();
return v;
}
v = values[n];
if (v == NULL) {
values.erase(n);
return aNOELEMENT;
}
return v;
}
Tamgu* Value(BLONG n) {
Tamgu* v;
if (globalTamgu->threadMODE) {
locking();
v = values[n];
if (v == NULL) {
values.erase(n);
v = aNOELEMENT;
}
unlocking();
return v;
}
v = values[n];
if (v == NULL) {
values.erase(n);
return aNOELEMENT;
}
return v;
}
Tamgu* Value(long n) {
Tamgu* v;
if (globalTamgu->threadMODE) {
locking();
v = values[n];
if (v == NULL) {
values.erase(n);
v = aNOELEMENT;
}
unlocking();
return v;
}
v = values[n];
if (v == NULL) {
values.erase(n);
return aNOELEMENT;
}
return v;
}
Tamgu* Value(double n) {
Tamgu* v;
if (globalTamgu->threadMODE) {
locking();
v = values[n];
if (v == NULL) {
values.erase(n);
v = aNOELEMENT;
}
unlocking();
return v;
}
v = values[n];
if (v == NULL) {
values.erase(n);
return aNOELEMENT;
}
return v;
}
Exporting long Integer();
Exporting double Float();
Exporting BLONG Long();
Exporting bool Boolean();
//Basic operations
Exporting long Size();
Exporting Tamgu* in(Tamgu* context, Tamgu* a, short idthread);
Exporting Tamgu* andset(Tamgu* a, bool itself);
Exporting Tamgu* orset(Tamgu* a, bool itself);
Exporting Tamgu* xorset(Tamgu* a, bool itself);
Exporting Tamgu* plus(Tamgu* a, bool itself);
Exporting Tamgu* minus(Tamgu* a, bool itself);
Exporting Tamgu* multiply(Tamgu* a, bool itself);
Exporting Tamgu* divide(Tamgu* a, bool itself);
Exporting Tamgu* divideinteger(Tamgu* a, bool itself);
Exporting Tamgu* power(Tamgu* a, bool itself);
Exporting Tamgu* shiftleft(Tamgu* a, bool itself);
Exporting Tamgu* shiftright(Tamgu* a, bool itself);
Exporting Tamgu* mod(Tamgu* a, bool itself);
Exporting Tamgu* same(Tamgu* a);
};
//--------------------------------------------------------------------------------------------------
class TamguIterationmapl : public TamguIteration {
public:
hmap<BLONG, Tamgu*>::iterator it;
Tamgumapl* map;
Locking _lock;
TamguIterationmapl(Tamgumapl* m, bool d, TamguGlobal* g = NULL) : map(m), _lock(m), TamguIteration(d, g) {}
void Setvalue(Tamgu* recipient, short idthread) {
recipient->storevalue(it->first);
}
Tamgu* Key() {
return globalTamgu->Providelong(it->first);
}
Tamgu* Value() {
return it->second;
}
string Keystring() {
return convertfromnumber(it->first);
}
string Valuestring() {
return it->second->String();
}
wstring Keyustring() {
return wconvertfromnumber(it->first);
}
wstring Valueustring() {
return it->second->UString();
}
BLONG Keylong() {
return it->first;
}
long Keyinteger() {
return it->first;
}
BLONG Valuelong() {
return it->second->Long();
}
long Valueinteger() {
return it->second->Float();
}
double Keyfloat() {
return it->first;
}
double Valuefloat() {
return it->second->Float();
}
void Next() {
it++;
}
Tamgu* End() {
if (it == map->values.end())
return aTRUE;
return aFALSE;
}
Tamgu* Begin() {
it = map->values.begin();
return aTRUE;
}
};
//-------------------------------------------------------------------------------------------
#endif