forked from RefPerSys/RefPerSys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inline_rps.hh
2395 lines (2069 loc) · 69.7 KB
/
inline_rps.hh
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
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************
* file inline_rps.hh
* SPDX-License-Identifier: GPL-3.0-or-later
*
* Description:
* This file is part of the Reflective Persistent System.
* It is its internal C++ header file for inlined functions.
*
* Author(s):
* Basile Starynkevitch <[email protected]>
* Abhishek Chakravarti <[email protected]>
* Nimesh Neema <[email protected]>
*
* © Copyright 2019 - 2022 The Reflective Persistent System Team
* [email protected] & http://refpersys.org/
*
* License:
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
#ifndef INLINE_RPS_INCLUDED
#define INLINE_RPS_INCLUDED
//// this file should only be included at end of refpersys.hh
#ifndef REFPERSYS_INCLUDED
#error wrong direct inclusion of "inline_rps.hh"
#endif /*REFPERSYS_INCLUDED*/
//////////////////////////////////////////////////////////// time functions
// see http://man7.org/linux/man-pages/man2/clock_gettime.2.html
static inline double
rps_wallclock_real_time(void)
{
struct timespec ts = {0,0};
if (clock_gettime(CLOCK_REALTIME, &ts))
return NAN;
return 1.0*ts.tv_sec + 1.0e-9*ts.tv_nsec;
} // end rps_wallclock_real_time
static inline double
rps_monotonic_real_time(void)
{
struct timespec ts = {0,0};
if (clock_gettime(CLOCK_MONOTONIC, &ts))
return NAN;
return 1.0*ts.tv_sec + 1.0e-9*ts.tv_nsec;
} // end rps_monotonic_real_time
static inline double
rps_process_cpu_time(void)
{
struct timespec ts = {0,0};
if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts))
return NAN;
return 1.0*ts.tv_sec + 1.0e-9*ts.tv_nsec;
} // end rps_process_cpu_time
static inline double
rps_thread_cpu_time(void)
{
struct timespec ts = {0,0};
if (clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts))
return NAN;
return 1.0*ts.tv_sec + 1.0e-9*ts.tv_nsec;
} // end rps_thread_cpu_time
/// name of current pthread
static inline std::string
rps_current_pthread_name(void)
{
char pthnam[24];
memset (pthnam, 0, sizeof(pthnam));
if (pthread_getname_np(pthread_self(), pthnam, sizeof(pthnam)))
snprintf(pthnam, sizeof(pthnam)-1, "*pthr#%ld*",
(long)pthread_self());
return std::string{pthnam};
} // end rps_current_pthread_name
//////////////////////////////////////////////////////////// objids
std::string
Rps_Id::to_string() const
{
char cbuf[24];
memset (cbuf, 0, sizeof(cbuf));
to_cbuf24(cbuf);
return std::string(cbuf);
} // end of Rps_Id::to_string
static inline
std::ostream& operator << (std::ostream& out, const Rps_Id id)
{
char cbuf[24];
memset (cbuf, 0, sizeof(cbuf));
id.to_cbuf24(cbuf);
out << cbuf;
return out;
} // end output of Rps_Id
//////////////////////////////////////////////////////////// values
Rps_Value::Rps_Value() : _wptr(nullptr) {};
void Rps_Value::clear()
{
_wptr = nullptr;
};
Rps_Value::Rps_Value(const void*ptr, const Rps_PayloadSymbol*symb) : _wptr(ptr)
{
RPS_ASSERT(symb != nullptr && symb->stored_type() == Rps_Type::PaylSymbol);
};
Rps_Value::Rps_Value(const void*ptr, Rps_CallFrame*cframe) : _wptr(ptr)
{
RPS_ASSERT(cframe != nullptr && cframe->stored_type() == Rps_Type::CallFrame);
};
Rps_Value::~Rps_Value()
{
_wptr = nullptr;
};
Rps_Value::Rps_Value(std::nullptr_t) : _wptr(nullptr) {};
Rps_Value::Rps_Value(Rps_EmptyTag) : _wptr (RPS_EMPTYSLOT) {};
Rps_Value::Rps_Value(intptr_t i, Rps_IntTag) :
_ival((i<< 1) | 1) {};
Rps_Value
Rps_Value::make_tagged_int(intptr_t i)
{
return Rps_Value(i, Rps_IntTag{});
} // end Rps_Value::make_tagged_int
Rps_Value::Rps_Value(const Rps_ZoneValue*ptr, Rps_ValPtrTag) :
_pval(ptr)
{
RPS_ASSERT(ptr == nullptr || ptr == RPS_EMPTYSLOT
|| (((intptr_t)ptr) & (sizeof(void*)-1)) == 0);
}
Rps_Value::Rps_Value(Rps_Value&&oth) :
_wptr(oth._wptr) {};
bool Rps_Value::is_int() const
{
return (_ival & 1) != 0;
};
bool Rps_Value::is_ptr() const
{
return !is_int()
&& _wptr != nullptr
&& _wptr != RPS_EMPTYSLOT
&& (((intptr_t)_pval) & (sizeof(void*)-1)) == 0;
}
Rps_Type
Rps_Value::type() const
{
if (is_int())
return Rps_Type::Int;
else if (is_empty())
return Rps_Type::None;
else
return _pval->type();
} // end Rps_Value::type()
const Rps_ZoneValue*
Rps_Value::as_ptr() const
{
if (is_ptr())
return _pval;
else if (is_int())
{
char buf[80];
memset(buf, 0, sizeof(buf));
snprintf(buf, sizeof(buf), "Rps_Value::as_ptr: given tagged int %ld",
(long) as_int());
throw std::runtime_error(buf);
}
else
{
char buf[64];
memset(buf, 0, sizeof(buf));
snprintf(buf, sizeof(buf), "Rps_Value::as_ptr: value@%p is not genuine pointer",
(void*)this);
throw std::runtime_error(buf);
}
} // end Rps_Value::as_ptr()
const Rps_ZoneValue*
Rps_Value::to_ptr(const Rps_ZoneValue*defzp) const
{
if (is_ptr()) return _pval;
else return defzp;
}
void
Rps_Value::gc_mark(Rps_GarbageCollector&gc, unsigned depth) const
{
if (!is_ptr()) return;
if (_pval->is_gcmarked(gc)) return;
Rps_ZoneValue* pzv = const_cast<Rps_ZoneValue*>(_pval);
pzv->set_gcmark(gc);
if (RPS_UNLIKELY(depth > max_gc_mark_depth))
throw std::runtime_error("too deep gc_mark");
pzv->gc_mark(gc, depth);
} // end Rps_Value::gc_mark
void
Rps_Value::output(std::ostream&out, unsigned depth) const
{
if (is_int())
out << as_int();
else if (is_empty())
out << "___";
else if (is_ptr())
as_ptr()->val_output(out,depth);
} // end Rps_Value::output
const void*
Rps_Value::data_for_symbol(Rps_PayloadSymbol*symb) const
{
RPS_ASSERT(dynamic_cast<Rps_PayloadSymbol*>(symb) != nullptr);
return _wptr;
} // end Rps_Value::data_for_symbol
bool
Rps_Value::is_object() const
{
return is_ptr()
&& as_ptr()->stored_type() == Rps_Type::Object;
} //end Rps_Value::is_object()
bool
Rps_Value::is_instance() const
{
return is_ptr()
&& as_ptr()->stored_type() == Rps_Type::Instance;
} //end Rps_Value::is_instance()
Rps_HashInt
Rps_Value::valhash() const noexcept
{
if (is_empty()) return 0;
if (is_int())
{
intptr_t i = to_int();
Rps_HashInt h = i ^ (i>>27);
if (RPS_UNLIKELY(!h))
h = (i & 0xfffff) + 13;
RPS_ASSERT(h != 0);
return h;
}
else if (is_ptr())
{
const Rps_ZoneValue*pval = as_ptr();
RPS_ASSERT(pval != nullptr);
return pval->val_hash();
};
return 0;
} // end of Rps_Value::valhash()
Rps_Value&
Rps_Value::operator=(Rps_Value&&r)
{
_wptr = r._wptr;
return *this;
}
Rps_Value&
Rps_Value::operator=(const Rps_Value&oth)
{
_wptr = oth._wptr;
return *this;
}
bool Rps_Value::is_set() const
{
return is_ptr()
&& as_ptr()->stored_type() == Rps_Type::Set;
} //end Rps_Value::is_set()
bool Rps_Value::is_closure() const
{
return is_ptr()
&& as_ptr()->stored_type() == Rps_Type::Closure;
} //end Rps_Value::is_closure()
bool Rps_Value::is_tuple() const
{
return is_ptr()
&& as_ptr()->stored_type() == Rps_Type::Tuple;
} //end Rps_Value::is_tuple()
bool Rps_Value::is_lextoken() const
{
return is_ptr()
&& as_ptr()->stored_type() == Rps_Type::LexToken;
} //end Rps_Value::is_lextoken()
bool Rps_Value::is_string() const
{
return is_ptr()
&& as_ptr()->stored_type() == Rps_Type::String;
} //end Rps_Value::is_string()
bool Rps_Value::is_double() const
{
return is_ptr()
&& as_ptr()->stored_type() == Rps_Type::Double;
} //end Rps_Value::is_double()
bool Rps_Value::is_json() const
{
return is_ptr()
&& as_ptr()->stored_type() == Rps_Type::Json;
} //end Rps_Value::is_json()
const Rps_SetOb*
Rps_Value::as_set() const
{
if (is_set())
return reinterpret_cast<const Rps_SetOb*>(_pval);
else throw std::domain_error("Rps_Value::as_set: value is not genuine set");
} // end Rps_Value::as_set
const Rps_SetOb*
Rps_Value::to_set(const Rps_SetOb*defset) const
{
if (is_set())
return reinterpret_cast<const Rps_SetOb*>(const_cast<Rps_ZoneValue*>(_pval));
else return defset;
} // end Rps_Value::to_set
const Rps_ClosureZone*
Rps_Value::as_closure() const
{
if (is_closure())
return reinterpret_cast<const Rps_ClosureZone*>(_pval);
else throw std::domain_error("Rps_Value::as_closure: value is not genuine closure");
} // end Rps_Value::as_closure
const Rps_ClosureZone*
Rps_Value::to_closure(const Rps_ClosureZone*defcloz) const
{
if (is_closure())
return reinterpret_cast<const Rps_ClosureZone*>(const_cast<Rps_ZoneValue*>(_pval));
else return defcloz;
} // end Rps_Value::to_closure
const Rps_JsonZone*
Rps_Value::as_json() const
{
if (is_json())
return reinterpret_cast<const Rps_JsonZone*>(_pval);
else throw std::domain_error("Rps_Value::as_json: value is not genuine JSON");
} // end Rps_Value::as_json
//////////////////////////////////////////////// routines common to
//////////////////////////////////////////////// sequence of objects
template<typename RpsSeq, Rps_Type seqty, unsigned k1, unsigned k2, unsigned k3>
void
Rps_SeqObjRef<RpsSeq,seqty,k1,k2,k3>::iterate_apply0(Rps_CallFrame*callerf, const Rps_Value closvarg)
{
RPS_LOCALFRAME(compute_class(callerf),
callerf,
Rps_ObjectRef curob;
Rps_Value closv;
Rps_Value mainv;
Rps_Value xtrav;
);
if (!closvarg.is_closure())
return;
_f.closv = closvarg;
for (auto it: *this)
{
_f.mainv = nullptr;
_f.xtrav = nullptr;
_f.curob = it;
Rps_TwoValues two = _f.closv->as_closure().apply1(&_, _f.curob);
_f.mainv = two.main();
_f.xtrav = two.xtra();
if (!_f.mainv && !_f.xtrav)
break;
}
} // end Rps_SeqObjRef::iterate_apply0
template<typename RpsSeq, Rps_Type seqty, unsigned k1, unsigned k2, unsigned k3>
void
Rps_SeqObjRef<RpsSeq,seqty,k1,k2,k3>::reverse_iterate_apply0(Rps_CallFrame*callerf, const Rps_Value closvarg)
{
RPS_LOCALFRAME(compute_class(callerf),
callerf,
Rps_ObjectRef curob;
Rps_Value closv;
Rps_Value mainv;
Rps_Value xtrav;
);
if (!closvarg.is_closure())
return;
_f.closv = closvarg;
const unsigned len = cnt();
for (int ix = (int)len-1; ix>=0; ix--)
{
_f.mainv = nullptr;
_f.xtrav = nullptr;
_f.curob = _seqob[ix];
Rps_TwoValues two = _f.closv->as_closure().apply1(&_, _f.curob);
_f.mainv = two.main();
_f.xtrav = two.xtra();
if (!_f.mainv && !_f.xtrav)
break;
}
} // end Rps_SeqObjRef::reverse_iterate_apply0
template<typename RpsSeq, Rps_Type seqty, unsigned k1, unsigned k2, unsigned k3>
void
Rps_SeqObjRef<RpsSeq,seqty,k1,k2,k3>::iterate_apply1(Rps_CallFrame*callerf, const Rps_Value closvarg, const Rps_Value arg0arg)
{
RPS_LOCALFRAME(compute_class(callerf),
callerf,
Rps_ObjectRef curob;
Rps_Value closv;
Rps_Value mainv;
Rps_Value xtrav;
Rps_Value arg0v;
);
if (!closvarg.is_closure())
return;
_f.closv = closvarg;
_f.arg0 = arg0arg;
for (auto it: *this)
{
_f.mainv = nullptr;
_f.xtrav = nullptr;
_f.curob = it;
Rps_TwoValues two = _f.closv->as_closure().apply2(&_, _f.curob, _f.arg0);
_f.mainv = two.main();
_f.xtrav = two.xtra();
if (!_f.mainv && !_f.xtrav)
break;
}
} // end Rps_SeqObjRef::iterate_apply1
template<typename RpsSeq, Rps_Type seqty, unsigned k1, unsigned k2, unsigned k3>
void
Rps_SeqObjRef<RpsSeq,seqty,k1,k2,k3>::reverse_iterate_apply1(Rps_CallFrame*callerf, const Rps_Value closvarg, const Rps_Value arg0arg)
{
RPS_LOCALFRAME(compute_class(callerf),
callerf,
Rps_ObjectRef curob;
Rps_Value closv;
Rps_Value arg0v;
Rps_Value mainv;
Rps_Value xtrav;
);
if (!closvarg.is_closure())
return;
_f.closv = closvarg;
_f.arg0v = arg0arg;
const unsigned len = cnt();
for (int ix = (int)len-1; ix>=0; ix--)
{
_f.mainv = nullptr;
_f.xtrav = nullptr;
_f.curob = _seqob[ix];
Rps_TwoValues two = _f.closv->as_closure().apply2(&_, _f.curob, _f.arg0v);
_f.mainv = two.main();
_f.xtrav = two.xtra();
if (!_f.mainv && !_f.xtrav)
break;
}
} // end Rps_SeqObjRef::reverse_iterate_apply1
int
Rps_SetOb::element_index(const Rps_ObjectRef obelem) const
{
if (stored_type() != Rps_Type::Set) return -1;
if (!obelem || obelem.is_empty()) return -1;
RPS_ASSERT(obelem->stored_type() == Rps_Type::Object);
unsigned card = cnt();
RPS_ASSERT(card <= maxsize);
auto setdata = (raw_const_data());
int lo = 0, hi = (int)card - 1;
while (lo + 4 < hi)
{
int md = (lo + hi) / 2;
auto curobr = setdata[md];
if (RPS_UNLIKELY(curobr == obelem))
return md;
RPS_ASSERT(!curobr.is_empty()
&& curobr->stored_type() == Rps_Type::Object);
if (curobr < obelem)
lo = md;
else
hi = md;
};
for (int md = lo; md < hi; md++)
{
auto curobr = setdata[md];
if (RPS_UNLIKELY(curobr == obelem))
return md;
}
return -1;
} // end Rps_SetOb::element_index
////////////////////////////////// Rps_SetOb::element_after***
Rps_ObjectRef
Rps_SetOb::element_after_or_equal(const Rps_ObjectRef obelem) const
{
if (stored_type() != Rps_Type::Set)
return nullptr;
if (!obelem)
return nullptr;
int ix = element_index(obelem);
if (ix<0)
return nullptr;
unsigned card = cnt();
if (card == 0)
return nullptr;
RPS_ASSERT(card <= maxsize);
auto setdata = (raw_const_data());
int lo = 0, hi = (int)card - 1;
while (lo + 4 < hi)
{
int md = (lo + hi) / 2;
auto curobr = setdata[md];
RPS_ASSERT(!curobr.is_empty()
&& curobr->stored_type() == Rps_Type::Object);
if (RPS_UNLIKELY(curobr == obelem))
return obelem;
if (curobr < obelem)
lo = md;
else
hi = md;
};
for (int md = lo; md < hi; md++)
{
auto curobr = setdata[md];
RPS_ASSERT(!curobr.is_empty()
&& curobr->stored_type() == Rps_Type::Object);
if (RPS_UNLIKELY(curobr == obelem))
return obelem;
if (curobr > obelem)
return curobr;
};
return nullptr;
} // end of Rps_SetOb::element_after_or_equal
////////////////////////////////// Rps_SetOb::element_before***
Rps_ObjectRef
Rps_SetOb::element_before(const Rps_ObjectRef obelem) const
{
if (stored_type() != Rps_Type::Set)
return nullptr;
if (!obelem)
return nullptr;
int ix = element_index(obelem);
if (ix<0)
return nullptr;
unsigned card = cnt();
if (card == 0)
return nullptr;
RPS_ASSERT(card <= maxsize);
auto setdata = (raw_const_data());
int lo = 0, hi = (int)card - 1;
while (lo + 4 < hi)
{
int md = (lo + hi) / 2;
auto curobr = setdata[md];
RPS_ASSERT(!curobr.is_empty()
&& curobr->stored_type() == Rps_Type::Object);
if (RPS_UNLIKELY(curobr == obelem))
{
if (md>0)
return setdata[md-1];
else
return nullptr;
}
else if (curobr < obelem)
lo = md;
else
hi = md;
};
for (int md = lo; md < hi; md++)
{
auto curobr = setdata[md];
RPS_ASSERT(!curobr.is_empty()
&& curobr->stored_type() == Rps_Type::Object);
if (RPS_UNLIKELY(curobr == obelem))
{
if (md>0)
return setdata[md-1];
}
else if (curobr > obelem)
return nullptr;
};
return nullptr;
} // end of Rps_SetOb::element_before
Rps_ObjectRef
Rps_SetOb::element_before_or_equal(const Rps_ObjectRef obelem) const
{
if (stored_type() != Rps_Type::Set)
return nullptr;
if (!obelem)
return nullptr;
int ix = element_index(obelem);
if (ix<0)
return nullptr;
unsigned card = cnt();
if (card == 0)
return nullptr;
RPS_ASSERT(card <= maxsize);
auto setdata = (raw_const_data());
int lo = 0, hi = (int)card - 1;
while (lo + 4 < hi)
{
int md = (lo + hi) / 2;
auto curobr = setdata[md];
RPS_ASSERT(!curobr.is_empty()
&& curobr->stored_type() == Rps_Type::Object);
if (RPS_UNLIKELY(curobr == obelem))
{
return curobr;
}
else if (curobr < obelem)
lo = md;
else
hi = md;
};
for (int md = lo; md < hi; md++)
{
auto curobr = setdata[md];
RPS_ASSERT(!curobr.is_empty()
&& curobr->stored_type() == Rps_Type::Object);
if (RPS_UNLIKELY(curobr == obelem))
{
return curobr;
}
else if (curobr > obelem)
return nullptr;
else
return curobr;
};
return nullptr;
} // end of Rps_SetOb::element_before_or_equal
Rps_ObjectRef
Rps_SetOb::random_element_or_fail(int startix, int endix) const
{
if (stored_type() != Rps_Type::Set)
throw std::runtime_error("non set value");
return random_object_in_sequence_or_fail (startix, endix);
} // end Rps_SetOb::random_element_or_fail
////////////////////////
Rps_ObjectRef
Rps_SetOb::random_element_or_default(Rps_ObjectRef defob, int startix, int endix) const
{
if (stored_type() != Rps_Type::Set)
return defob;
return random_object_in_sequence_or_default (defob, startix, endix);
} // end Rps_SetOb::random_element_or_default
////////////////////////////////////////////////////////////////
Rps_SetValue::Rps_SetValue (void)
: Rps_Value (&Rps_SetOb::the_empty_set(), Rps_ValPtrTag{})
{
} // end of Rps_SetValue::Rps_SetValue empty
Rps_SetValue::Rps_SetValue (const std::set<Rps_ObjectRef>& obset)
: Rps_Value (Rps_SetOb::make(obset), Rps_ValPtrTag{})
{
} // end of Rps_SetValue::Rps_SetValue of set
Rps_SetValue::Rps_SetValue (const std::vector<Rps_ObjectRef>& obvec)
: Rps_Value (Rps_SetOb::make(obvec), Rps_ValPtrTag{})
{
} // end of Rps_SetValue::Rps_SetValue of vector
Rps_SetValue::Rps_SetValue (const std::initializer_list<Rps_ObjectRef>& obil, Rps_SetTag)
: Rps_Value (Rps_SetOb::make(obil), Rps_ValPtrTag{})
{
} // end of Rps_SetValue::Rps_SetValue of initializer_list
Rps_SetValue::Rps_SetValue(const std::vector<Rps_Value>& vecval)
: Rps_Value (Rps_SetOb::collect(vecval), Rps_ValPtrTag{})
{
} // end of Rps_SetValue::Rps_SetValue of vector of values
Rps_SetValue::Rps_SetValue(const std::initializer_list<Rps_Value>&valil)
: Rps_Value (Rps_SetOb::collect(valil), Rps_ValPtrTag{})
{
} // end of Rps_SetValue::Rps_SetValue of initializer_list of values
Rps_SetValue::Rps_SetValue(const Rps_Value val)
: Rps_Value (val.is_set()?val.as_set():nullptr, Rps_ValPtrTag{})
{
} // end Rps_SetValue::Rps_SetValue dynamic
////////////////
int
Rps_TupleOb::index_found_after(Rps_ObjectRef findob, int startix)
{
if (stored_type() != Rps_Type::Tuple)
return -1;
if (!findob)
return -1;
unsigned siz = cnt();
if (startix < 0)
startix += siz;
if (startix < 0 || startix >= (int)siz)
return -1;
auto tupdata = (raw_const_data());
for (int ix=startix; ix<(int)siz; ix++)
if (tupdata[ix] == findob)
return ix;
return -1;
} // end Rps_TupleOb::index_found_after
int
Rps_TupleOb::index_found_before(Rps_ObjectRef findob, int endix)
{
if (stored_type() != Rps_Type::Tuple)
return -1;
if (!findob)
return -1;
unsigned siz = cnt();
if (endix < 0)
endix += siz;
if (endix < 0 || endix >= (int)siz)
return -1;
auto tupdata = (raw_const_data());
for (int ix=endix; ix >= 0; ix--)
if (tupdata[ix] == findob)
return ix;
return -1;
} // end Rps_TupleOb::index_found_before
Rps_ObjectRef
Rps_TupleOb::random_component_or_fail(int startix, int endix) const
{
if (stored_type() != Rps_Type::Tuple)
throw std::runtime_error("non tuple value");
return random_object_in_sequence_or_fail (startix, endix);
} // end Rps_TupleOb::random_component_or_fail
Rps_ObjectRef
Rps_TupleOb::random_component_or_default(Rps_ObjectRef defob, int startix, int endix) const
{
if (stored_type() != Rps_Type::Tuple)
return defob;
return random_object_in_sequence_or_default (defob, startix, endix);
} // end Rps_TupleOb::random_component_or_default
////////////////////////
////////////////
const Rps_TupleOb*
Rps_Value::as_tuple() const
{
if (is_tuple())
return reinterpret_cast<const Rps_TupleOb*>(_pval);
else throw std::domain_error("Rps_Value::as_tuple: value is not genuine tuple");
} // end Rps_Value::as_tuple
const Rps_TupleOb*
Rps_Value::to_tuple(const Rps_TupleOb*deftup) const
{
if (is_tuple())
return reinterpret_cast<const Rps_TupleOb*>(const_cast<Rps_ZoneValue*>(_pval));
else return deftup;
} // end Rps_Value::to_tuple
const Rps_LexTokenZone*
Rps_Value::as_lextoken() const
{
if (is_lextoken())
return reinterpret_cast<const Rps_LexTokenZone*>(_pval);
else throw std::domain_error("Rps_Value::as_lextoken: value is not genuine lexical token");
} // end Rps_Value::as_lextoken
const Rps_LexTokenZone*
Rps_Value::to_lextoken() const
{
if (is_lextoken())
return reinterpret_cast<const Rps_LexTokenZone*>(const_cast<Rps_ZoneValue*>(_pval));
else return nullptr;
} // end Rps_Value::to_lextoken
Rps_LexTokenValue::Rps_LexTokenValue (const Rps_LexTokenZone*lxz)
: Rps_Value (lxz, Rps_ValPtrTag{})
{
} // end of Rps_LexTokenValue::Rps_LexTokenValue
Rps_LexTokenValue::Rps_LexTokenValue(const Rps_Value val)
: Rps_Value (val.is_lextoken()?val.as_lextoken():nullptr, Rps_ValPtrTag{})
{
} // end Rps_LexTokenValue::Rps_LexTokenValue dynamic
Rps_LexTokenValue::Rps_LexTokenValue (void)
: Rps_Value (nullptr, Rps_ValPtrTag{})
{
} // end of empty Rps_LexTokenValue::Rps_LexTokenValue
Rps_TupleValue::Rps_TupleValue (const std::vector<Rps_ObjectRef>& obvec)
: Rps_Value (Rps_TupleOb::make(obvec), Rps_ValPtrTag{})
{
} // end of Rps_TupleValue::Rps_TupleValue of vector
Rps_TupleValue::Rps_TupleValue (const std::initializer_list<Rps_ObjectRef>& obil, Rps_TupleTag)
: Rps_Value (Rps_TupleOb::make(obil), Rps_ValPtrTag{})
{
} // end of Rps_TupleValue::Rps_TupleValue of initializer_list
Rps_TupleValue::Rps_TupleValue(const std::vector<Rps_Value>& vecval)
: Rps_Value (Rps_TupleOb::collect(vecval), Rps_ValPtrTag{})
{
} // end of Rps_TupleValue::Rps_TupleValue of vector of values
Rps_TupleValue::Rps_TupleValue(const std::initializer_list<Rps_Value>&valil)
: Rps_Value (Rps_TupleOb::collect(valil), Rps_ValPtrTag{})
{
} // end of Rps_TupleValue::Rps_TupleValue of initializer_list of values
Rps_TupleValue::Rps_TupleValue(const Rps_Value val)
: Rps_Value (val.is_tuple()?val.as_tuple():nullptr, Rps_ValPtrTag{})
{
} // end Rps_TupleValue::Rps_TupleValue dynamic
const Rps_String*
Rps_Value::as_string() const
{
if (is_string())
return reinterpret_cast<const Rps_String*>(_pval);
else throw std::domain_error("Rps_Value::as_string: value is not genuine string");
} // end Rps_Value::as_string
const std::string
Rps_Value::as_cppstring() const
{
if (is_string())
return to_string()->cppstring();
else
throw std::domain_error("Rps_Value::as_cppstring: value is not genuine string");
} // end Rps_Value::as_cppstring
const char*
Rps_Value::as_cstring() const
{
if (is_string())
return to_string()->cstr();
else
throw std::domain_error("Rps_Value::as_string: value is not genuine string");
} // end Rps_Value::as_cstring
const Rps_String*
Rps_Value::to_string(const Rps_String*defstr) const
{
if (is_string())
return reinterpret_cast<const Rps_String*>(const_cast<Rps_ZoneValue*>(_pval));
else return defstr;
} // end Rps_Value::to_string
const std::string
Rps_Value::to_cppstring(std::string defstr) const
{
auto strp = to_string();
if (strp) return strp->cppstring();
else return defstr;
} // end of Rps_Value::to_cppstring
Rps_ObjectZone*
Rps_Value::as_object() const
{
if (is_object())
return reinterpret_cast<Rps_ObjectZone*>(const_cast<Rps_ZoneValue*>(_pval));
else
{
RPS_DEBUG_LOG(LOWREP, "Rps_Value::as_object bad this=" << *this
<< RPS_FULL_BACKTRACE_HERE(1, "bad as_object"));
throw std::domain_error("Rps_Value::as_object: value is not genuine object");
}
} // end Rps_Value::as_object
const Rps_InstanceZone*
Rps_Value::as_instance() const
{
if (is_instance())
return reinterpret_cast<const Rps_InstanceZone*>(const_cast<Rps_ZoneValue*>(_pval));
else throw std::domain_error("Rps_Value::as_instance: value is not genuine instance");
} // end Rps_Value::as_instance
const Rps_ObjectZone*
Rps_Value::to_object(const Rps_ObjectZone*defob) const
{
if (is_object())
return reinterpret_cast<Rps_ObjectZone*>(const_cast<Rps_ZoneValue*>(_pval));
else return defob;
} // end Rps_Value::to_object
const Rps_InstanceZone*
Rps_Value::to_instance(const Rps_InstanceZone*definst) const
{
if (is_instance())
return reinterpret_cast<const Rps_InstanceZone*>(const_cast<Rps_ZoneValue*>(_pval));
else return definst;
} // end Rps_Value::to_instance
const Rps_Double*
Rps_Value::as_boxed_double() const
{
if (is_double())
return reinterpret_cast<const Rps_Double*>(_pval);
else throw std::domain_error("Rps_Value::as_boxed_double: value is not genuine double");
} // end Rps_Value::as_boxed_double
double
Rps_Value::as_double() const
{
if (is_double())
return as_boxed_double()->dval();
else throw std::domain_error("Rps_Value::as_double: value is not genuine double");
} // end Rps_Value::as_boxed_double
double
Rps_Value::to_double(double def) const
{
if (is_double())
return as_double();
else return def;
} // end Rps_Value::to_double
const Json::Value
Rps_Value::to_json(const Json::Value defjv) const
{
if (is_json())
return as_json()->json();
else return defjv;
} // end Rps_Value::to_json
bool
Rps_Value::operator == (const Rps_Value v) const
{
if (v._wptr == _wptr) return true;
if (is_empty() || is_null()) return v.is_empty() || v.is_null();
if (is_int()) return false;
return (*as_ptr()) == (*v.as_ptr());
} // end Rps_Value::operator ==
bool
Rps_Value::operator != (const Rps_Value v) const
{