isotree 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (151) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +8 -1
  3. data/LICENSE.txt +2 -2
  4. data/README.md +32 -14
  5. data/ext/isotree/ext.cpp +144 -31
  6. data/ext/isotree/extconf.rb +7 -7
  7. data/lib/isotree/isolation_forest.rb +110 -30
  8. data/lib/isotree/version.rb +1 -1
  9. data/vendor/isotree/LICENSE +1 -1
  10. data/vendor/isotree/README.md +165 -27
  11. data/vendor/isotree/include/isotree.hpp +2111 -0
  12. data/vendor/isotree/include/isotree_oop.hpp +394 -0
  13. data/vendor/isotree/inst/COPYRIGHTS +62 -0
  14. data/vendor/isotree/src/RcppExports.cpp +525 -52
  15. data/vendor/isotree/src/Rwrapper.cpp +1931 -268
  16. data/vendor/isotree/src/c_interface.cpp +953 -0
  17. data/vendor/isotree/src/crit.hpp +4232 -0
  18. data/vendor/isotree/src/dist.hpp +1886 -0
  19. data/vendor/isotree/src/exp_depth_table.hpp +134 -0
  20. data/vendor/isotree/src/extended.hpp +1444 -0
  21. data/vendor/isotree/src/external_facing_generic.hpp +399 -0
  22. data/vendor/isotree/src/fit_model.hpp +2401 -0
  23. data/vendor/isotree/src/{dealloc.cpp → headers_joined.hpp} +38 -22
  24. data/vendor/isotree/src/helpers_iforest.hpp +813 -0
  25. data/vendor/isotree/src/{impute.cpp → impute.hpp} +353 -122
  26. data/vendor/isotree/src/indexer.cpp +515 -0
  27. data/vendor/isotree/src/instantiate_template_headers.cpp +118 -0
  28. data/vendor/isotree/src/instantiate_template_headers.hpp +240 -0
  29. data/vendor/isotree/src/isoforest.hpp +1659 -0
  30. data/vendor/isotree/src/isotree.hpp +1804 -392
  31. data/vendor/isotree/src/isotree_exportable.hpp +99 -0
  32. data/vendor/isotree/src/merge_models.cpp +159 -16
  33. data/vendor/isotree/src/mult.hpp +1321 -0
  34. data/vendor/isotree/src/oop_interface.cpp +842 -0
  35. data/vendor/isotree/src/oop_interface.hpp +278 -0
  36. data/vendor/isotree/src/other_helpers.hpp +219 -0
  37. data/vendor/isotree/src/predict.hpp +1932 -0
  38. data/vendor/isotree/src/python_helpers.hpp +134 -0
  39. data/vendor/isotree/src/ref_indexer.hpp +154 -0
  40. data/vendor/isotree/src/robinmap/LICENSE +21 -0
  41. data/vendor/isotree/src/robinmap/README.md +483 -0
  42. data/vendor/isotree/src/robinmap/include/tsl/robin_growth_policy.h +406 -0
  43. data/vendor/isotree/src/robinmap/include/tsl/robin_hash.h +1620 -0
  44. data/vendor/isotree/src/robinmap/include/tsl/robin_map.h +807 -0
  45. data/vendor/isotree/src/robinmap/include/tsl/robin_set.h +660 -0
  46. data/vendor/isotree/src/serialize.cpp +4300 -139
  47. data/vendor/isotree/src/sql.cpp +141 -59
  48. data/vendor/isotree/src/subset_models.cpp +174 -0
  49. data/vendor/isotree/src/utils.hpp +3808 -0
  50. data/vendor/isotree/src/xoshiro.hpp +467 -0
  51. data/vendor/isotree/src/ziggurat.hpp +405 -0
  52. metadata +38 -104
  53. data/vendor/cereal/LICENSE +0 -24
  54. data/vendor/cereal/README.md +0 -85
  55. data/vendor/cereal/include/cereal/access.hpp +0 -351
  56. data/vendor/cereal/include/cereal/archives/adapters.hpp +0 -163
  57. data/vendor/cereal/include/cereal/archives/binary.hpp +0 -169
  58. data/vendor/cereal/include/cereal/archives/json.hpp +0 -1019
  59. data/vendor/cereal/include/cereal/archives/portable_binary.hpp +0 -334
  60. data/vendor/cereal/include/cereal/archives/xml.hpp +0 -956
  61. data/vendor/cereal/include/cereal/cereal.hpp +0 -1089
  62. data/vendor/cereal/include/cereal/details/helpers.hpp +0 -422
  63. data/vendor/cereal/include/cereal/details/polymorphic_impl.hpp +0 -796
  64. data/vendor/cereal/include/cereal/details/polymorphic_impl_fwd.hpp +0 -65
  65. data/vendor/cereal/include/cereal/details/static_object.hpp +0 -127
  66. data/vendor/cereal/include/cereal/details/traits.hpp +0 -1411
  67. data/vendor/cereal/include/cereal/details/util.hpp +0 -84
  68. data/vendor/cereal/include/cereal/external/base64.hpp +0 -134
  69. data/vendor/cereal/include/cereal/external/rapidjson/allocators.h +0 -284
  70. data/vendor/cereal/include/cereal/external/rapidjson/cursorstreamwrapper.h +0 -78
  71. data/vendor/cereal/include/cereal/external/rapidjson/document.h +0 -2652
  72. data/vendor/cereal/include/cereal/external/rapidjson/encodedstream.h +0 -299
  73. data/vendor/cereal/include/cereal/external/rapidjson/encodings.h +0 -716
  74. data/vendor/cereal/include/cereal/external/rapidjson/error/en.h +0 -74
  75. data/vendor/cereal/include/cereal/external/rapidjson/error/error.h +0 -161
  76. data/vendor/cereal/include/cereal/external/rapidjson/filereadstream.h +0 -99
  77. data/vendor/cereal/include/cereal/external/rapidjson/filewritestream.h +0 -104
  78. data/vendor/cereal/include/cereal/external/rapidjson/fwd.h +0 -151
  79. data/vendor/cereal/include/cereal/external/rapidjson/internal/biginteger.h +0 -290
  80. data/vendor/cereal/include/cereal/external/rapidjson/internal/diyfp.h +0 -271
  81. data/vendor/cereal/include/cereal/external/rapidjson/internal/dtoa.h +0 -245
  82. data/vendor/cereal/include/cereal/external/rapidjson/internal/ieee754.h +0 -78
  83. data/vendor/cereal/include/cereal/external/rapidjson/internal/itoa.h +0 -308
  84. data/vendor/cereal/include/cereal/external/rapidjson/internal/meta.h +0 -186
  85. data/vendor/cereal/include/cereal/external/rapidjson/internal/pow10.h +0 -55
  86. data/vendor/cereal/include/cereal/external/rapidjson/internal/regex.h +0 -740
  87. data/vendor/cereal/include/cereal/external/rapidjson/internal/stack.h +0 -232
  88. data/vendor/cereal/include/cereal/external/rapidjson/internal/strfunc.h +0 -69
  89. data/vendor/cereal/include/cereal/external/rapidjson/internal/strtod.h +0 -290
  90. data/vendor/cereal/include/cereal/external/rapidjson/internal/swap.h +0 -46
  91. data/vendor/cereal/include/cereal/external/rapidjson/istreamwrapper.h +0 -128
  92. data/vendor/cereal/include/cereal/external/rapidjson/memorybuffer.h +0 -70
  93. data/vendor/cereal/include/cereal/external/rapidjson/memorystream.h +0 -71
  94. data/vendor/cereal/include/cereal/external/rapidjson/msinttypes/inttypes.h +0 -316
  95. data/vendor/cereal/include/cereal/external/rapidjson/msinttypes/stdint.h +0 -300
  96. data/vendor/cereal/include/cereal/external/rapidjson/ostreamwrapper.h +0 -81
  97. data/vendor/cereal/include/cereal/external/rapidjson/pointer.h +0 -1414
  98. data/vendor/cereal/include/cereal/external/rapidjson/prettywriter.h +0 -277
  99. data/vendor/cereal/include/cereal/external/rapidjson/rapidjson.h +0 -656
  100. data/vendor/cereal/include/cereal/external/rapidjson/reader.h +0 -2230
  101. data/vendor/cereal/include/cereal/external/rapidjson/schema.h +0 -2497
  102. data/vendor/cereal/include/cereal/external/rapidjson/stream.h +0 -223
  103. data/vendor/cereal/include/cereal/external/rapidjson/stringbuffer.h +0 -121
  104. data/vendor/cereal/include/cereal/external/rapidjson/writer.h +0 -709
  105. data/vendor/cereal/include/cereal/external/rapidxml/license.txt +0 -52
  106. data/vendor/cereal/include/cereal/external/rapidxml/manual.html +0 -406
  107. data/vendor/cereal/include/cereal/external/rapidxml/rapidxml.hpp +0 -2624
  108. data/vendor/cereal/include/cereal/external/rapidxml/rapidxml_iterators.hpp +0 -175
  109. data/vendor/cereal/include/cereal/external/rapidxml/rapidxml_print.hpp +0 -428
  110. data/vendor/cereal/include/cereal/external/rapidxml/rapidxml_utils.hpp +0 -123
  111. data/vendor/cereal/include/cereal/macros.hpp +0 -154
  112. data/vendor/cereal/include/cereal/specialize.hpp +0 -139
  113. data/vendor/cereal/include/cereal/types/array.hpp +0 -79
  114. data/vendor/cereal/include/cereal/types/atomic.hpp +0 -55
  115. data/vendor/cereal/include/cereal/types/base_class.hpp +0 -203
  116. data/vendor/cereal/include/cereal/types/bitset.hpp +0 -176
  117. data/vendor/cereal/include/cereal/types/boost_variant.hpp +0 -164
  118. data/vendor/cereal/include/cereal/types/chrono.hpp +0 -72
  119. data/vendor/cereal/include/cereal/types/common.hpp +0 -129
  120. data/vendor/cereal/include/cereal/types/complex.hpp +0 -56
  121. data/vendor/cereal/include/cereal/types/concepts/pair_associative_container.hpp +0 -73
  122. data/vendor/cereal/include/cereal/types/deque.hpp +0 -62
  123. data/vendor/cereal/include/cereal/types/forward_list.hpp +0 -68
  124. data/vendor/cereal/include/cereal/types/functional.hpp +0 -43
  125. data/vendor/cereal/include/cereal/types/list.hpp +0 -62
  126. data/vendor/cereal/include/cereal/types/map.hpp +0 -36
  127. data/vendor/cereal/include/cereal/types/memory.hpp +0 -425
  128. data/vendor/cereal/include/cereal/types/optional.hpp +0 -66
  129. data/vendor/cereal/include/cereal/types/polymorphic.hpp +0 -483
  130. data/vendor/cereal/include/cereal/types/queue.hpp +0 -132
  131. data/vendor/cereal/include/cereal/types/set.hpp +0 -103
  132. data/vendor/cereal/include/cereal/types/stack.hpp +0 -76
  133. data/vendor/cereal/include/cereal/types/string.hpp +0 -61
  134. data/vendor/cereal/include/cereal/types/tuple.hpp +0 -123
  135. data/vendor/cereal/include/cereal/types/unordered_map.hpp +0 -36
  136. data/vendor/cereal/include/cereal/types/unordered_set.hpp +0 -99
  137. data/vendor/cereal/include/cereal/types/utility.hpp +0 -47
  138. data/vendor/cereal/include/cereal/types/valarray.hpp +0 -89
  139. data/vendor/cereal/include/cereal/types/variant.hpp +0 -109
  140. data/vendor/cereal/include/cereal/types/vector.hpp +0 -112
  141. data/vendor/cereal/include/cereal/version.hpp +0 -52
  142. data/vendor/isotree/src/Makevars +0 -4
  143. data/vendor/isotree/src/crit.cpp +0 -912
  144. data/vendor/isotree/src/dist.cpp +0 -749
  145. data/vendor/isotree/src/extended.cpp +0 -790
  146. data/vendor/isotree/src/fit_model.cpp +0 -1090
  147. data/vendor/isotree/src/helpers_iforest.cpp +0 -324
  148. data/vendor/isotree/src/isoforest.cpp +0 -771
  149. data/vendor/isotree/src/mult.cpp +0 -607
  150. data/vendor/isotree/src/predict.cpp +0 -853
  151. data/vendor/isotree/src/utils.cpp +0 -1566
