summaryrefslogtreecommitdiff
path: root/boost/sort/common/deque_cnc.hpp
blob: eb3b31ee6ada6c2a3b8cd92ed8ae996c3a408a84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
//----------------------------------------------------------------------------
/// @file   deque_cnc.hpp
/// @brief  This file contains the implementation of the several types of
///         recursive fastmutex for read and write
///
/// @author Copyright (c) 2010 2015 Francisco José Tapia (fjtapia@gmail.com )\n
///         Distributed under the Boost Software License, Version 1.0.\n
///         ( See accompanyingfile LICENSE_1_0.txt or copy at
///           http://www.boost.org/LICENSE_1_0.txt  )
/// @version 0.1
///
/// @remarks
//-----------------------------------------------------------------------------
#ifndef __TOOLS_DEQUE_CNC_HPP
#define __TOOLS_DEQUE_CNC_HPP

#include <sort/tools/spinlock.hpp>
#include <vector>
#include <deque>

namespace sort
{
namespace tools
{

//###########################################################################
//                                                                         ##
//    ################################################################     ##
//    #                                                              #     ##
//    #                      C L A S S                               #     ##
//    #                   S T A C K _ C N C                          #     ##
//    #                                                              #     ##
//    ################################################################     ##
//                                                                         ##
//###########################################################################
//
//---------------------------------------------------------------------------
/// @class  deque_cnc
/// @brief This class is a concurrent stack controled by a spin_lock
/// @remarks
//---------------------------------------------------------------------------
template<typename T, typename Allocator = std::allocator<T> >
class deque_cnc
{
public:
    //-----------------------------------------------------------------------
    //                     D E F I N I T I O N S
    //-----------------------------------------------------------------------
    typedef std::deque<T, Allocator>                deque_t;
    typedef typename deque_t::size_type             size_type;
    typedef typename deque_t::difference_type       difference_type;
    typedef typename deque_t::value_type            value_type;
    typedef typename deque_t::pointer               pointer;
    typedef typename deque_t::const_pointer         const_pointer;
    typedef typename deque_t::reference             reference;
    typedef typename deque_t::const_reference       const_reference;
    typedef typename deque_t::allocator_type        allocator_type;

protected:
    //------------------------------------------------------------------------
    //                     VARIABLES
    //------------------------------------------------------------------------
    deque_t dq;
    mutable spinlock spl;

public:
    //
    //-----------------------------------------------------------------------
    //  C O N S T R U C T O R S     A N D    D E S T R U C T O R
    //-----------------------------------------------------------------------
    //
    //-----------------------------------------------------------------------
    //  function : deque_cnc
    /// @brief  constructor
    //----------------------------------------------------------------------
    explicit inline deque_cnc(void): dq() { };
//
    //----------------------------------------------------------------------
    //  function : deque_cnc
    /// @brief  constructor
    /// @param [in] ALLC : Allocator
    //----------------------------------------------------------------------
    explicit inline deque_cnc(const Allocator &ALLC): dq(ALLC){ };
    //
    //----------------------------------------------------------------------
    //  function : ~deque_cnc
    /// @brief  Destructor
    //----------------------------------------------------------------------
    virtual ~deque_cnc(void){ dq.clear(); };
    //
    //----------------------------------------------------------------------
    //  function : clear
    /// @brief Delete all the elements of the deque_cnc.
    //----------------------------------------------------------------------
    void clear(void)
    {
        std::lock_guard < spinlock > S(spl);
        dq.clear();
    };
    //
    //------------------------------------------------------------------------
    //  function : swap
    /// @brief swap the data between the two deque_cnc
    /// @param [in] A : deque_cnc to swap
    /// @return none
    //-----------------------------------------------------------------------
    void swap(deque_cnc & A) noexcept
    {
        if (this == &A) return;
        std::lock_guard < spinlock > S(spl);
        dq.swap(A.dq);
    };
    //
    //-----------------------------------------------------------------------
    //  S I Z E , M A X _ S I Z E , R E S I Z E
    //  C A P A C I T Y , E M P T Y , R E S E R V E
    //-----------------------------------------------------------------------
    //
    //------------------------------------------------------------------------
    //  function : size
    /// @brief return the number of elements in the deque_cnc
    /// @return number of elements in the deque_cnc
    //------------------------------------------------------------------------
    size_type size(void) const noexcept
    {
        std::lock_guard < spinlock > S(spl);
        return dq.size();
    };
    //
    //------------------------------------------------------------------------
    //  function :max_size
    /// @brief return the maximun size of the container
    /// @return maximun size of the container
    //------------------------------------------------------------------------
    size_type max_size(void) const noexcept
    {
        std::lock_guard < spinlock > S(spl);
        return (dq.max_size());
    };
    //
    //-------------------------------------------------------------------------
    //  function : shrink_to_fit
    /// @brief resize the current vector size and change to size.\n
    ///        If sz is smaller than the current size, delete elements to end\n
    ///        If sz is greater than the current size, insert elements to the
    ///        end with the value c
    /// @param [in] sz : new size of the deque_cnc after the resize
    /// @param [in] c : Value to insert if sz is greather than the current size
    /// @return none
    //------------------------------------------------------------------------
    void shrink_to_fit()
    {
        std::lock_guard < spinlock > S(spl);
        dq.shrink_to_fit();
    };
    //
    //------------------------------------------------------------------------
    //  function : empty
    /// @brief indicate if the map is empty
    /// @return true if the map is empty, false in any other case
    //------------------------------------------------------------------------
    bool empty(void) const noexcept
    {
        std::lock_guard < spinlock > S(spl);
        return (dq.empty());
    };
    //---------------------------------------------------------------------------
    //  function : push_back
    /// @brief Insert one element in the back of the container
    /// @param [in] D : value to insert. Can ve a value, a reference or an
    ///                 rvalue
    //---------------------------------------------------------------------------
    void push_back(const value_type & D)
    {
        std::lock_guard < spinlock > S(spl);
        dq.push_back(D);
    };

