1 /*
2   Copyright (C) 2014 Intel Corporation
3   All rights reserved.
4 
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions
7   are met:
8 
9   * Redistributions of source code must retain the above copyright
10     notice, this list of conditions and the following disclaimer.
11   * Redistributions in binary form must reproduce the above copyright
12     notice, this list of conditions and the following disclaimer in
13     the documentation and/or other materials provided with the
14     distribution.
15   * Neither the name of Intel Corporation nor the names of its
16     contributors may be used to endorse or promote products derived
17     from this software without specific prior written permission.
18 
19   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23   HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25   BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26   OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27   AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28   LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
29   WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   POSSIBILITY OF SUCH DAMAGE.
31 */
32 #include <utility>
33 #include <iterator>
34 
35 namespace pss {
36 
37 namespace internal {
38 
39 //! Destroy sequence [xs,xe)
40 template<class RandomAccessIterator>
serial_destroy(RandomAccessIterator zs,RandomAccessIterator ze)41 void serial_destroy( RandomAccessIterator zs, RandomAccessIterator ze ) {
42     typedef typename std::iterator_traits<RandomAccessIterator>::value_type T;
43     while( zs!=ze ) {
44         --ze;
45         (*ze).~T();
46     }
47 }
48 
49 //! Merge sequences [xs,xe) and [ys,ye) to output sequence [zs,(xe-xs)+(ye-ys)), using std::move
50 template<class RandomAccessIterator1, class RandomAccessIterator2, class RandomAccessIterator3, class Compare>
serial_move_merge(RandomAccessIterator1 xs,RandomAccessIterator1 xe,RandomAccessIterator2 ys,RandomAccessIterator2 ye,RandomAccessIterator3 zs,Compare comp)51 void serial_move_merge( RandomAccessIterator1 xs, RandomAccessIterator1 xe, RandomAccessIterator2 ys, RandomAccessIterator2 ye, RandomAccessIterator3 zs, Compare comp ) {
52     if( xs!=xe ) {
53         if( ys!=ye ) {
54             for(;;) {
55                 if( comp(*ys,*xs) ) {
56                     *zs = std::move(*ys);
57                     ++zs;
58                     if( ++ys==ye ) { break; }
59                 } else {
60                     *zs = std::move(*xs);
61                     ++zs;
62                     if( ++xs==xe ) { goto movey; }
63                 }
64             }
65         }
66         ys = xs;
67         ye = xe;
68     }
69 movey:
70     std::move( ys, ye, zs );
71 }
72 
73 template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename Compare>
stable_sort_base_case(RandomAccessIterator1 xs,RandomAccessIterator1 xe,RandomAccessIterator2 zs,int inplace,Compare comp)74 void stable_sort_base_case(  RandomAccessIterator1 xs, RandomAccessIterator1 xe, RandomAccessIterator2 zs, int inplace, Compare comp) {
75     std::stable_sort( xs, xe, comp );
76     if( inplace!=2 ) {
77         RandomAccessIterator2 ze = zs + (xe-xs);
78         typedef typename std::iterator_traits<RandomAccessIterator2>::value_type T;
79         if( inplace )
80             // Initialize the temporary buffer
81             for( ; zs<ze; ++zs )
82                 new(&*zs) T;
83         else
84             // Initialize the temporary buffer and move keys to it.
85             for( ; zs<ze; ++xs, ++zs )
86                 new(&*zs) T(std::move(*xs));
87     }
88 }
89 
90 //! Raw memory buffer with automatic cleanup.
91 class raw_buffer {
92     void* ptr;
93 public:
94     //! Try to obtain buffer of given size.
raw_buffer(size_t bytes)95     raw_buffer( size_t bytes ) : ptr( operator new(bytes,std::nothrow) ) {}
96     //! True if buffer was successfully obtained, zero otherwise.
97     operator bool() const {return ptr;}
98     //! Return pointer to buffer, or  NULL if buffer could not be obtained.
get()99     void* get() const {return ptr;}
100     //! Destroy buffer
~raw_buffer()101     ~raw_buffer() {operator delete(ptr);}
102 };
103 
104 } // namespace internal
105 
106 } // namespace pss
107