isotree 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (118) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +5 -0
  3. data/LICENSE.txt +2 -2
  4. data/README.md +22 -1
  5. data/ext/isotree/ext.cpp +26 -0
  6. data/ext/isotree/extconf.rb +3 -3
  7. data/lib/isotree.rb +1 -0
  8. data/lib/isotree/isolation_forest.rb +86 -1
  9. data/lib/isotree/version.rb +1 -1
  10. data/vendor/cereal/LICENSE +24 -0
  11. data/vendor/cereal/README.md +85 -0
  12. data/vendor/cereal/include/cereal/access.hpp +351 -0
  13. data/vendor/cereal/include/cereal/archives/adapters.hpp +163 -0
  14. data/vendor/cereal/include/cereal/archives/binary.hpp +169 -0
  15. data/vendor/cereal/include/cereal/archives/json.hpp +1019 -0
  16. data/vendor/cereal/include/cereal/archives/portable_binary.hpp +334 -0
  17. data/vendor/cereal/include/cereal/archives/xml.hpp +956 -0
  18. data/vendor/cereal/include/cereal/cereal.hpp +1089 -0
  19. data/vendor/cereal/include/cereal/details/helpers.hpp +422 -0
  20. data/vendor/cereal/include/cereal/details/polymorphic_impl.hpp +796 -0
  21. data/vendor/cereal/include/cereal/details/polymorphic_impl_fwd.hpp +65 -0
  22. data/vendor/cereal/include/cereal/details/static_object.hpp +127 -0
  23. data/vendor/cereal/include/cereal/details/traits.hpp +1411 -0
  24. data/vendor/cereal/include/cereal/details/util.hpp +84 -0
  25. data/vendor/cereal/include/cereal/external/base64.hpp +134 -0
  26. data/vendor/cereal/include/cereal/external/rapidjson/allocators.h +284 -0
  27. data/vendor/cereal/include/cereal/external/rapidjson/cursorstreamwrapper.h +78 -0
  28. data/vendor/cereal/include/cereal/external/rapidjson/document.h +2652 -0
  29. data/vendor/cereal/include/cereal/external/rapidjson/encodedstream.h +299 -0
  30. data/vendor/cereal/include/cereal/external/rapidjson/encodings.h +716 -0
  31. data/vendor/cereal/include/cereal/external/rapidjson/error/en.h +74 -0
  32. data/vendor/cereal/include/cereal/external/rapidjson/error/error.h +161 -0
  33. data/vendor/cereal/include/cereal/external/rapidjson/filereadstream.h +99 -0
  34. data/vendor/cereal/include/cereal/external/rapidjson/filewritestream.h +104 -0
  35. data/vendor/cereal/include/cereal/external/rapidjson/fwd.h +151 -0
  36. data/vendor/cereal/include/cereal/external/rapidjson/internal/biginteger.h +290 -0
  37. data/vendor/cereal/include/cereal/external/rapidjson/internal/diyfp.h +271 -0
  38. data/vendor/cereal/include/cereal/external/rapidjson/internal/dtoa.h +245 -0
  39. data/vendor/cereal/include/cereal/external/rapidjson/internal/ieee754.h +78 -0
  40. data/vendor/cereal/include/cereal/external/rapidjson/internal/itoa.h +308 -0
  41. data/vendor/cereal/include/cereal/external/rapidjson/internal/meta.h +186 -0
  42. data/vendor/cereal/include/cereal/external/rapidjson/internal/pow10.h +55 -0
  43. data/vendor/cereal/include/cereal/external/rapidjson/internal/regex.h +740 -0
  44. data/vendor/cereal/include/cereal/external/rapidjson/internal/stack.h +232 -0
  45. data/vendor/cereal/include/cereal/external/rapidjson/internal/strfunc.h +69 -0
  46. data/vendor/cereal/include/cereal/external/rapidjson/internal/strtod.h +290 -0
  47. data/vendor/cereal/include/cereal/external/rapidjson/internal/swap.h +46 -0
  48. data/vendor/cereal/include/cereal/external/rapidjson/istreamwrapper.h +128 -0
  49. data/vendor/cereal/include/cereal/external/rapidjson/memorybuffer.h +70 -0
  50. data/vendor/cereal/include/cereal/external/rapidjson/memorystream.h +71 -0
  51. data/vendor/cereal/include/cereal/external/rapidjson/msinttypes/inttypes.h +316 -0
  52. data/vendor/cereal/include/cereal/external/rapidjson/msinttypes/stdint.h +300 -0
  53. data/vendor/cereal/include/cereal/external/rapidjson/ostreamwrapper.h +81 -0
  54. data/vendor/cereal/include/cereal/external/rapidjson/pointer.h +1414 -0
  55. data/vendor/cereal/include/cereal/external/rapidjson/prettywriter.h +277 -0
  56. data/vendor/cereal/include/cereal/external/rapidjson/rapidjson.h +656 -0
  57. data/vendor/cereal/include/cereal/external/rapidjson/reader.h +2230 -0
  58. data/vendor/cereal/include/cereal/external/rapidjson/schema.h +2497 -0
  59. data/vendor/cereal/include/cereal/external/rapidjson/stream.h +223 -0
  60. data/vendor/cereal/include/cereal/external/rapidjson/stringbuffer.h +121 -0
  61. data/vendor/cereal/include/cereal/external/rapidjson/writer.h +709 -0
  62. data/vendor/cereal/include/cereal/external/rapidxml/license.txt +52 -0
  63. data/vendor/cereal/include/cereal/external/rapidxml/manual.html +406 -0
  64. data/vendor/cereal/include/cereal/external/rapidxml/rapidxml.hpp +2624 -0
  65. data/vendor/cereal/include/cereal/external/rapidxml/rapidxml_iterators.hpp +175 -0
  66. data/vendor/cereal/include/cereal/external/rapidxml/rapidxml_print.hpp +428 -0
  67. data/vendor/cereal/include/cereal/external/rapidxml/rapidxml_utils.hpp +123 -0
  68. data/vendor/cereal/include/cereal/macros.hpp +154 -0
  69. data/vendor/cereal/include/cereal/specialize.hpp +139 -0
  70. data/vendor/cereal/include/cereal/types/array.hpp +79 -0
  71. data/vendor/cereal/include/cereal/types/atomic.hpp +55 -0
  72. data/vendor/cereal/include/cereal/types/base_class.hpp +203 -0
  73. data/vendor/cereal/include/cereal/types/bitset.hpp +176 -0
  74. data/vendor/cereal/include/cereal/types/boost_variant.hpp +164 -0
  75. data/vendor/cereal/include/cereal/types/chrono.hpp +72 -0
  76. data/vendor/cereal/include/cereal/types/common.hpp +129 -0
  77. data/vendor/cereal/include/cereal/types/complex.hpp +56 -0
  78. data/vendor/cereal/include/cereal/types/concepts/pair_associative_container.hpp +73 -0
  79. data/vendor/cereal/include/cereal/types/deque.hpp +62 -0
  80. data/vendor/cereal/include/cereal/types/forward_list.hpp +68 -0
  81. data/vendor/cereal/include/cereal/types/functional.hpp +43 -0
  82. data/vendor/cereal/include/cereal/types/list.hpp +62 -0
  83. data/vendor/cereal/include/cereal/types/map.hpp +36 -0
  84. data/vendor/cereal/include/cereal/types/memory.hpp +425 -0
  85. data/vendor/cereal/include/cereal/types/optional.hpp +66 -0
  86. data/vendor/cereal/include/cereal/types/polymorphic.hpp +483 -0
  87. data/vendor/cereal/include/cereal/types/queue.hpp +132 -0
  88. data/vendor/cereal/include/cereal/types/set.hpp +103 -0
  89. data/vendor/cereal/include/cereal/types/stack.hpp +76 -0
  90. data/vendor/cereal/include/cereal/types/string.hpp +61 -0
  91. data/vendor/cereal/include/cereal/types/tuple.hpp +123 -0
  92. data/vendor/cereal/include/cereal/types/unordered_map.hpp +36 -0
  93. data/vendor/cereal/include/cereal/types/unordered_set.hpp +99 -0
  94. data/vendor/cereal/include/cereal/types/utility.hpp +47 -0
  95. data/vendor/cereal/include/cereal/types/valarray.hpp +89 -0
  96. data/vendor/cereal/include/cereal/types/variant.hpp +109 -0
  97. data/vendor/cereal/include/cereal/types/vector.hpp +112 -0
  98. data/vendor/cereal/include/cereal/version.hpp +52 -0
  99. data/vendor/isotree/LICENSE +1 -1
  100. data/vendor/isotree/README.md +2 -1
  101. data/vendor/isotree/src/RcppExports.cpp +44 -4
  102. data/vendor/isotree/src/Rwrapper.cpp +141 -51
  103. data/vendor/isotree/src/crit.cpp +1 -1
  104. data/vendor/isotree/src/dealloc.cpp +1 -1
  105. data/vendor/isotree/src/dist.cpp +6 -6
  106. data/vendor/isotree/src/extended.cpp +5 -5
  107. data/vendor/isotree/src/fit_model.cpp +30 -19
  108. data/vendor/isotree/src/helpers_iforest.cpp +26 -11
  109. data/vendor/isotree/src/impute.cpp +7 -7
  110. data/vendor/isotree/src/isoforest.cpp +7 -7
  111. data/vendor/isotree/src/isotree.hpp +27 -5
  112. data/vendor/isotree/src/merge_models.cpp +1 -1
  113. data/vendor/isotree/src/mult.cpp +1 -1
  114. data/vendor/isotree/src/predict.cpp +20 -16
  115. data/vendor/isotree/src/serialize.cpp +1 -1
  116. data/vendor/isotree/src/sql.cpp +545 -0
  117. data/vendor/isotree/src/utils.cpp +36 -44
  118. metadata +98 -92