    //------------------------------------------------------------------------
    //  function : emplace_back
    /// @brief Insert one element in the back of the container
    /// @param [in] args :group of arguments for to build the object to insert
    //-------------------------------------------------------------------------
    template<class ... Args>
    void emplace_back(Args && ... args)
    {
        std::lock_guard < spinlock > S(spl);
        dq.emplace_back(std::forward <Args>(args) ...);
    };
    //------------------------------------------------------------------------
    //  function : push_back
    /// @brief Insert one element in the back of the container
    /// @param [in] D : deque to insert in the actual deque, inserting a copy
    ///                  of the elements
    /// @return reference to the deque after the insertion
    //------------------------------------------------------------------------
    template<class Allocator2>
    deque_cnc & push_back(const std::deque<value_type, Allocator2> & D)
    {
        std::lock_guard < spinlock > S(spl);
        for (size_type i = 0; i < D.size(); ++i)
            dq.push_back(D[i]);
        return *this;
    };
    //------------------------------------------------------------------------
    //  function : push_back
    /// @brief Insert one element in the back of the container
    /// @param [in] D : deque to insert in the actual deque, inserting a move
    ///                 of the elements
    /// @return reference to the deque after the insertion
    //------------------------------------------------------------------------
    deque_cnc & push_back(std::deque<value_type, Allocator> && D)
    {
        std::lock_guard < spinlock > S(spl);
        for (size_type i = 0; i < D.size(); ++i)
            dq.emplace_back(std::move(D[i]));
        return *this;
    };
    //
    //------------------------------------------------------------------------
    //  function :pop_back
    /// @brief erase the last element of the container
    //-----------------------------------------------------------------------
    void pop_back(void)
    {
        std::lock_guard < spinlock > S(spl);
        dq.pop_back();
    };
    //
    //------------------------------------------------------------------------
    //  function :pop_copy_back
    /// @brief erase the last element and return a copy over P
    /// @param [out] P : reference to a variable where copy the element
    /// @return code of the operation
    ///         true - Element erased
    ///         false - Empty tree
    //------------------------------------------------------------------------
    bool pop_copy_back(value_type & P)
    {   //-------------------------- begin -----------------------------
        std::lock_guard < spinlock > S(spl);
        if (dq.size() == 0) return false;
        P = dq.back();
        dq.pop_back();
        return true;
    };
    //
    //------------------------------------------------------------------------
    //  function :pop_move_back
    /// @brief erase the last element and move over P
    /// @param [out] P : reference to a variable where move the element
    /// @return code of the operation
    ///         true - Element erased
    ///         false - Empty tree
    //------------------------------------------------------------------------
    bool pop_move_back(value_type & P)
    {   //-------------------------- begin -----------------------------
        std::lock_guard < spinlock > S(spl);
        if (dq.size() == 0) return false;
        P = std::move(dq.back());
        dq.pop_back();
        return true;
    };

