File: merge_arrays.cpp

package info (click to toggle)
boost1.90 1.90.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 593,156 kB
  • sloc: cpp: 4,190,642; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,776; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (105 lines) | stat: -rw-r--r-- 2,353 bytes parent folder | download | duplicates (12)
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

//          Copyright Keld Helsgaun 2000, Oliver Kowalke 2014.
// Distributed under the Boost Software License, Version 1.0.
//    (See accompanying file LICENSE_1_0.txt or copy at
//          http://www.boost.org/LICENSE_1_0.txt)

#include <boost/coroutine/all.hpp>

#include <cstdlib>
#include <cstddef>
#include <iostream>
#include <vector>

#include <boost/bind.hpp>
#include <boost/foreach.hpp>

typedef boost::coroutines::symmetric_coroutine< void >  coro_t;

class merger
{
private:
    std::size_t                 max_;
    std::vector< int >      &   to_;

    void run_( coro_t::yield_type & yield)
    {
        while ( idx < from.size() )
        {
            if ( other->from[other->idx] < from[idx])
                yield( other->coro);
            to_.push_back(from[idx++]);
        }
        while ( to_.size() < max_)
            to_.push_back( other->from[other->idx++]); 
    }

    merger( merger const&);
    merger & operator=( merger const&);

public:
    std::vector< int >  const&  from;
    std::size_t                 idx;
    merger                  *   other;
    coro_t::call_type           coro;

    merger( std::vector< int > const& from_, std::vector< int > & to, std::size_t max) :
        max_( max),
        to_( to),
        from( from_),
        idx( 0),
        other( 0),
        coro( boost::bind( & merger::run_, this, _1) )
    {}

    void run()
    { coro(); }
};

std::vector< int > merge( std::vector< int > const& a, std::vector< int > const& b)
{
    std::vector< int > c;
    merger ma( a, c, a.size() + b. size() );
    merger mb( b, c, a.size() + b. size() );

    ma.other = & mb;
    mb.other = & ma;

    ma.run();

    return c;
}

void print( std::string const& name, std::vector< int > const& v)
{
    std::cout << name << " : ";
    BOOST_FOREACH( int itm, v)
    { std::cout << itm << " "; }
    std::cout << "\n";
}

int main( int argc, char * argv[])
{
    std::vector< int > a;
    a.push_back( 1);
    a.push_back( 5);
    a.push_back( 6);
    a.push_back( 10);
    print( "a", a);

    std::vector< int > b;
    b.push_back( 2);
    b.push_back( 4);
    b.push_back( 7);
    b.push_back( 8);
    b.push_back( 9);
    b.push_back( 13);
    print( "b", b);

    std::vector< int > c = merge( a, b);
    print( "c", c);

    std::cout << "Done" << std::endl;

    return EXIT_SUCCESS;
}