Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / include / o3tl / vector_pool.hxx
blob1b94f316f248d6dc00fc81cb329a84e67c561192
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #ifndef INCLUDED_O3TL_VECTOR_POOL_HXX
21 #define INCLUDED_O3TL_VECTOR_POOL_HXX
23 #include <vector>
25 namespace o3tl
27 namespace detail
29 template<typename ValueType, class Container> class simple_pool_impl :
30 public Container
32 typedef typename Container::value_type value_type;
33 std::ptrdiff_t mnFirstFreeIndex;
35 public:
36 simple_pool_impl() :
37 mnFirstFreeIndex(-1)
40 std::ptrdiff_t alloc()
42 return store(ValueType());
45 std::ptrdiff_t store(const ValueType& rCopy)
47 if( mnFirstFreeIndex != -1 )
49 std::ptrdiff_t nIdx=mnFirstFreeIndex;
50 mnFirstFreeIndex = this->at(mnFirstFreeIndex).nextFree;
51 this->at(nIdx).value = rCopy;
52 this->at(nIdx).nextFree = -1;
54 return nIdx;
56 else
58 this->push_back(value_type(rCopy));
59 return this->size()-1;
63 void free( std::ptrdiff_t nIdx )
65 this->at(nIdx).nextFree = mnFirstFreeIndex;
66 mnFirstFreeIndex = nIdx;
69 const ValueType& get( std::ptrdiff_t nIdx ) const
71 return this->operator[](nIdx).value;
73 ValueType& get( std::ptrdiff_t nIdx )
75 return this->operator[](nIdx).value;
79 template< typename ValueType > struct struct_from_value
81 struct type
83 type() :
84 value(),
85 nextFree(-1)
87 explicit type( const ValueType& val ) :
88 value(val),
89 nextFree(-1)
92 ValueType value;
93 std::ptrdiff_t nextFree;
98 /** Simple vector-based memory pool allocator
100 This template can be used to provide simple pooled memory
101 allocation from a container class that adheres to the stl
102 random access container concept. Note that alloc/free works
103 with _indices_ into the container!
105 @example
106 <pre>
107 vector_pool<type> myPool;
108 int nIdx=myPool.alloc();
109 myPool[nIdx] = myVal;
110 ... do stuff ...
111 myPool.free(nIdx);
112 </pre>
114 template<typename ValueType> struct vector_pool :
115 public detail::simple_pool_impl<ValueType,
116 std::vector<typename detail::struct_from_value<ValueType>::type > >
120 #endif /* INCLUDED_O3TL_VECTOR_POOL_HXX */
122 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */