isotree 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/LICENSE.txt +2 -2
- data/README.md +22 -1
- data/ext/isotree/ext.cpp +26 -0
- data/ext/isotree/extconf.rb +3 -3
- data/lib/isotree.rb +1 -0
- data/lib/isotree/isolation_forest.rb +86 -1
- data/lib/isotree/version.rb +1 -1
- data/vendor/cereal/LICENSE +24 -0
- data/vendor/cereal/README.md +85 -0
- data/vendor/cereal/include/cereal/access.hpp +351 -0
- data/vendor/cereal/include/cereal/archives/adapters.hpp +163 -0
- data/vendor/cereal/include/cereal/archives/binary.hpp +169 -0
- data/vendor/cereal/include/cereal/archives/json.hpp +1019 -0
- data/vendor/cereal/include/cereal/archives/portable_binary.hpp +334 -0
- data/vendor/cereal/include/cereal/archives/xml.hpp +956 -0
- data/vendor/cereal/include/cereal/cereal.hpp +1089 -0
- data/vendor/cereal/include/cereal/details/helpers.hpp +422 -0
- data/vendor/cereal/include/cereal/details/polymorphic_impl.hpp +796 -0
- data/vendor/cereal/include/cereal/details/polymorphic_impl_fwd.hpp +65 -0
- data/vendor/cereal/include/cereal/details/static_object.hpp +127 -0
- data/vendor/cereal/include/cereal/details/traits.hpp +1411 -0
- data/vendor/cereal/include/cereal/details/util.hpp +84 -0
- data/vendor/cereal/include/cereal/external/base64.hpp +134 -0
- data/vendor/cereal/include/cereal/external/rapidjson/allocators.h +284 -0
- data/vendor/cereal/include/cereal/external/rapidjson/cursorstreamwrapper.h +78 -0
- data/vendor/cereal/include/cereal/external/rapidjson/document.h +2652 -0
- data/vendor/cereal/include/cereal/external/rapidjson/encodedstream.h +299 -0
- data/vendor/cereal/include/cereal/external/rapidjson/encodings.h +716 -0
- data/vendor/cereal/include/cereal/external/rapidjson/error/en.h +74 -0
- data/vendor/cereal/include/cereal/external/rapidjson/error/error.h +161 -0
- data/vendor/cereal/include/cereal/external/rapidjson/filereadstream.h +99 -0
- data/vendor/cereal/include/cereal/external/rapidjson/filewritestream.h +104 -0
- data/vendor/cereal/include/cereal/external/rapidjson/fwd.h +151 -0
- data/vendor/cereal/include/cereal/external/rapidjson/internal/biginteger.h +290 -0
- data/vendor/cereal/include/cereal/external/rapidjson/internal/diyfp.h +271 -0
- data/vendor/cereal/include/cereal/external/rapidjson/internal/dtoa.h +245 -0
- data/vendor/cereal/include/cereal/external/rapidjson/internal/ieee754.h +78 -0
- data/vendor/cereal/include/cereal/external/rapidjson/internal/itoa.h +308 -0
- data/vendor/cereal/include/cereal/external/rapidjson/internal/meta.h +186 -0
- data/vendor/cereal/include/cereal/external/rapidjson/internal/pow10.h +55 -0
- data/vendor/cereal/include/cereal/external/rapidjson/internal/regex.h +740 -0
- data/vendor/cereal/include/cereal/external/rapidjson/internal/stack.h +232 -0
- data/vendor/cereal/include/cereal/external/rapidjson/internal/strfunc.h +69 -0
- data/vendor/cereal/include/cereal/external/rapidjson/internal/strtod.h +290 -0
- data/vendor/cereal/include/cereal/external/rapidjson/internal/swap.h +46 -0
- data/vendor/cereal/include/cereal/external/rapidjson/istreamwrapper.h +128 -0
- data/vendor/cereal/include/cereal/external/rapidjson/memorybuffer.h +70 -0
- data/vendor/cereal/include/cereal/external/rapidjson/memorystream.h +71 -0
- data/vendor/cereal/include/cereal/external/rapidjson/msinttypes/inttypes.h +316 -0
- data/vendor/cereal/include/cereal/external/rapidjson/msinttypes/stdint.h +300 -0
- data/vendor/cereal/include/cereal/external/rapidjson/ostreamwrapper.h +81 -0
- data/vendor/cereal/include/cereal/external/rapidjson/pointer.h +1414 -0
- data/vendor/cereal/include/cereal/external/rapidjson/prettywriter.h +277 -0
- data/vendor/cereal/include/cereal/external/rapidjson/rapidjson.h +656 -0
- data/vendor/cereal/include/cereal/external/rapidjson/reader.h +2230 -0
- data/vendor/cereal/include/cereal/external/rapidjson/schema.h +2497 -0
- data/vendor/cereal/include/cereal/external/rapidjson/stream.h +223 -0
- data/vendor/cereal/include/cereal/external/rapidjson/stringbuffer.h +121 -0
- data/vendor/cereal/include/cereal/external/rapidjson/writer.h +709 -0
- data/vendor/cereal/include/cereal/external/rapidxml/license.txt +52 -0
- data/vendor/cereal/include/cereal/external/rapidxml/manual.html +406 -0
- data/vendor/cereal/include/cereal/external/rapidxml/rapidxml.hpp +2624 -0
- data/vendor/cereal/include/cereal/external/rapidxml/rapidxml_iterators.hpp +175 -0
- data/vendor/cereal/include/cereal/external/rapidxml/rapidxml_print.hpp +428 -0
- data/vendor/cereal/include/cereal/external/rapidxml/rapidxml_utils.hpp +123 -0
- data/vendor/cereal/include/cereal/macros.hpp +154 -0
- data/vendor/cereal/include/cereal/specialize.hpp +139 -0
- data/vendor/cereal/include/cereal/types/array.hpp +79 -0
- data/vendor/cereal/include/cereal/types/atomic.hpp +55 -0
- data/vendor/cereal/include/cereal/types/base_class.hpp +203 -0
- data/vendor/cereal/include/cereal/types/bitset.hpp +176 -0
- data/vendor/cereal/include/cereal/types/boost_variant.hpp +164 -0
- data/vendor/cereal/include/cereal/types/chrono.hpp +72 -0
- data/vendor/cereal/include/cereal/types/common.hpp +129 -0
- data/vendor/cereal/include/cereal/types/complex.hpp +56 -0
- data/vendor/cereal/include/cereal/types/concepts/pair_associative_container.hpp +73 -0
- data/vendor/cereal/include/cereal/types/deque.hpp +62 -0
- data/vendor/cereal/include/cereal/types/forward_list.hpp +68 -0
- data/vendor/cereal/include/cereal/types/functional.hpp +43 -0
- data/vendor/cereal/include/cereal/types/list.hpp +62 -0
- data/vendor/cereal/include/cereal/types/map.hpp +36 -0
- data/vendor/cereal/include/cereal/types/memory.hpp +425 -0
- data/vendor/cereal/include/cereal/types/optional.hpp +66 -0
- data/vendor/cereal/include/cereal/types/polymorphic.hpp +483 -0
- data/vendor/cereal/include/cereal/types/queue.hpp +132 -0
- data/vendor/cereal/include/cereal/types/set.hpp +103 -0
- data/vendor/cereal/include/cereal/types/stack.hpp +76 -0
- data/vendor/cereal/include/cereal/types/string.hpp +61 -0
- data/vendor/cereal/include/cereal/types/tuple.hpp +123 -0
- data/vendor/cereal/include/cereal/types/unordered_map.hpp +36 -0
- data/vendor/cereal/include/cereal/types/unordered_set.hpp +99 -0
- data/vendor/cereal/include/cereal/types/utility.hpp +47 -0
- data/vendor/cereal/include/cereal/types/valarray.hpp +89 -0
- data/vendor/cereal/include/cereal/types/variant.hpp +109 -0
- data/vendor/cereal/include/cereal/types/vector.hpp +112 -0
- data/vendor/cereal/include/cereal/version.hpp +52 -0
- data/vendor/isotree/LICENSE +1 -1
- data/vendor/isotree/README.md +2 -1
- data/vendor/isotree/src/RcppExports.cpp +44 -4
- data/vendor/isotree/src/Rwrapper.cpp +141 -51
- data/vendor/isotree/src/crit.cpp +1 -1
- data/vendor/isotree/src/dealloc.cpp +1 -1
- data/vendor/isotree/src/dist.cpp +6 -6
- data/vendor/isotree/src/extended.cpp +5 -5
- data/vendor/isotree/src/fit_model.cpp +30 -19
- data/vendor/isotree/src/helpers_iforest.cpp +26 -11
- data/vendor/isotree/src/impute.cpp +7 -7
- data/vendor/isotree/src/isoforest.cpp +7 -7
- data/vendor/isotree/src/isotree.hpp +27 -5
- data/vendor/isotree/src/merge_models.cpp +1 -1
- data/vendor/isotree/src/mult.cpp +1 -1
- data/vendor/isotree/src/predict.cpp +20 -16
- data/vendor/isotree/src/serialize.cpp +1 -1
- data/vendor/isotree/src/sql.cpp +545 -0
- data/vendor/isotree/src/utils.cpp +36 -44
- metadata +98 -92
@@ -0,0 +1,84 @@
|
|
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_
|
@@ -0,0 +1,134 @@
|
|
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_
|
@@ -0,0 +1,284 @@
|
|
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_
|