00001
00007 #ifndef INCLUDE_KDTREE_ALLOCATOR_HPP
00008 #define INCLUDE_KDTREE_ALLOCATOR_HPP
00009
00010 #include <cstddef>
00011
00012 #include "node.hpp"
00013
00014 namespace KDTree
00015 {
00016
00017 template <typename _Tp, typename _Alloc>
00018 class _Alloc_base
00019 {
00020 public:
00021 typedef _Node<_Tp> _Node_;
00022 typedef typename _Node_::_Base_ptr _Base_ptr;
00023 typedef _Alloc allocator_type;
00024
00025 _Alloc_base(allocator_type const& __A)
00026 : _M_node_allocator(__A) {}
00027
00028 allocator_type
00029 get_allocator() const
00030 {
00031 return _M_node_allocator;
00032 }
00033
00034
00035 class NoLeakAlloc
00036 {
00037 _Alloc_base * base;
00038 _Node_ * new_node;
00039
00040 public:
00041 NoLeakAlloc(_Alloc_base * b) : base(b), new_node(base->_M_allocate_node()) {}
00042
00043 _Node_ * get() { return new_node; }
00044 void disconnect() { new_node = NULL; }
00045
00046 ~NoLeakAlloc() { if (new_node) base->_M_deallocate_node(new_node); }
00047 };
00048
00049
00050 protected:
00051 allocator_type _M_node_allocator;
00052
00053 _Node_*
00054 _M_allocate_node()
00055 {
00056 return _M_node_allocator.allocate(1);
00057 }
00058
00059 void
00060 _M_deallocate_node(_Node_* const __P)
00061 {
00062 return _M_node_allocator.deallocate(__P, 1);
00063 }
00064
00065 void
00066 _M_construct_node(_Node_* __p, _Tp const __V = _Tp(),
00067 _Base_ptr const __PARENT = NULL,
00068 _Base_ptr const __LEFT = NULL,
00069 _Base_ptr const __RIGHT = NULL)
00070 {
00071 new (__p) _Node_(__V, __PARENT, __LEFT, __RIGHT);
00072 }
00073
00074 void
00075 _M_destroy_node(_Node_* __p)
00076 {
00077 _M_node_allocator.destroy(__p);
00078 }
00079 };
00080
00081 }
00082
00083 #endif // include guard
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096