    //------------------------------------------------------------------------
    //  function : push_front
    /// @brief Insert one copy of the element in the front of the container
    /// @param [in] D : value to insert
    //------------------------------------------------------------------------
    void push_front(const value_type & D)
    {
        std::lock_guard < spinlock > S(spl);
        dq.push_front(D);
    };

    //------------------------------------------------------------------------
    //  function : emplace_front
    /// @brief Insert one element in the front of the container
    /// @param [in] args :group of arguments for to build the object to insert
    //-------------------------------------------------------------------------
    template<class ... Args>
    void emplace_front(Args && ... args)
    {
        std::lock_guard < spinlock > S(spl);
        dq.emplace_front(std::forward <Args>(args) ...);
    };
    //------------------------------------------------------------------------
    //  function : push_front
    /// @brief Insert a copy of the elements of the deque V1 in the front
    ///        of the container
    /// @param [in] V1 : deque with the elements to insert
    /// @return reference to the deque after the insertion
    //------------------------------------------------------------------------
    template<class Allocator2>
    deque_cnc & push_front(const std::deque<value_type, Allocator2> & V1)
    {
        std::lock_guard < spinlock > S(spl);
        for (size_type i = 0; i < V1.size(); ++i)
            dq.push_front(V1[i]);
        return *this;
    };
    //-----------------------------------------------------------------------
    //  function : push_front
    /// @brief Insert a move of the elements of the deque V1 in the front
    ///        of the container
    /// @param [in] V1 : deque with the elements to insert
    /// @return reference to the deque after the insertion
    //-----------------------------------------------------------------------
    deque_cnc & push_front(std::deque<value_type, Allocator> && V1)
    {
        std::lock_guard < spinlock > S(spl);
        for (size_type i = 0; i < V1.size(); ++i)
            dq.emplace_front(std::move(V1[i]));
        return *this;
    };
    //
    //-----------------------------------------------------------------------
    //  function :pop_front
    /// @brief erase the first element of the container
    //-----------------------------------------------------------------------
    void pop_front(void)
    {
        std::lock_guard < spinlock > S(spl);
        dq.pop_front();
    };
    //
    //-----------------------------------------------------------------------
    //  function :pop_copy_front
    /// @brief erase the first element of the tree and return a copy over P
    /// @param [out] P : reference to a variable where copy the element
    /// @return code of the operation
    ///         true- Element erased
    ///         false - Empty tree
    //-----------------------------------------------------------------------
    bool pop_copy_front(value_type & P)
    {   //-------------------------- begin -----------------------------
        std::lock_guard < spinlock > S(spl);
        if (dq.size() == 0) return false;
        P = dq.front();
        dq.pop_front();
        return true;
    };
    //
    //------------------------------------------------------------------------
    //  function :pop_move_front
    /// @brief erase the first element of the tree and return a move over P
    /// @param [out] P : reference to a variable where move the element
    /// @return code of the operation
    ///         true- Element erased
    ///         false - Empty tree
    //------------------------------------------------------------------------
    bool pop_move_front(value_type & P)
    {   //-------------------------- begin -----------------------------
        std::lock_guard < spinlock > S(spl);
        if (dq.size() == 0) return false;
        P = std::move(dq.front());
        dq.pop_front();
        return true;
    };
};
// end class deque_cnc

//***************************************************************************
};// end namespace tools
};// end namespace sort
//***************************************************************************
#endif