2 This file is a part of libcds - Concurrent Data Structures library
4 (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2016
6 Source code repo: http://github.com/khizmax/libcds/
7 Download: http://sourceforge.net/projects/libcds/files/
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions are met:
12 * Redistributions of source code must retain the above copyright notice, this
13 list of conditions and the following disclaimer.
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.
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.
31 #ifndef CDSUNIT_NONCONCURRENT_ITERATOR_SEQUENCE_H
32 #define CDSUNIT_NONCONCURRENT_ITERATOR_SEQUENCE_H
34 #include <cds/details/bounded_array.h>
35 #include <cds/atomic.h>
36 #include <algorithm> // std::sort
38 namespace map { namespace nonconcurrent_iterator {
45 TKey keyControl ; // èñïîëüçóåòñÿ äëÿ êîíòðîëÿ, ÷òî äàííûå îòíîñÿòñÿ ê êëþ÷ó
46 int nShuffle ; // ñëó÷àéíîå çíà÷åíèå, èñïîëüçóåìîå äëÿ ñîðòèðîâêè
47 TValue * pOrigItem ; // óêàçàòåëü íà ýëåìåíò â ìàññèâå; òàê êàê ñïèñîê õðàíèò êîïèè,
48 // òî ñ ïîìîùüþ ýòîãî ïîëÿ îðãàíèçóåòñÿ äîñòóï ê èñõîäíîìó ýëåìåíòó
50 cds::atomics::item_counter<cds::membar_release> nAccess ; // ñ÷åò÷èê äîñòóïà ïðè îáõîäå ñïèñêà ïî èòåðàòîðàì
51 // (Atomic, òàê êàê âîçìîæåí ïàðàëëåüíûé äîñòóï)
57 TValue( const TValue& v )
59 memcpy( this, &v, sizeof(*this) );
74 static size_t nConstructCount;
75 static size_t nDestructCount;
78 typedef cds::details::BoundedArray<Data> TDataArray;
79 typedef TDataArray::const_iterator const_iterator;
83 const_iterator begin() const { return arrData.begin(); }
84 const_iterator end() const { return arrData.end(); }
87 Sequence( size_t nSize )
91 static unsigned int Rand( unsigned int nMax )
93 double rnd = double( rand() ) / double( RAND_MAX );
94 unsigned int n = (unsigned int) (rnd * nMax);
95 return n < nMax ? n : (n-1);
98 void generateSequence()
100 // Ãåíåðèðóåì òåñòîâûé ìàññèâ äàííûõ. Ìàññèâ äîëæåí áûòü ïåðåìåøàí ñëó÷àéíûì îáðàçîì, ïîýòîìó
101 // â êà÷åñòâå çíà÷åíèÿ ïîëÿ value.nShuffle èñïîëüçóåì ñëó÷àéíîå, è ñîðòèðóåì ìàññèâ
102 // ïî ýòîìó ñëó÷àéíîìó çíà÷åíèþ
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 );
116 size_t nMax = arrData.capacity();
117 for ( size_t i = 0; i < nMax; ++i )
118 arrData[i].value.pOrigItem = &(arrData[i].value);
121 static bool sortValue( const Data& p1, const Data&p2 )
123 return p1.value.nShuffle < p2.value.nShuffle;
126 void makeRandomSortedSequence()
128 std::sort( arrData.begin(), arrData.end(), sortValue );
132 static bool sortAsc( const Data& p1, const Data&p2 )
134 return p1.key < p2.key;
137 void makeAscSortedSequence()
139 // Ñîðòèðóåò ìàññèâ â ïîðÿäêå âîçðàñòàíèÿ êëþ÷åé
140 std::sort( arrData.begin(), arrData.end(), sortAsc );
144 static bool sortDesc( const Data& p1, const Data&p2 )
146 return p2.key < p1.key;
149 void makeDescSortedSequence()
151 // Ñîðòèðóåò ìàññèâ â ïîðÿäêå óáûâàíèÿ êëþ÷åé
152 std::sort( arrData.begin(), arrData.end(), sortDesc );
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 );
164 } } // namespace map::nonconcurrent_iterator
166 #endif // #ifndef CDSUNIT_NONCONCURRENT_ITERATOR_SEQUENCE_H