@@ -1,84 +0,0 @@
1
- /*! \file util.hpp
2
- \brief Internal misc utilities
3
- \ingroup Internal */
4
- /*
5
- Copyright (c) 2014, Randolph Voorhies, Shane Grant
6
- All rights reserved.
7
-
8
- Redistribution and use in source and binary forms, with or without
9
- modification, are permitted provided that the following conditions are met:
10
- * Redistributions of source code must retain the above copyright
11
- notice, this list of conditions and the following disclaimer.
12
- * Redistributions in binary form must reproduce the above copyright
13
- notice, this list of conditions and the following disclaimer in the
14
- documentation and/or other materials provided with the distribution.
15
- * Neither the name of cereal nor the
16
- names of its contributors may be used to endorse or promote products
17
- derived from this software without specific prior written permission.
18
-
19
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20
- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21
- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
- DISCLAIMED. IN NO EVENT SHALL RANDOLPH VOORHIES OR SHANE GRANT BE LIABLE FOR ANY
23
- DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24
- (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25
- LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26
- ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28
- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
- */
30
- #ifndef CEREAL_DETAILS_UTIL_HPP_
31
- #define CEREAL_DETAILS_UTIL_HPP_
32
-
33
- #include <typeinfo>
34
- #include <string>
35
-
36
- #ifdef _MSC_VER
37
- namespace cereal
38
- {
39
- namespace util
40
- {
41
- //! Demangles the type encoded in a string
42
- /*! @internal */
43
- inline std::string demangle( std::string const & name )
44
- { return name; }
45
-
46
- //! Gets the demangled name of a type
47
- /*! @internal */
48
- template <class T> inline
49
- std::string demangledName()
50
- { return typeid( T ).name(); }
51
- } // namespace util
52
- } // namespace cereal
53
- #else // clang or gcc
54
- #include <cxxabi.h>
55
- #include <cstdlib>
56
- namespace cereal
57
- {
58
- namespace util
59
- {
60
- //! Demangles the type encoded in a string
61
- /*! @internal */
62
- inline std::string demangle(std::string mangledName)
63
- {
64
- int status = 0;
65
- char *demangledName = nullptr;
66
- std::size_t len;
67
-
68
- demangledName = abi::__cxa_demangle(mangledName.c_str(), 0, &len, &status);
69
-
70
- std::string retName(demangledName);
71
- free(demangledName);
72
-
73
- return retName;
74
- }
75
-
76
- //! Gets the demangled name of a type
77
- /*! @internal */
78
- template<class T> inline
79
- std::string demangledName()
80
- { return demangle(typeid(T).name()); }
81
- }
82
- } // namespace cereal
83
- #endif // clang or gcc branch of _MSC_VER
84
- #endif // CEREAL_DETAILS_UTIL_HPP_
@@ -1,134 +0,0 @@
1
- /*
2
- Copyright (C) 2004-2008 René Nyffenegger
3
-
4
- This source code is provided 'as-is', without any express or implied
5
- warranty. In no event will the author be held liable for any damages
6
- arising from the use of this software.
7
-
8
- Permission is granted to anyone to use this software for any purpose,
9
- including commercial applications, and to alter it and redistribute it
10
- freely, subject to the following restrictions:
11
-
12
- 1. The origin of this source code must not be misrepresented; you must not
13
- claim that you wrote the original source code. If you use this source code
14
- in a product, an acknowledgment in the product documentation would be
15
- appreciated but is not required.
16
-
17
- 2. Altered source versions must be plainly marked as such, and must not be
18
- misrepresented as being the original source code.
19
-
20
- 3. This notice may not be removed or altered from any source distribution.
21
-
22
- René Nyffenegger rene.nyffenegger@adp-gmbh.ch
23
- */
24
-
25
- #ifndef CEREAL_EXTERNAL_BASE64_HPP_
26
- #define CEREAL_EXTERNAL_BASE64_HPP_
27
-
28
- #ifdef __GNUC__
29
- #pragma GCC diagnostic push
30
- #pragma GCC diagnostic ignored "-Wconversion"
31
- #endif
32
-
33
- #include <string>
34
-
35
- namespace cereal
36
- {
37
- namespace base64
38
- {
39
- static const std::string chars =
40
- "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
41
- "abcdefghijklmnopqrstuvwxyz"
42
- "0123456789+/";
43
-
44
- static inline bool is_base64(unsigned char c) {
45
- return (isalnum(c) || (c == '+') || (c == '/'));
46
- }
47
-
48
- inline std::string encode(unsigned char const* bytes_to_encode, size_t in_len) {
49
- std::string ret;
50
- int i = 0;
51
- int j = 0;
52
- unsigned char char_array_3[3];
53
- unsigned char char_array_4[4];
54
-
55
- while (in_len--) {
56
- char_array_3[i++] = *(bytes_to_encode++);
57
- if (i == 3) {
58
- char_array_4[0] = static_cast<unsigned char>((char_array_3[0] & 0xfc) >> 2);
59
- char_array_4[1] = static_cast<unsigned char>( ( ( char_array_3[0] & 0x03 ) << 4 ) + ( ( char_array_3[1] & 0xf0 ) >> 4 ) );
60
- char_array_4[2] = static_cast<unsigned char>( ( ( char_array_3[1] & 0x0f ) << 2 ) + ( ( char_array_3[2] & 0xc0 ) >> 6 ) );
61
- char_array_4[3] = static_cast<unsigned char>( char_array_3[2] & 0x3f );
62
-
63
- for(i = 0; (i <4) ; i++)
64
- ret += chars[char_array_4[i]];
65
- i = 0;
66
- }
67
- }
68
-
69
- if (i)
70
- {
71
- for(j = i; j < 3; j++)
72
- char_array_3[j] = '\0';
73
-
74
- char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
75
- char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
76
- char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
77
- char_array_4[3] = char_array_3[2] & 0x3f;
78
-
79
- for (j = 0; (j < i + 1); j++)
80
- ret += chars[char_array_4[j]];
81
-
82
- while((i++ < 3))
83
- ret += '=';
84
- }
85
-
86
- return ret;
87
- }
88
-
89
- inline std::string decode(std::string const& encoded_string) {
90
- size_t in_len = encoded_string.size();
91
- size_t i = 0;
92
- size_t j = 0;
93
- int in_ = 0;
94
- unsigned char char_array_4[4], char_array_3[3];
95
- std::string ret;
96
-
97
- while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
98
- char_array_4[i++] = encoded_string[in_]; in_++;
99
- if (i ==4) {
100
- for (i = 0; i <4; i++)
101
- char_array_4[i] = static_cast<unsigned char>(chars.find( char_array_4[i] ));
102
-
103
- char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
104
- char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
105
- char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
106
-
107
- for (i = 0; (i < 3); i++)
108
- ret += char_array_3[i];
109
- i = 0;
110
- }
111
- }
112
-
113
- if (i) {
114
- for (j = i; j <4; j++)
115
- char_array_4[j] = 0;
116
-
117
- for (j = 0; j <4; j++)
118
- char_array_4[j] = static_cast<unsigned char>(chars.find( char_array_4[j] ));
119
-
120
- char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
121
- char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
122
- char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
123
-
124
- for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
125
- }
126
-
127
- return ret;
128
- }
129
- } // namespace base64
130
- } // namespace cereal
131
- #ifdef __GNUC__
132
- #pragma GCC diagnostic pop
133
- #endif
134
- #endif // CEREAL_EXTERNAL_BASE64_HPP_
@@ -1,284 +0,0 @@
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_ALLOCATORS_H_
16
- #define CEREAL_RAPIDJSON_ALLOCATORS_H_
17
-
18
- #include "rapidjson.h"
19
-
20
- CEREAL_RAPIDJSON_NAMESPACE_BEGIN
21
-
22
- ///////////////////////////////////////////////////////////////////////////////
23
- // Allocator
24
-
25
- /*! \class rapidjson::Allocator
26
- \brief Concept for allocating, resizing and freeing memory block.
27
-
28
- Note that Malloc() and Realloc() are non-static but Free() is static.
29
-
30
- So if an allocator need to support Free(), it needs to put its pointer in
31
- the header of memory block.
32
-
33
- \code
34
- concept Allocator {
35
- static const bool kNeedFree; //!< Whether this allocator needs to call Free().
36
-
37
- // Allocate a memory block.
38
- // \param size of the memory block in bytes.
39
- // \returns pointer to the memory block.
40
- void* Malloc(size_t size);
41
-
42
- // Resize a memory block.
43
- // \param originalPtr The pointer to current memory block. Null pointer is permitted.
44
- // \param originalSize The current size in bytes. (Design issue: since some allocator may not book-keep this, explicitly pass to it can save memory.)
45
- // \param newSize the new size in bytes.
46
- void* Realloc(void* originalPtr, size_t originalSize, size_t newSize);
47
-
48
- // Free a memory block.
49
- // \param pointer to the memory block. Null pointer is permitted.
50
- static void Free(void *ptr);
51
- };
52
- \endcode
53
- */
54
-
55
-
56
- /*! \def CEREAL_RAPIDJSON_ALLOCATOR_DEFAULT_CHUNK_CAPACITY
57
- \ingroup CEREAL_RAPIDJSON_CONFIG
58
- \brief User-defined kDefaultChunkCapacity definition.
59
-
60
- User can define this as any \c size that is a power of 2.
61
- */
62
-
63
- #ifndef CEREAL_RAPIDJSON_ALLOCATOR_DEFAULT_CHUNK_CAPACITY
64
- #define CEREAL_RAPIDJSON_ALLOCATOR_DEFAULT_CHUNK_CAPACITY (64 * 1024)
65
- #endif
66
-
67
-
68
- ///////////////////////////////////////////////////////////////////////////////
69
- // CrtAllocator
70
-
71
- //! C-runtime library allocator.
72
- /*! This class is just wrapper for standard C library memory routines.
73
- \note implements Allocator concept
74
- */
75
- class CrtAllocator {
76
- public:
77
- static const bool kNeedFree = true;
78
- void* Malloc(size_t size) {
79
- if (size) // behavior of malloc(0) is implementation defined.
80
- return std::malloc(size);
81
- else
82
- return NULL; // standardize to returning NULL.
83
- }
84
- void* Realloc(void* originalPtr, size_t originalSize, size_t newSize) {
85
- (void)originalSize;
86
- if (newSize == 0) {
87
- std::free(originalPtr);
88
- return NULL;
89
- }
90
- return std::realloc(originalPtr, newSize);
91
- }
92
- static void Free(void *ptr) { std::free(ptr); }
93
- };
94
-
95
- ///////////////////////////////////////////////////////////////////////////////
96
- // MemoryPoolAllocator
97
-
98
- //! Default memory allocator used by the parser and DOM.
99
- /*! This allocator allocate memory blocks from pre-allocated memory chunks.
100
-
101
- It does not free memory blocks. And Realloc() only allocate new memory.
102
-
103
- The memory chunks are allocated by BaseAllocator, which is CrtAllocator by default.
104
-
105
- User may also supply a buffer as the first chunk.
106
-
107
- If the user-buffer is full then additional chunks are allocated by BaseAllocator.
108
-
109
- The user-buffer is not deallocated by this allocator.
110
-
111
- \tparam BaseAllocator the allocator type for allocating memory chunks. Default is CrtAllocator.
112
- \note implements Allocator concept
113
- */
114
- template <typename BaseAllocator = CrtAllocator>
115
- class MemoryPoolAllocator {
116
- public:
117
- static const bool kNeedFree = false; //!< Tell users that no need to call Free() with this allocator. (concept Allocator)
118
-
119
- //! Constructor with chunkSize.
120
- /*! \param chunkSize The size of memory chunk. The default is kDefaultChunkSize.
121
- \param baseAllocator The allocator for allocating memory chunks.
122
- */
123
- MemoryPoolAllocator(size_t chunkSize = kDefaultChunkCapacity, BaseAllocator* baseAllocator = 0) :
124
- chunkHead_(0), chunk_capacity_(chunkSize), userBuffer_(0), baseAllocator_(baseAllocator), ownBaseAllocator_(0)
125
- {
126
- }
127
-
128
- //! Constructor with user-supplied buffer.
129
- /*! The user buffer will be used firstly. When it is full, memory pool allocates new chunk with chunk size.
130
-
131
- The user buffer will not be deallocated when this allocator is destructed.
132
-
133
- \param buffer User supplied buffer.
134
- \param size Size of the buffer in bytes. It must at least larger than sizeof(ChunkHeader).
135
- \param chunkSize The size of memory chunk. The default is kDefaultChunkSize.
136
- \param baseAllocator The allocator for allocating memory chunks.
137
- */
138
- MemoryPoolAllocator(void *buffer, size_t size, size_t chunkSize = kDefaultChunkCapacity, BaseAllocator* baseAllocator = 0) :
139
- chunkHead_(0), chunk_capacity_(chunkSize), userBuffer_(buffer), baseAllocator_(baseAllocator), ownBaseAllocator_(0)
140
- {
141
- CEREAL_RAPIDJSON_ASSERT(buffer != 0);
142
- CEREAL_RAPIDJSON_ASSERT(size > sizeof(ChunkHeader));
143
- chunkHead_ = reinterpret_cast<ChunkHeader*>(buffer);
144
- chunkHead_->capacity = size - sizeof(ChunkHeader);
145
- chunkHead_->size = 0;
146
- chunkHead_->next = 0;
147
- }
148
-
149
- //! Destructor.
150
- /*! This deallocates all memory chunks, excluding the user-supplied buffer.
151
- */
152
- ~MemoryPoolAllocator() {
153
- Clear();
154
- CEREAL_RAPIDJSON_DELETE(ownBaseAllocator_);
155
- }
156
-
157
- //! Deallocates all memory chunks, excluding the user-supplied buffer.
158
- void Clear() {
159
- while (chunkHead_ && chunkHead_ != userBuffer_) {
160
- ChunkHeader* next = chunkHead_->next;
161
- baseAllocator_->Free(chunkHead_);
162
- chunkHead_ = next;
163
- }
164
- if (chunkHead_ && chunkHead_ == userBuffer_)
165
- chunkHead_->size = 0; // Clear user buffer
166
- }
167
-
168
- //! Computes the total capacity of allocated memory chunks.
169
- /*! \return total capacity in bytes.
170
- */
171
- size_t Capacity() const {
172
- size_t capacity = 0;
173
- for (ChunkHeader* c = chunkHead_; c != 0; c = c->next)
174
- capacity += c->capacity;
175
- return capacity;
176
- }
177
-
178
- //! Computes the memory blocks allocated.
179
- /*! \return total used bytes.
180
- */
181
- size_t Size() const {
182
- size_t size = 0;
183
- for (ChunkHeader* c = chunkHead_; c != 0; c = c->next)
184
- size += c->size;
185
- return size;
186
- }
187
-
188
- //! Allocates a memory block. (concept Allocator)
189
- void* Malloc(size_t size) {
190
- if (!size)
191
- return NULL;
192
-
193
- size = CEREAL_RAPIDJSON_ALIGN(size);
194
- if (chunkHead_ == 0 || chunkHead_->size + size > chunkHead_->capacity)
195
- if (!AddChunk(chunk_capacity_ > size ? chunk_capacity_ : size))
196
- return NULL;
197
-
198
- void *buffer = reinterpret_cast<char *>(chunkHead_) + CEREAL_RAPIDJSON_ALIGN(sizeof(ChunkHeader)) + chunkHead_->size;
199
- chunkHead_->size += size;
200
- return buffer;
201
- }
202
-
203
- //! Resizes a memory block (concept Allocator)
204
- void* Realloc(void* originalPtr, size_t originalSize, size_t newSize) {
205
- if (originalPtr == 0)
206
- return Malloc(newSize);
207
-
208
- if (newSize == 0)
209
- return NULL;
210
-
211
- originalSize = CEREAL_RAPIDJSON_ALIGN(originalSize);
212
- newSize = CEREAL_RAPIDJSON_ALIGN(newSize);
213
-
214
- // Do not shrink if new size is smaller than original
215
- if (originalSize >= newSize)
216
- return originalPtr;
217
-
218
- // Simply expand it if it is the last allocation and there is sufficient space
219
- if (originalPtr == reinterpret_cast<char *>(chunkHead_) + CEREAL_RAPIDJSON_ALIGN(sizeof(ChunkHeader)) + chunkHead_->size - originalSize) {
220
- size_t increment = static_cast<size_t>(newSize - originalSize);
221
- if (chunkHead_->size + increment <= chunkHead_->capacity) {
222
- chunkHead_->size += increment;
223
- return originalPtr;
224
- }
225
- }
226
-
227
- // Realloc process: allocate and copy memory, do not free original buffer.
228
- if (void* newBuffer = Malloc(newSize)) {
229
- if (originalSize)
230
- std::memcpy(newBuffer, originalPtr, originalSize);
231
- return newBuffer;
232
- }
233
- else
234
- return NULL;
235
- }
236
-
237
- //! Frees a memory block (concept Allocator)
238
- static void Free(void *ptr) { (void)ptr; } // Do nothing
239
-
240
- private:
241
- //! Copy constructor is not permitted.
242
- MemoryPoolAllocator(const MemoryPoolAllocator& rhs) /* = delete */;
243
- //! Copy assignment operator is not permitted.
244
- MemoryPoolAllocator& operator=(const MemoryPoolAllocator& rhs) /* = delete */;
245
-
246
- //! Creates a new chunk.
247
- /*! \param capacity Capacity of the chunk in bytes.
248
- \return true if success.
249
- */
250
- bool AddChunk(size_t capacity) {
251
- if (!baseAllocator_)
252
- ownBaseAllocator_ = baseAllocator_ = CEREAL_RAPIDJSON_NEW(BaseAllocator)();
253
- if (ChunkHeader* chunk = reinterpret_cast<ChunkHeader*>(baseAllocator_->Malloc(CEREAL_RAPIDJSON_ALIGN(sizeof(ChunkHeader)) + capacity))) {
254
- chunk->capacity = capacity;
255
- chunk->size = 0;
256
- chunk->next = chunkHead_;
257
- chunkHead_ = chunk;
258
- return true;
259
- }
260
- else
261
- return false;
262
- }
263
-
264
- static const int kDefaultChunkCapacity = CEREAL_RAPIDJSON_ALLOCATOR_DEFAULT_CHUNK_CAPACITY; //!< Default chunk capacity.
265
-
266
- //! Chunk header for perpending to each chunk.
267
- /*! Chunks are stored as a singly linked list.
268
- */
269
- struct ChunkHeader {
270
- size_t capacity; //!< Capacity of the chunk in bytes (excluding the header itself).
271
- size_t size; //!< Current size of allocated memory in bytes.
272
- ChunkHeader *next; //!< Next chunk in the linked list.
273
- };
274
-
275
- ChunkHeader *chunkHead_; //!< Head of the chunk linked-list. Only the head chunk serves allocation.
276
- size_t chunk_capacity_; //!< The minimum capacity of chunk when they are allocated.
277
- void *userBuffer_; //!< User supplied buffer.
278
- BaseAllocator* baseAllocator_; //!< base allocator for allocating memory chunks.
279
- BaseAllocator* ownBaseAllocator_; //!< base allocator created by this object.
280
- };
281
-
282
- CEREAL_RAPIDJSON_NAMESPACE_END
283
-
284
- #endif // CEREAL_RAPIDJSON_ENCODINGS_H_
@@ -1,78 +0,0 @@
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_CURSORSTREAMWRAPPER_H_
16
- #define CEREAL_RAPIDJSON_CURSORSTREAMWRAPPER_H_
17
-
18
- #include "stream.h"
19
-
20
- #if defined(__GNUC__)
21
- CEREAL_RAPIDJSON_DIAG_PUSH
22
- CEREAL_RAPIDJSON_DIAG_OFF(effc++)
23
- #endif
24
-
25
- #if defined(_MSC_VER) && _MSC_VER <= 1800
26
- CEREAL_RAPIDJSON_DIAG_PUSH
27
- CEREAL_RAPIDJSON_DIAG_OFF(4702) // unreachable code
28
- CEREAL_RAPIDJSON_DIAG_OFF(4512) // assignment operator could not be generated
29
- #endif
30
-
31
- CEREAL_RAPIDJSON_NAMESPACE_BEGIN
32
-
33
-
34
- //! Cursor stream wrapper for counting line and column number if error exists.
35
- /*!
36
- \tparam InputStream Any stream that implements Stream Concept
37
- */
38
- template <typename InputStream, typename Encoding = UTF8<> >
39
- class CursorStreamWrapper : public GenericStreamWrapper<InputStream, Encoding> {
40
- public:
41
- typedef typename Encoding::Ch Ch;
42
-
43
- CursorStreamWrapper(InputStream& is):
44
- GenericStreamWrapper<InputStream, Encoding>(is), line_(1), col_(0) {}
45
-
46
- // counting line and column number
47
- Ch Take() {
48
- Ch ch = this->is_.Take();
49
- if(ch == '\n') {
50
- line_ ++;
51
- col_ = 0;
52
- } else {
53
- col_ ++;
54
- }
55
- return ch;
56
- }
57
-
58
- //! Get the error line number, if error exists.
59
- size_t GetLine() const { return line_; }
60
- //! Get the error column number, if error exists.
61
- size_t GetColumn() const { return col_; }
62
-
63
- private:
64
- size_t line_; //!< Current Line
65
- size_t col_; //!< Current Column
66
- };
67
-
68
- #if defined(_MSC_VER) && _MSC_VER <= 1800
69
- CEREAL_RAPIDJSON_DIAG_POP
70
- #endif
71
-
72
- #if defined(__GNUC__)
73
- CEREAL_RAPIDJSON_DIAG_POP
74
- #endif
75
-
76
- CEREAL_RAPIDJSON_NAMESPACE_END
77
-
78
- #endif // CEREAL_RAPIDJSON_CURSORSTREAMWRAPPER_H_