Removed old map tests
[libcds.git] / tests / unit / nonconcurrent_iterator_sequence.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 CDSUNIT_NONCONCURRENT_ITERATOR_SEQUENCE_H
32 #define CDSUNIT_NONCONCURRENT_ITERATOR_SEQUENCE_H
33
34 #include <cds/details/bounded_array.h>
35 #include <cds/atomic.h>
36 #include <algorithm>    // std::sort
37
38 namespace map { namespace nonconcurrent_iterator {
39
40     class Sequence
41     {
42     public:
43         typedef int        TKey;
44         struct TValue {
45             TKey        keyControl    ;    // èñïîëüçóåòñÿ äëÿ êîíòðîëÿ, ÷òî äàííûå îòíîñÿòñÿ ê êëþ÷ó
46             int            nShuffle    ;    // ñëó÷àéíîå çíà÷åíèå, èñïîëüçóåìîå äëÿ ñîðòèðîâêè
47             TValue *    pOrigItem    ;    // óêàçàòåëü íà ýëåìåíò â ìàññèâå; òàê êàê ñïèñîê õðàíèò êîïèè,
48                                         // òî ñ ïîìîùüþ ýòîãî ïîëÿ îðãàíèçóåòñÿ äîñòóï ê èñõîäíîìó ýëåìåíòó
49
50             cds::atomics::item_counter<cds::membar_release>   nAccess        ;    // ñ÷åò÷èê äîñòóïà ïðè îáõîäå ñïèñêà ïî èòåðàòîðàì
51                                                 // (Atomic, òàê êàê âîçìîæåí ïàðàëëåüíûé äîñòóï)
52
53             TValue()
54             {
55                 ++nConstructCount;
56             }
57             TValue( const TValue& v )
58             {
59                 memcpy( this, &v, sizeof(*this) );
60                 ++nConstructCount;
61             }
62             ~TValue()
63             {
64                 ++nDestructCount;
65             }
66         };
67
68         struct Data {
69             TKey    key;
70             TValue    value;
71         };
72
73     protected:
74         static size_t nConstructCount;
75         static size_t nDestructCount;
76
77     public:
78         typedef cds::details::BoundedArray<Data>        TDataArray;
79         typedef TDataArray::const_iterator              const_iterator;
80
81         TDataArray                                        arrData;
82
83         const_iterator begin() const    { return arrData.begin(); }
84         const_iterator end() const      { return arrData.end(); }
85
86     public:
87         Sequence( size_t nSize )
88             : arrData( nSize )
89         {}
90
91         static unsigned int Rand( unsigned int nMax )
92         {
93             double rnd = double( rand() ) / double( RAND_MAX );
94             unsigned int n = (unsigned int) (rnd * nMax);
95             return n < nMax ? n : (n-1);
96         }
97
98         void generateSequence()
99         {
100             // Ãåíåðèðóåì òåñòîâûé ìàññèâ äàííûõ. Ìàññèâ äîëæåí áûòü ïåðåìåøàí ñëó÷àéíûì îáðàçîì, ïîýòîìó
101             // â êà÷åñòâå çíà÷åíèÿ ïîëÿ value.nShuffle èñïîëüçóåì ñëó÷àéíîå, è ñîðòèðóåì ìàññèâ
102             // ïî ýòîìó ñëó÷àéíîìó çíà÷åíèþ
103
104             size_t nMax = arrData.capacity();
105             for ( size_t i = 0; i < nMax; ++i ) {
106                 arrData[i].key = (unsigned int) i;
107                 arrData[i].value.keyControl = (unsigned int) i;
108                 arrData[i].value.nShuffle = Rand( (unsigned int) nMax );
109                 arrData[i].value.pOrigItem = &(arrData[i].value);
110                 arrData[i].value.nAccess.reset( cds::membar_relaxed::order );
111             }
112         }
113
114         void restoreLinks()
115         {
116             size_t nMax = arrData.capacity();
117             for ( size_t i = 0; i < nMax; ++i )
118                 arrData[i].value.pOrigItem = &(arrData[i].value);
119         }
120
121         static bool sortValue( const Data& p1, const Data&p2 )
122         {
123             return p1.value.nShuffle < p2.value.nShuffle;
124         }
125
126         void makeRandomSortedSequence()
127         {
128             std::sort( arrData.begin(), arrData.end(), sortValue );
129             restoreLinks();
130         }
131
132         static bool sortAsc( const Data& p1, const Data&p2 )
133         {
134             return p1.key < p2.key;
135         }
136
137         void makeAscSortedSequence()
138         {
139             // Ñîðòèðóåò ìàññèâ â ïîðÿäêå âîçðàñòàíèÿ êëþ÷åé
140             std::sort( arrData.begin(), arrData.end(), sortAsc );
141             restoreLinks();
142         }
143
144         static bool sortDesc( const Data& p1, const Data&p2 )
145         {
146             return p2.key < p1.key;
147         }
148
149         void makeDescSortedSequence()
150         {
151             // Ñîðòèðóåò ìàññèâ â ïîðÿäêå óáûâàíèÿ êëþ÷åé
152             std::sort( arrData.begin(), arrData.end(), sortDesc );
153             restoreLinks();
154         }
155
156         void clearAccess()
157         {
158             size_t nMax = arrData.capacity();
159             for ( size_t i = 0; i < nMax; ++i )
160                 arrData[i].value.nAccess.reset( cds::membar_relaxed::order );
161         }
162     };
163
164 } } // namespace map::nonconcurrent_iterator
165
166 #endif    // #ifndef CDSUNIT_NONCONCURRENT_ITERATOR_SEQUENCE_H