LibreOffice Module o3tl (master)  1
vector_pool.hxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #ifndef INCLUDED_O3TL_VECTOR_POOL_HXX
21 #define INCLUDED_O3TL_VECTOR_POOL_HXX
22 
23 #include <vector>
24 
25 namespace o3tl
26 {
27  namespace detail
28  {
29  template<typename ValueType, class Container> class simple_pool_impl :
30  public Container
31  {
32  typedef typename Container::value_type value_type;
33  std::ptrdiff_t mnFirstFreeIndex;
34 
35  public:
37  mnFirstFreeIndex(-1)
38  {}
39 
40  std::ptrdiff_t alloc()
41  {
42  return store(ValueType());
43  }
44 
45  std::ptrdiff_t store(const ValueType& rCopy)
46  {
47  if( mnFirstFreeIndex != -1 )
48  {
49  std::ptrdiff_t nIdx=mnFirstFreeIndex;
50  mnFirstFreeIndex = this->at(mnFirstFreeIndex).nextFree;
51  this->at(nIdx).value = rCopy;
52  this->at(nIdx).nextFree = -1;
53 
54  return nIdx;
55  }
56  else
57  {
58  this->push_back(value_type(rCopy));
59  return this->size()-1;
60  }
61  }
62 
63  void free( std::ptrdiff_t nIdx )
64  {
65  this->at(nIdx).nextFree = mnFirstFreeIndex;
66  mnFirstFreeIndex = nIdx;
67  }
68 
69  const ValueType& get( std::ptrdiff_t nIdx ) const
70  {
71  return this->operator[](nIdx).value;
72  }
73  ValueType& get( std::ptrdiff_t nIdx )
74  {
75  return this->operator[](nIdx).value;
76  }
77  };
78 
79  template< typename ValueType > struct struct_from_value
80  {
81  struct type
82  {
83  type() :
84  value(),
85  nextFree(-1)
86  {}
87  explicit type( const ValueType& val ) :
88  value(val),
89  nextFree(-1)
90  {}
91 
93  std::ptrdiff_t nextFree;
94  };
95  };
96  }
97 
114  template<typename ValueType> struct vector_pool :
115  public detail::simple_pool_impl<ValueType,
116  std::vector<typename detail::struct_from_value<ValueType>::type > >
117  {};
118 }
119 
120 #endif /* INCLUDED_O3TL_VECTOR_POOL_HXX */
121 
122 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Container::value_type value_type
Definition: vector_pool.hxx:32
std::ptrdiff_t store(const ValueType &rCopy)
Definition: vector_pool.hxx:45
size
void free(std::ptrdiff_t nIdx)
Definition: vector_pool.hxx:63
ValueType