Geophysical Inversion and Modelling Library v1.5.4
Loading...
Searching...
No Matches
allocator.hpp
Go to the documentation of this file.
1
6
7#ifndef INCLUDE_KDTREE_ALLOCATOR_HPP
8#define INCLUDE_KDTREE_ALLOCATOR_HPP
9
10#include <cstddef>
11
12#include "node.hpp"
13
14namespace KDTree
15{
16
17 template <typename _Tp, typename _Alloc>
18 class _Alloc_base
19 {
20 public:
21 typedef _Node<_Tp> _Node_;
22 typedef typename _Node_::_Base_ptr _Base_ptr;
23 typedef _Alloc allocator_type;
24
25 _Alloc_base(allocator_type const& __A)
26 : _M_node_allocator(__A) {}
27
28 allocator_type
29 get_allocator() const
30 {
31 return _M_node_allocator;
32 }
33
34
35 class NoLeakAlloc
36 {
37 _Alloc_base * base;
38 _Node_ * new_node;
39
40 public:
41 NoLeakAlloc(_Alloc_base * b) : base(b), new_node(base->_M_allocate_node()) {}
42
43 _Node_ * get() { return new_node; }
44 void disconnect() { new_node = NULL; }
45
46 ~NoLeakAlloc() { if (new_node) base->_M_deallocate_node(new_node); }
47 };
48
49
50 protected:
51 allocator_type _M_node_allocator;
52
53 _Node_*
54 _M_allocate_node()
55 {
56 return _M_node_allocator.allocate(1);
57 }
58
59 void
60 _M_deallocate_node(_Node_* const __P)
61 {
62 return _M_node_allocator.deallocate(__P, 1);
63 }
64
65 void
66 _M_construct_node(_Node_* __p, _Tp const __V = _Tp(),
67 _Base_ptr const __PARENT = NULL,
68 _Base_ptr const __LEFT = NULL,
69 _Base_ptr const __RIGHT = NULL)
70 {
71 new (__p) _Node_(__V, __PARENT, __LEFT, __RIGHT);
72 }
73
74 void
75 _M_destroy_node(_Node_* __p)
76 {
77 _M_node_allocator.destroy(__p);
78 }
79 };
80
81} // namespace KDTree
82
83#endif // include guard
84
85/* COPYRIGHT --
86 *
87 * This file is part of libkdtree++, a C++ template KD-Tree sorting container.
88 * libkdtree++ is (c) 2004-2007 Martin F. Krafft <libkdtree@pobox.madduck.net>
89 * and Sylvain Bougerel <sylvain.bougerel.devel@gmail.com> distributed under the
90 * terms of the Artistic License 2.0. See the ./COPYING file in the source tree
91 * root for more information.
92 *
93 * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
94 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES
95 * OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
96 */
Definition node.hpp:50