-//$$CDS-header$$
+/*
+ This file is a part of libcds - Concurrent Data Structures library
+
+ (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2017
+
+ Source code repo: http://github.com/khizmax/libcds/
+ Download: http://sourceforge.net/projects/libcds/files/
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice, this
+ list of conditions and the following disclaimer.
-#ifndef __CDS_IMPL_BOUNDED_ARRAY_H
-#define __CDS_IMPL_BOUNDED_ARRAY_H
+ * Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+#ifndef CDSLIB_IMPL_BOUNDED_ARRAY_H
+#define CDSLIB_IMPL_BOUNDED_ARRAY_H
/*
Dynamic non-growing array
//@cond
namespace cds {
namespace details {
-
- /// Upper bounded dynamic array
- /**
- BoundedArray is dynamic allocated C-array of item of type T with the interface like STL.
- The max size (capacity) of array is defined at ctor time and cannot be changed during object's lifetime
-
- \par Template parameters
- - \p T type of elements
- - \p Allocator dynamic memory allocator class (<tt>std::allocator</tt> semantics)
-
- This class is deprecated: it is based on std::vector and does not support non-copyable type \p T.
- See \ref bounded_array.
- */
- template <typename T, class Allocator = CDS_DEFAULT_ALLOCATOR >
- class BoundedArray: private std::vector< T, typename Allocator::template rebind<T>::other >
- {
- public:
- typedef T value_type ; ///< value type stored in the array
- typedef Allocator allocator_type ; ///< allocator type
- typedef std::vector<T, typename Allocator::template rebind<T>::other> vector_type ; ///< underlying vector type
-
- typedef typename vector_type::iterator iterator ; ///< item iterator
- typedef typename vector_type::const_iterator const_iterator ; ///< item const iterator
-
- public:
- /// Default ctor
- explicit BoundedArray(
- size_t nCapacity ///< capacity
- )
- {
- vector_type::resize( nCapacity );
- assert( size() == capacity() );
- }
-
- /// Ctor with item's initialization
- BoundedArray(
- size_t nCapacity, ///< capacity of array
- const value_type& init, ///< initial value of any item
- size_t nInitCount = 0 ///< how many items will be initialized; 0 - all items
- )
- {
- assert( nInitCount <= nCapacity );
- vector_type::resize( nCapacity );
- assign( nInitCount ? nInitCount : vector_type::capacity(), init );
- assert( size() == capacity() );
- }
-
- const value_type& operator []( size_t nItem ) const
- {
- return vector_type::operator[](nItem);
- }
-
- value_type& operator []( size_t nItem )
- {
- return vector_type::operator[](nItem);
- }
-
- size_t size() const
- {
- return vector_type::size();
- }
-
- size_t capacity() const
- {
- return vector_type::capacity();
- }
-
- /// Returns sizeof(T)
- static size_t itemSize()
- {
- return sizeof(T);
- }
-
- /// Returns pointer to the first item in the array
- value_type * top()
- {
- return & vector_type::front();
- }
-
- friend value_type * operator +( BoundedArray<value_type, allocator_type>& arr, size_t i )
- {
- return &( arr[i] );
- }
-
- /// Get begin iterator
- const_iterator begin() const
- {
- return vector_type::begin();
- }
- iterator begin()
- {
- return vector_type::begin();
- }
-
- /// Get end iterator
- const_iterator end() const
- {
- return vector_type::end();
- }
- iterator end()
- {
- return vector_type::end();
- }
-
- /// Get end iterator for \p nMax-th item
- const_iterator end( size_t nMax ) const
- {
- assert( nMax <= vector_type::capacity());
- return vector_type::begin() + nMax;
- }
- iterator end( size_t nMax )
- {
- assert( nMax <= vector_type::capacity());
- return vector_type::begin() + nMax;
- }
- };
-
/// Bounded dynamic array
/**
The class template is intended for storing fixed-size sequences of objects.
explicit bounded_array(
size_t nCapacity ///< capacity
)
- : m_arr( allocator_impl().NewArray( nCapacity ) )
+ : m_arr( allocator_impl().NewArray( nCapacity ))
, m_nCapacity( nCapacity )
{}
~bounded_array()
{
- allocator_impl().Delete( m_arr, capacity() );
+ allocator_impl().Delete( m_arr, capacity());
}
const value_type& operator []( size_t nItem ) const
{
- assert( nItem < capacity() );
+ assert( nItem < capacity());
return m_arr[nItem];
}
value_type& operator []( size_t nItem )
{
- assert( nItem < capacity() );
+ assert( nItem < capacity());
return m_arr[nItem];
}
} // namespace cds
//@endcond
-#endif // #ifndef __CDS_IMPL_BOUNDED_ARRAY_H
+#endif // #ifndef CDSLIB_IMPL_BOUNDED_ARRAY_H