@@ -0,0 +1,232 @@
1
+ // Tencent is pleased to support the open source community by making RapidJSON available.
2
+ //
3
+ // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
4
+ //
5
+ // Licensed under the MIT License (the "License"); you may not use this file except
6
+ // in compliance with the License. You may obtain a copy of the License at
7
+ //
8
+ // http://opensource.org/licenses/MIT
9
+ //
10
+ // Unless required by applicable law or agreed to in writing, software distributed
11
+ // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12
+ // CONDITIONS OF ANY KIND, either express or implied. See the License for the
13
+ // specific language governing permissions and limitations under the License.
14
+
15
+ #ifndef CEREAL_RAPIDJSON_INTERNAL_STACK_H_
16
+ #define CEREAL_RAPIDJSON_INTERNAL_STACK_H_
17
+
18
+ #include "../allocators.h"
19
+ #include "swap.h"
20
+ #include <cstddef>
21
+
22
+ #if defined(__clang__)
23
+ CEREAL_RAPIDJSON_DIAG_PUSH
24
+ CEREAL_RAPIDJSON_DIAG_OFF(c++98-compat)
25
+ #endif
26
+
27
+ CEREAL_RAPIDJSON_NAMESPACE_BEGIN
28
+ namespace internal {
29
+
30
+ ///////////////////////////////////////////////////////////////////////////////
31
+ // Stack
32
+
33
+ //! A type-unsafe stack for storing different types of data.
34
+ /*! \tparam Allocator Allocator for allocating stack memory.
35
+ */
36
+ template <typename Allocator>
37
+ class Stack {
38
+ public:
39
+ // Optimization note: Do not allocate memory for stack_ in constructor.
40
+ // Do it lazily when first Push() -> Expand() -> Resize().
41
+ Stack(Allocator* allocator, size_t stackCapacity) : allocator_(allocator), ownAllocator_(0), stack_(0), stackTop_(0), stackEnd_(0), initialCapacity_(stackCapacity) {
42
+ }
43
+
44
+ #if CEREAL_RAPIDJSON_HAS_CXX11_RVALUE_REFS
45
+ Stack(Stack&& rhs)
46
+ : allocator_(rhs.allocator_),
47
+ ownAllocator_(rhs.ownAllocator_),
48
+ stack_(rhs.stack_),
49
+ stackTop_(rhs.stackTop_),
50
+ stackEnd_(rhs.stackEnd_),
51
+ initialCapacity_(rhs.initialCapacity_)
52
+ {
53
+ rhs.allocator_ = 0;
54
+ rhs.ownAllocator_ = 0;
55
+ rhs.stack_ = 0;
56
+ rhs.stackTop_ = 0;
57
+ rhs.stackEnd_ = 0;
58
+ rhs.initialCapacity_ = 0;
59
+ }
60
+ #endif
61
+
62
+ ~Stack() {
63
+ Destroy();
64
+ }
65
+
66
+ #if CEREAL_RAPIDJSON_HAS_CXX11_RVALUE_REFS
67
+ Stack& operator=(Stack&& rhs) {
68
+ if (&rhs != this)
69
+ {
70
+ Destroy();
71
+
72
+ allocator_ = rhs.allocator_;
73
+ ownAllocator_ = rhs.ownAllocator_;
74
+ stack_ = rhs.stack_;
75
+ stackTop_ = rhs.stackTop_;
76
+ stackEnd_ = rhs.stackEnd_;
77
+ initialCapacity_ = rhs.initialCapacity_;
78
+
79
+ rhs.allocator_ = 0;
80
+ rhs.ownAllocator_ = 0;
81
+ rhs.stack_ = 0;
82
+ rhs.stackTop_ = 0;
83
+ rhs.stackEnd_ = 0;
84
+ rhs.initialCapacity_ = 0;
85
+ }
86
+ return *this;
87
+ }
88
+ #endif
89
+
90
+ void Swap(Stack& rhs) CEREAL_RAPIDJSON_NOEXCEPT {
91
+ internal::Swap(allocator_, rhs.allocator_);
92
+ internal::Swap(ownAllocator_, rhs.ownAllocator_);
93
+ internal::Swap(stack_, rhs.stack_);
94
+ internal::Swap(stackTop_, rhs.stackTop_);
95
+ internal::Swap(stackEnd_, rhs.stackEnd_);
96
+ internal::Swap(initialCapacity_, rhs.initialCapacity_);
97
+ }
98
+
99
+ void Clear() { stackTop_ = stack_; }
100
+
101
+ void ShrinkToFit() {
102
+ if (Empty()) {
103
+ // If the stack is empty, completely deallocate the memory.
104
+ Allocator::Free(stack_); // NOLINT (+clang-analyzer-unix.Malloc)
105
+ stack_ = 0;
106
+ stackTop_ = 0;
107
+ stackEnd_ = 0;
108
+ }
109
+ else
110
+ Resize(GetSize());
111
+ }
112
+
113
+ // Optimization note: try to minimize the size of this function for force inline.
114
+ // Expansion is run very infrequently, so it is moved to another (probably non-inline) function.
115
+ template<typename T>
116
+ CEREAL_RAPIDJSON_FORCEINLINE void Reserve(size_t count = 1) {
117
+ // Expand the stack if needed
118
+ if (CEREAL_RAPIDJSON_UNLIKELY(static_cast<std::ptrdiff_t>(sizeof(T) * count) > (stackEnd_ - stackTop_)))
119
+ Expand<T>(count);
120
+ }
121
+
122
+ template<typename T>
123
+ CEREAL_RAPIDJSON_FORCEINLINE T* Push(size_t count = 1) {
124
+ Reserve<T>(count);
125
+ return PushUnsafe<T>(count);
126
+ }
127
+
128
+ template<typename T>
129
+ CEREAL_RAPIDJSON_FORCEINLINE T* PushUnsafe(size_t count = 1) {
130
+ CEREAL_RAPIDJSON_ASSERT(stackTop_);
131
+ CEREAL_RAPIDJSON_ASSERT(static_cast<std::ptrdiff_t>(sizeof(T) * count) <= (stackEnd_ - stackTop_));
132
+ T* ret = reinterpret_cast<T*>(stackTop_);
133
+ stackTop_ += sizeof(T) * count;
134
+ return ret;
135
+ }
136
+
137
+ template<typename T>
138
+ T* Pop(size_t count) {
139
+ CEREAL_RAPIDJSON_ASSERT(GetSize() >= count * sizeof(T));
140
+ stackTop_ -= count * sizeof(T);
141
+ return reinterpret_cast<T*>(stackTop_);
142
+ }
143
+
144
+ template<typename T>
145
+ T* Top() {
146
+ CEREAL_RAPIDJSON_ASSERT(GetSize() >= sizeof(T));
147
+ return reinterpret_cast<T*>(stackTop_ - sizeof(T));
148
+ }
149
+
150
+ template<typename T>
151
+ const T* Top() const {
152
+ CEREAL_RAPIDJSON_ASSERT(GetSize() >= sizeof(T));
153
+ return reinterpret_cast<T*>(stackTop_ - sizeof(T));
154
+ }
155
+
156
+ template<typename T>
157
+ T* End() { return reinterpret_cast<T*>(stackTop_); }
158
+
159
+ template<typename T>
160
+ const T* End() const { return reinterpret_cast<T*>(stackTop_); }
161
+
162
+ template<typename T>
163
+ T* Bottom() { return reinterpret_cast<T*>(stack_); }
164
+
165
+ template<typename T>
166
+ const T* Bottom() const { return reinterpret_cast<T*>(stack_); }
167
+
168
+ bool HasAllocator() const {
169
+ return allocator_ != 0;
170
+ }
171
+
172
+ Allocator& GetAllocator() {
173
+ CEREAL_RAPIDJSON_ASSERT(allocator_);
174
+ return *allocator_;
175
+ }
176
+
177
+ bool Empty() const { return stackTop_ == stack_; }
178
+ size_t GetSize() const { return static_cast<size_t>(stackTop_ - stack_); }
179
+ size_t GetCapacity() const { return static_cast<size_t>(stackEnd_ - stack_); }
180
+
181
+ private:
182
+ template<typename T>
183
+ void Expand(size_t count) {
184
+ // Only expand the capacity if the current stack exists. Otherwise just create a stack with initial capacity.
185
+ size_t newCapacity;
186
+ if (stack_ == 0) {
187
+ if (!allocator_)
188
+ ownAllocator_ = allocator_ = CEREAL_RAPIDJSON_NEW(Allocator)();
189
+ newCapacity = initialCapacity_;
190
+ } else {
191
+ newCapacity = GetCapacity();
192
+ newCapacity += (newCapacity + 1) / 2;
193
+ }
194
+ size_t newSize = GetSize() + sizeof(T) * count;
195
+ if (newCapacity < newSize)
196
+ newCapacity = newSize;
197
+
198
+ Resize(newCapacity);
199
+ }
200
+
201
+ void Resize(size_t newCapacity) {
202
+ const size_t size = GetSize(); // Backup the current size
203
+ stack_ = static_cast<char*>(allocator_->Realloc(stack_, GetCapacity(), newCapacity));
204
+ stackTop_ = stack_ + size;
205
+ stackEnd_ = stack_ + newCapacity;
206
+ }
207
+
208
+ void Destroy() {
209
+ Allocator::Free(stack_);
210
+ CEREAL_RAPIDJSON_DELETE(ownAllocator_); // Only delete if it is owned by the stack
211
+ }
212
+
213
+ // Prohibit copy constructor & assignment operator.
214
+ Stack(const Stack&);
215
+ Stack& operator=(const Stack&);
216
+
217
+ Allocator* allocator_;
218
+ Allocator* ownAllocator_;
219
+ char *stack_;
220
+ char *stackTop_;
221
+ char *stackEnd_;
222
+ size_t initialCapacity_;
223
+ };
224
+
225
+ } // namespace internal
226
+ CEREAL_RAPIDJSON_NAMESPACE_END
227
+
228
+ #if defined(__clang__)
229
+ CEREAL_RAPIDJSON_DIAG_POP
230
+ #endif
231
+
232
+ #endif // CEREAL_RAPIDJSON_STACK_H_
@@ -0,0 +1,69 @@
1
+ // Tencent is pleased to support the open source community by making RapidJSON available.
2
+ //
3
+ // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
4
+ //
5
+ // Licensed under the MIT License (the "License"); you may not use this file except
6
+ // in compliance with the License. You may obtain a copy of the License at
7
+ //
8
+ // http://opensource.org/licenses/MIT
9
+ //
10
+ // Unless required by applicable law or agreed to in writing, software distributed
11
+ // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12
+ // CONDITIONS OF ANY KIND, either express or implied. See the License for the
13
+ // specific language governing permissions and limitations under the License.
14
+
15
+ #ifndef CEREAL_RAPIDJSON_INTERNAL_STRFUNC_H_
16
+ #define CEREAL_RAPIDJSON_INTERNAL_STRFUNC_H_
17
+
18
+ #include "../stream.h"
19
+ #include <cwchar>
20
+
21
+ CEREAL_RAPIDJSON_NAMESPACE_BEGIN
22
+ namespace internal {
23
+
24
+ //! Custom strlen() which works on different character types.
25
+ /*! \tparam Ch Character type (e.g. char, wchar_t, short)
26
+ \param s Null-terminated input string.
27
+ \return Number of characters in the string.
28
+ \note This has the same semantics as strlen(), the return value is not number of Unicode codepoints.
29
+ */
30
+ template <typename Ch>
31
+ inline SizeType StrLen(const Ch* s) {
32
+ CEREAL_RAPIDJSON_ASSERT(s != 0);
33
+ const Ch* p = s;
34
+ while (*p) ++p;
35
+ return SizeType(p - s);
36
+ }
37
+
38
+ template <>
39
+ inline SizeType StrLen(const char* s) {
40
+ return SizeType(std::strlen(s));
41
+ }
42
+
43
+ template <>
44
+ inline SizeType StrLen(const wchar_t* s) {
45
+ return SizeType(std::wcslen(s));
46
+ }
47
+
48
+ //! Returns number of code points in a encoded string.
49
+ template<typename Encoding>
50
+ bool CountStringCodePoint(const typename Encoding::Ch* s, SizeType length, SizeType* outCount) {
51
+ CEREAL_RAPIDJSON_ASSERT(s != 0);
52
+ CEREAL_RAPIDJSON_ASSERT(outCount != 0);
53
+ GenericStringStream<Encoding> is(s);
54
+ const typename Encoding::Ch* end = s + length;
55
+ SizeType count = 0;
56
+ while (is.src_ < end) {
57
+ unsigned codepoint;
58
+ if (!Encoding::Decode(is, &codepoint))
59
+ return false;
60
+ count++;
61
+ }
62
+ *outCount = count;
63
+ return true;
64
+ }
65
+
66
+ } // namespace internal
67
+ CEREAL_RAPIDJSON_NAMESPACE_END
68
+
69
+ #endif // CEREAL_RAPIDJSON_INTERNAL_STRFUNC_H_
@@ -0,0 +1,290 @@
1
+ // Tencent is pleased to support the open source community by making RapidJSON available.
2
+ //
3
+ // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
4
+ //
5
+ // Licensed under the MIT License (the "License"); you may not use this file except
6
+ // in compliance with the License. You may obtain a copy of the License at
7
+ //
8
+ // http://opensource.org/licenses/MIT
9
+ //
10
+ // Unless required by applicable law or agreed to in writing, software distributed
11
+ // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12
+ // CONDITIONS OF ANY KIND, either express or implied. See the License for the
13
+ // specific language governing permissions and limitations under the License.
14
+
15
+ #ifndef CEREAL_RAPIDJSON_STRTOD_
16
+ #define CEREAL_RAPIDJSON_STRTOD_
17
+
18
+ #include "ieee754.h"
19
+ #include "biginteger.h"
20
+ #include "diyfp.h"
21
+ #include "pow10.h"
22
+ #include <climits>
23
+ #include <limits>
24
+
25
+ CEREAL_RAPIDJSON_NAMESPACE_BEGIN
26
+ namespace internal {
27
+
28
+ inline double FastPath(double significand, int exp) {
29
+ if (exp < -308)
30
+ return 0.0;
31
+ else if (exp >= 0)
32
+ return significand * internal::Pow10(exp);
33
+ else
34
+ return significand / internal::Pow10(-exp);
35
+ }
36
+
37
+ inline double StrtodNormalPrecision(double d, int p) {
38
+ if (p < -308) {
39
+ // Prevent expSum < -308, making Pow10(p) = 0
40
+ d = FastPath(d, -308);
41
+ d = FastPath(d, p + 308);
42
+ }
43
+ else
44
+ d = FastPath(d, p);
45
+ return d;
46
+ }
47
+
48
+ template <typename T>
49
+ inline T Min3(T a, T b, T c) {
50
+ T m = a;
51
+ if (m > b) m = b;
52
+ if (m > c) m = c;
53
+ return m;
54
+ }
55
+
56
+ inline int CheckWithinHalfULP(double b, const BigInteger& d, int dExp) {
57
+ const Double db(b);
58
+ const uint64_t bInt = db.IntegerSignificand();
59
+ const int bExp = db.IntegerExponent();
60
+ const int hExp = bExp - 1;
61
+
62
+ int dS_Exp2 = 0, dS_Exp5 = 0, bS_Exp2 = 0, bS_Exp5 = 0, hS_Exp2 = 0, hS_Exp5 = 0;
63
+
64
+ // Adjust for decimal exponent
65
+ if (dExp >= 0) {
66
+ dS_Exp2 += dExp;
67
+ dS_Exp5 += dExp;
68
+ }
69
+ else {
70
+ bS_Exp2 -= dExp;
71
+ bS_Exp5 -= dExp;
72
+ hS_Exp2 -= dExp;
73
+ hS_Exp5 -= dExp;
74
+ }
75
+
76
+ // Adjust for binary exponent
77
+ if (bExp >= 0)
78
+ bS_Exp2 += bExp;
79
+ else {
80
+ dS_Exp2 -= bExp;
81
+ hS_Exp2 -= bExp;
82
+ }
83
+
84
+ // Adjust for half ulp exponent
85
+ if (hExp >= 0)
86
+ hS_Exp2 += hExp;
87
+ else {
88
+ dS_Exp2 -= hExp;
89
+ bS_Exp2 -= hExp;
90
+ }
91
+
92
+ // Remove common power of two factor from all three scaled values
93
+ int common_Exp2 = Min3(dS_Exp2, bS_Exp2, hS_Exp2);
94
+ dS_Exp2 -= common_Exp2;
95
+ bS_Exp2 -= common_Exp2;
96
+ hS_Exp2 -= common_Exp2;
97
+
98
+ BigInteger dS = d;
99
+ dS.MultiplyPow5(static_cast<unsigned>(dS_Exp5)) <<= static_cast<unsigned>(dS_Exp2);
100
+
101
+ BigInteger bS(bInt);
102
+ bS.MultiplyPow5(static_cast<unsigned>(bS_Exp5)) <<= static_cast<unsigned>(bS_Exp2);
103
+
104
+ BigInteger hS(1);
105
+ hS.MultiplyPow5(static_cast<unsigned>(hS_Exp5)) <<= static_cast<unsigned>(hS_Exp2);
106
+
107
+ BigInteger delta(0);
108
+ dS.Difference(bS, &delta);
109
+
110
+ return delta.Compare(hS);
111
+ }
112
+
113
+ inline bool StrtodFast(double d, int p, double* result) {
114
+ // Use fast path for string-to-double conversion if possible
115
+ // see http://www.exploringbinary.com/fast-path-decimal-to-floating-point-conversion/
116
+ if (p > 22 && p < 22 + 16) {
117
+ // Fast Path Cases In Disguise
118
+ d *= internal::Pow10(p - 22);
119
+ p = 22;
120
+ }
121
+
122
+ if (p >= -22 && p <= 22 && d <= 9007199254740991.0) { // 2^53 - 1
123
+ *result = FastPath(d, p);
124
+ return true;
125
+ }
126
+ else
127
+ return false;
128
+ }
129
+
130
+ // Compute an approximation and see if it is within 1/2 ULP
131
+ inline bool StrtodDiyFp(const char* decimals, int dLen, int dExp, double* result) {
132
+ uint64_t significand = 0;
133
+ int i = 0; // 2^64 - 1 = 18446744073709551615, 1844674407370955161 = 0x1999999999999999
134
+ for (; i < dLen; i++) {
135
+ if (significand > CEREAL_RAPIDJSON_UINT64_C2(0x19999999, 0x99999999) ||
136
+ (significand == CEREAL_RAPIDJSON_UINT64_C2(0x19999999, 0x99999999) && decimals[i] > '5'))
137
+ break;
138
+ significand = significand * 10u + static_cast<unsigned>(decimals[i] - '0');
139
+ }
140
+
141
+ if (i < dLen && decimals[i] >= '5') // Rounding
142
+ significand++;
143
+
144
+ int remaining = dLen - i;
145
+ const int kUlpShift = 3;
146
+ const int kUlp = 1 << kUlpShift;
147
+ int64_t error = (remaining == 0) ? 0 : kUlp / 2;
148
+
149
+ DiyFp v(significand, 0);
150
+ v = v.Normalize();
151
+ error <<= -v.e;
152
+
153
+ dExp += remaining;
154
+
155
+ int actualExp;
156
+ DiyFp cachedPower = GetCachedPower10(dExp, &actualExp);
157
+ if (actualExp != dExp) {
158
+ static const DiyFp kPow10[] = {
159
+ DiyFp(CEREAL_RAPIDJSON_UINT64_C2(0xa0000000, 0x00000000), -60), // 10^1
160
+ DiyFp(CEREAL_RAPIDJSON_UINT64_C2(0xc8000000, 0x00000000), -57), // 10^2
161
+ DiyFp(CEREAL_RAPIDJSON_UINT64_C2(0xfa000000, 0x00000000), -54), // 10^3
162
+ DiyFp(CEREAL_RAPIDJSON_UINT64_C2(0x9c400000, 0x00000000), -50), // 10^4
163
+ DiyFp(CEREAL_RAPIDJSON_UINT64_C2(0xc3500000, 0x00000000), -47), // 10^5
164
+ DiyFp(CEREAL_RAPIDJSON_UINT64_C2(0xf4240000, 0x00000000), -44), // 10^6
165
+ DiyFp(CEREAL_RAPIDJSON_UINT64_C2(0x98968000, 0x00000000), -40) // 10^7
166
+ };
167
+ int adjustment = dExp - actualExp;
168
+ CEREAL_RAPIDJSON_ASSERT(adjustment >= 1 && adjustment < 8);
169
+ v = v * kPow10[adjustment - 1];
170
+ if (dLen + adjustment > 19) // has more digits than decimal digits in 64-bit
171
+ error += kUlp / 2;
172
+ }
173
+
174
+ v = v * cachedPower;
175
+
176
+ error += kUlp + (error == 0 ? 0 : 1);
177
+
178
+ const int oldExp = v.e;
179
+ v = v.Normalize();
180
+ error <<= oldExp - v.e;
181
+
182
+ const int effectiveSignificandSize = Double::EffectiveSignificandSize(64 + v.e);
183
+ int precisionSize = 64 - effectiveSignificandSize;
184
+ if (precisionSize + kUlpShift >= 64) {
185
+ int scaleExp = (precisionSize + kUlpShift) - 63;
186
+ v.f >>= scaleExp;
187
+ v.e += scaleExp;
188
+ error = (error >> scaleExp) + 1 + kUlp;
189
+ precisionSize -= scaleExp;
190
+ }
191
+
192
+ DiyFp rounded(v.f >> precisionSize, v.e + precisionSize);
193
+ const uint64_t precisionBits = (v.f & ((uint64_t(1) << precisionSize) - 1)) * kUlp;
194
+ const uint64_t halfWay = (uint64_t(1) << (precisionSize - 1)) * kUlp;
195
+ if (precisionBits >= halfWay + static_cast<unsigned>(error)) {
196
+ rounded.f++;
197
+ if (rounded.f & (DiyFp::kDpHiddenBit << 1)) { // rounding overflows mantissa (issue #340)
198
+ rounded.f >>= 1;
199
+ rounded.e++;
200
+ }
201
+ }
202
+
203
+ *result = rounded.ToDouble();
204
+
205
+ return halfWay - static_cast<unsigned>(error) >= precisionBits || precisionBits >= halfWay + static_cast<unsigned>(error);
206
+ }
207
+
208
+ inline double StrtodBigInteger(double approx, const char* decimals, int dLen, int dExp) {
209
+ CEREAL_RAPIDJSON_ASSERT(dLen >= 0);
210
+ const BigInteger dInt(decimals, static_cast<unsigned>(dLen));
211
+ Double a(approx);
212
+ int cmp = CheckWithinHalfULP(a.Value(), dInt, dExp);
213
+ if (cmp < 0)
214
+ return a.Value(); // within half ULP
215
+ else if (cmp == 0) {
216
+ // Round towards even
217
+ if (a.Significand() & 1)
218
+ return a.NextPositiveDouble();
219
+ else
220
+ return a.Value();
221
+ }
222
+ else // adjustment
223
+ return a.NextPositiveDouble();
224
+ }
225
+
226
+ inline double StrtodFullPrecision(double d, int p, const char* decimals, size_t length, size_t decimalPosition, int exp) {
227
+ CEREAL_RAPIDJSON_ASSERT(d >= 0.0);
228
+ CEREAL_RAPIDJSON_ASSERT(length >= 1);
229
+
230
+ double result = 0.0;
231
+ if (StrtodFast(d, p, &result))
232
+ return result;
233
+
234
+ CEREAL_RAPIDJSON_ASSERT(length <= INT_MAX);
235
+ int dLen = static_cast<int>(length);
236
+
237
+ CEREAL_RAPIDJSON_ASSERT(length >= decimalPosition);
238
+ CEREAL_RAPIDJSON_ASSERT(length - decimalPosition <= INT_MAX);
239
+ int dExpAdjust = static_cast<int>(length - decimalPosition);
240
+
241
+ CEREAL_RAPIDJSON_ASSERT(exp >= INT_MIN + dExpAdjust);
242
+ int dExp = exp - dExpAdjust;
243
+
244
+ // Make sure length+dExp does not overflow
245
+ CEREAL_RAPIDJSON_ASSERT(dExp <= INT_MAX - dLen);
246
+
247
+ // Trim leading zeros
248
+ while (dLen > 0 && *decimals == '0') {
249
+ dLen--;
250
+ decimals++;
251
+ }
252
+
253
+ // Trim trailing zeros
254
+ while (dLen > 0 && decimals[dLen - 1] == '0') {
255
+ dLen--;
256
+ dExp++;
257
+ }
258
+
259
+ if (dLen == 0) { // Buffer only contains zeros.
260
+ return 0.0;
261
+ }
262
+
263
+ // Trim right-most digits
264
+ const int kMaxDecimalDigit = 767 + 1;
265
+ if (dLen > kMaxDecimalDigit) {
266
+ dExp += dLen - kMaxDecimalDigit;
267
+ dLen = kMaxDecimalDigit;
268
+ }
269
+
270
+ // If too small, underflow to zero.
271
+ // Any x <= 10^-324 is interpreted as zero.
272
+ if (dLen + dExp <= -324)
273
+ return 0.0;
274
+
275
+ // If too large, overflow to infinity.
276
+ // Any x >= 10^309 is interpreted as +infinity.
277
+ if (dLen + dExp > 309)
278
+ return std::numeric_limits<double>::infinity();
279
+
280
+ if (StrtodDiyFp(decimals, dLen, dExp, &result))
281
+ return result;
282
+
283
+ // Use approximation from StrtodDiyFp and make adjustment with BigInteger comparison
284
+ return StrtodBigInteger(result, decimals, dLen, dExp);
285
+ }
286
+
287
+ } // namespace internal
288
+ CEREAL_RAPIDJSON_NAMESPACE_END
289
+
290
+ #endif // CEREAL_RAPIDJSON_STRTOD_