1*1c78a5d6SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*1c78a5d6SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*1c78a5d6SAndrew Rist * or more contributor license agreements. See the NOTICE file
5*1c78a5d6SAndrew Rist * distributed with this work for additional information
6*1c78a5d6SAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*1c78a5d6SAndrew Rist * to you under the Apache License, Version 2.0 (the
8*1c78a5d6SAndrew Rist * "License"); you may not use this file except in compliance
9*1c78a5d6SAndrew Rist * with the License. You may obtain a copy of the License at
10cdf0e10cSrcweir *
11*1c78a5d6SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir *
13*1c78a5d6SAndrew Rist * Unless required by applicable law or agreed to in writing,
14*1c78a5d6SAndrew Rist * software distributed under the License is distributed on an
15*1c78a5d6SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*1c78a5d6SAndrew Rist * KIND, either express or implied. See the License for the
17*1c78a5d6SAndrew Rist * specific language governing permissions and limitations
18*1c78a5d6SAndrew Rist * under the License.
19cdf0e10cSrcweir *
20*1c78a5d6SAndrew Rist *************************************************************/
21*1c78a5d6SAndrew Rist
22*1c78a5d6SAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir #ifndef ARY_STORE_S_BASE_HXX
25cdf0e10cSrcweir #define ARY_STORE_S_BASE_HXX
26cdf0e10cSrcweir
27cdf0e10cSrcweir // USED SERVICES
28cdf0e10cSrcweir #include <deque>
29cdf0e10cSrcweir #include <cosv/tpl/tpltools.hxx>
30cdf0e10cSrcweir
31cdf0e10cSrcweir
32cdf0e10cSrcweir
33cdf0e10cSrcweir
34cdf0e10cSrcweir namespace ary
35cdf0e10cSrcweir {
36cdf0e10cSrcweir namespace stg
37cdf0e10cSrcweir {
38cdf0e10cSrcweir
39cdf0e10cSrcweir
40cdf0e10cSrcweir /** The basic storage container of the repository.
41cdf0e10cSrcweir
42cdf0e10cSrcweir @collab Storage
43cdf0e10cSrcweir Implements Storage. Not used elsewhere.
44cdf0e10cSrcweir
45cdf0e10cSrcweir @tpl ENTITY
46cdf0e10cSrcweir The type of *it, where it is of type c_iter, has to be ENTITY * const.
47cdf0e10cSrcweir */
48cdf0e10cSrcweir template <class ENTITY>
49cdf0e10cSrcweir class Base
50cdf0e10cSrcweir {
51cdf0e10cSrcweir public:
52cdf0e10cSrcweir // LIFECYCLE
53cdf0e10cSrcweir typedef std::deque< ENTITY* > impl_type;
54cdf0e10cSrcweir typedef typename impl_type::const_iterator c_iter;
55cdf0e10cSrcweir
56cdf0e10cSrcweir
57cdf0e10cSrcweir /** @param i_nrOfReservedItems
58cdf0e10cSrcweir The number of actual items to reserve, including the item
59cdf0e10cSrcweir at index [0] that is always empty and unused.
60cdf0e10cSrcweir */
61cdf0e10cSrcweir Base(
62cdf0e10cSrcweir uintt i_nrOfReservedItems );
63cdf0e10cSrcweir ~Base();
64cdf0e10cSrcweir
65cdf0e10cSrcweir // OPERATORS
66cdf0e10cSrcweir ENTITY * operator[](
67cdf0e10cSrcweir uintt i_index ) const;
68cdf0e10cSrcweir // OPERATIONS
69cdf0e10cSrcweir uintt Add_Entity( /// @return the index of the new element.
70cdf0e10cSrcweir DYN ENTITY & pass_newEntity );
71cdf0e10cSrcweir DYN ENTITY * Set_Entity( /// @return the previous value.
72cdf0e10cSrcweir uintt i_index,
73cdf0e10cSrcweir DYN ENTITY & pass_newEntity );
74cdf0e10cSrcweir // INQUIRY
75cdf0e10cSrcweir uintt Size() const; /// Incl. reserved size.
76cdf0e10cSrcweir uintt ReservedSize() const; /// Incl. zero for element at [0].
77cdf0e10cSrcweir
78cdf0e10cSrcweir c_iter Begin() const; /// @return location of index 1, because 0 is always empty.
79cdf0e10cSrcweir c_iter BeginUnreserved() const;
80cdf0e10cSrcweir c_iter End() const;
81cdf0e10cSrcweir
82cdf0e10cSrcweir private:
83cdf0e10cSrcweir // DATA
84cdf0e10cSrcweir impl_type aData;
85cdf0e10cSrcweir uintt nReservedSize;
86cdf0e10cSrcweir };
87cdf0e10cSrcweir
88cdf0e10cSrcweir
89cdf0e10cSrcweir
90cdf0e10cSrcweir // IMPLEMENTATION
91cdf0e10cSrcweir
92cdf0e10cSrcweir template <class ENTITY>
Base(uintt i_nrOfReservedItems)93cdf0e10cSrcweir Base<ENTITY>::Base(uintt i_nrOfReservedItems)
94cdf0e10cSrcweir : aData(i_nrOfReservedItems, 0),
95cdf0e10cSrcweir nReservedSize(i_nrOfReservedItems)
96cdf0e10cSrcweir {
97cdf0e10cSrcweir }
98cdf0e10cSrcweir
99cdf0e10cSrcweir template <class ENTITY>
~Base()100cdf0e10cSrcweir Base<ENTITY>::~Base()
101cdf0e10cSrcweir {
102cdf0e10cSrcweir csv::erase_container_of_heap_ptrs(aData);
103cdf0e10cSrcweir }
104cdf0e10cSrcweir
105cdf0e10cSrcweir
106cdf0e10cSrcweir template <class ENTITY>
107cdf0e10cSrcweir ENTITY *
operator [](uintt i_index) const108cdf0e10cSrcweir Base<ENTITY>::operator[](uintt i_index) const
109cdf0e10cSrcweir {
110cdf0e10cSrcweir if (i_index < aData.size())
111cdf0e10cSrcweir return aData[i_index];
112cdf0e10cSrcweir return 0;
113cdf0e10cSrcweir }
114cdf0e10cSrcweir
115cdf0e10cSrcweir template <class ENTITY>
116cdf0e10cSrcweir uintt
Add_Entity(DYN ENTITY & pass_newEntity)117cdf0e10cSrcweir Base<ENTITY>::Add_Entity(DYN ENTITY & pass_newEntity)
118cdf0e10cSrcweir {
119cdf0e10cSrcweir aData.push_back(&pass_newEntity);
120cdf0e10cSrcweir return aData.size() - 1;
121cdf0e10cSrcweir }
122cdf0e10cSrcweir
123cdf0e10cSrcweir template <class ENTITY>
124cdf0e10cSrcweir DYN ENTITY *
Set_Entity(uintt i_index,DYN ENTITY & pass_newEntity)125cdf0e10cSrcweir Base<ENTITY>::Set_Entity( uintt i_index,
126cdf0e10cSrcweir DYN ENTITY & pass_newEntity )
127cdf0e10cSrcweir {
128cdf0e10cSrcweir csv_assert(i_index != 0 AND i_index < aData.size());
129cdf0e10cSrcweir
130cdf0e10cSrcweir Dyn<ENTITY>
131cdf0e10cSrcweir ret(aData[i_index]);
132cdf0e10cSrcweir aData[i_index] = &pass_newEntity;
133cdf0e10cSrcweir return ret.Release();
134cdf0e10cSrcweir }
135cdf0e10cSrcweir
136cdf0e10cSrcweir template <class ENTITY>
137cdf0e10cSrcweir uintt
Size() const138cdf0e10cSrcweir Base<ENTITY>::Size() const
139cdf0e10cSrcweir {
140cdf0e10cSrcweir return aData.size();
141cdf0e10cSrcweir }
142cdf0e10cSrcweir
143cdf0e10cSrcweir template <class ENTITY>
144cdf0e10cSrcweir uintt
ReservedSize() const145cdf0e10cSrcweir Base<ENTITY>::ReservedSize() const
146cdf0e10cSrcweir {
147cdf0e10cSrcweir return nReservedSize;
148cdf0e10cSrcweir }
149cdf0e10cSrcweir
150cdf0e10cSrcweir template <class ENTITY>
151cdf0e10cSrcweir typename Base<ENTITY>::c_iter
Begin() const152cdf0e10cSrcweir Base<ENTITY>::Begin() const
153cdf0e10cSrcweir {
154cdf0e10cSrcweir return aData.begin() + 1;
155cdf0e10cSrcweir }
156cdf0e10cSrcweir
157cdf0e10cSrcweir template <class ENTITY>
158cdf0e10cSrcweir typename Base<ENTITY>::c_iter
BeginUnreserved() const159cdf0e10cSrcweir Base<ENTITY>::BeginUnreserved() const
160cdf0e10cSrcweir {
161cdf0e10cSrcweir return aData.begin() + nReservedSize;
162cdf0e10cSrcweir }
163cdf0e10cSrcweir
164cdf0e10cSrcweir template <class ENTITY>
165cdf0e10cSrcweir typename Base<ENTITY>::c_iter
End() const166cdf0e10cSrcweir Base<ENTITY>::End() const
167cdf0e10cSrcweir {
168cdf0e10cSrcweir return aData.end();
169cdf0e10cSrcweir }
170cdf0e10cSrcweir
171cdf0e10cSrcweir
172cdf0e10cSrcweir
173cdf0e10cSrcweir
174cdf0e10cSrcweir } // namespace stg
175cdf0e10cSrcweir } // namespace ary
176cdf0e10cSrcweir #endif
177