0347c9a35f97e7d5e4c81d25b7142d60395dea5e
[libcds.git] / cds / details / bounded_array.h
1 /*
2     This file is a part of libcds - Concurrent Data Structures library
3
4     (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2016
5
6     Source code repo: http://github.com/khizmax/libcds/
7     Download: http://sourceforge.net/projects/libcds/files/
8     
9     Redistribution and use in source and binary forms, with or without
10     modification, are permitted provided that the following conditions are met:
11
12     * Redistributions of source code must retain the above copyright notice, this
13       list of conditions and the following disclaimer.
14
15     * Redistributions in binary form must reproduce the above copyright notice,
16       this list of conditions and the following disclaimer in the documentation
17       and/or other materials provided with the distribution.
18
19     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23     FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24     DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25     SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26     CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27     OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28     OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.     
29 */
30
31 #ifndef CDSLIB_IMPL_BOUNDED_ARRAY_H
32 #define CDSLIB_IMPL_BOUNDED_ARRAY_H
33
34 /*
35     Dynamic non-growing array
36
37     Editions:
38         2008.03.08    Maxim.Khiszinsky    Created
39 */
40
41 #include <cds/details/allocator.h>
42 #include <vector>
43 #include <assert.h>
44
45 //@cond
46 namespace cds {
47     namespace details {
48         /// Bounded dynamic array
49         /**
50             The class template is intended for storing fixed-size sequences of objects.
51             Array capacity is constant and cannot be changed after creation of object of the class.
52             It is suitable for managing objects of non-copyable type \p T.
53
54             \par Template parameters
55                 - \p T type of elements
56                 - \p Allocator dynamic memory allocator class (<tt>std::allocator</tt> semantics)
57
58         */
59         template <typename T, class Allocator = CDS_DEFAULT_ALLOCATOR >
60         class bounded_array
61         {
62         public:
63             typedef T value_type ;    ///< value type stored in the array
64             typedef Allocator allocator_type ;  ///< allocator type
65
66             typedef value_type *          iterator          ; ///< item iterator
67             typedef value_type const *    const_iterator    ; ///< item const iterator
68
69         private:
70             typedef cds::details::Allocator< T, allocator_type> allocator_impl;
71
72             value_type *    m_arr;
73             const size_t    m_nCapacity;
74
75         public:
76             /// Default ctor
77             explicit bounded_array(
78                 size_t nCapacity            ///< capacity
79             )
80             : m_arr( allocator_impl().NewArray( nCapacity ) )
81             , m_nCapacity( nCapacity )
82             {}
83
84             ~bounded_array()
85             {
86                 allocator_impl().Delete( m_arr, capacity() );
87             }
88
89             const value_type& operator []( size_t nItem ) const
90             {
91                 assert( nItem < capacity() );
92                 return m_arr[nItem];
93             }
94
95             value_type& operator []( size_t nItem )
96             {
97                 assert( nItem < capacity() );
98                 return m_arr[nItem];
99             }
100
101             size_t   size() const CDS_NOEXCEPT
102             {
103                 return capacity();
104             }
105
106             size_t   capacity() const CDS_NOEXCEPT
107             {
108                 return m_nCapacity;
109             }
110
111             /// Returns pointer to the first item in the array
112             value_type * top() CDS_NOEXCEPT
113             {
114                 return m_arr;
115             }
116
117             /// Get begin iterator
118             const_iterator  begin() const CDS_NOEXCEPT
119             {
120                 return m_arr;
121             }
122             iterator        begin() CDS_NOEXCEPT
123             {
124                 return m_arr;
125             }
126
127             /// Get end iterator
128             const_iterator  end() const CDS_NOEXCEPT
129             {
130                 return begin() + capacity();
131             }
132             iterator        end() CDS_NOEXCEPT
133             {
134                 return begin() + capacity();
135             }
136         };
137
138     }    // namespace details
139 }    // namespace cds
140 //@endcond
141
142 #endif    // #ifndef CDSLIB_IMPL_BOUNDED_ARRAY_H