-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.cpp
More file actions
81 lines (64 loc) · 2.16 KB
/
server.cpp
File metadata and controls
81 lines (64 loc) · 2.16 KB
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
// This autogenerated skeleton file illustrates how to build a server.
// You should copy it to another filename to avoid overwriting it.
#include "HeapSort.h"
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/server/TSimpleServer.h>
#include <thrift/transport/TServerSocket.h>
#include <thrift/transport/TBufferTransports.h>
#define LeftChild(i) 2*i+1
using namespace ::apache::thrift;
using namespace ::apache::thrift::protocol;
using namespace ::apache::thrift::transport;
using namespace ::apache::thrift::server;
using boost::shared_ptr;
using namespace ::myrpc::thrift;
class HeapSortHandler : virtual public HeapSortIf {
public:
HeapSortHandler() {
// Your initialization goes here
}
void HeapSort(std::vector<int32_t> & _return, const std::vector<int32_t> & A, const int32_t N) {
_return = A;
//Build Heap
int i = N / 2;
int TMP = 0;
for( i = N / 2; i >= 0; i-- )
PercolateDown( _return, i, N );
for( i = N - 1; i >= 0; i-- )
{
TMP = _return.at(0);
_return.at(0) = _return.at(i);
_return.at(i) = TMP;
PercolateDown( _return, 0, i );
}
}
private:
void PercolateDown( std::vector<int32_t> & A, int i, int N ){
if( LeftChild( i ) >= N )
return;
int tmp = 0;
int child = LeftChild(i);
for( tmp = A.at(i); child < N; child = LeftChild(child) )
{
if( child != N - 1 && A.at(child) < A.at(child + 1) )
child++;
if( A.at(child) <= tmp )
break;
A.at(i) = A.at(child);
i = child;
}
if( A.at(i) > tmp )
A.at(i) = tmp;
}
};
int main(int argc, char **argv) {
int port = 9090;
shared_ptr<HeapSortHandler> handler(new HeapSortHandler());
shared_ptr<TProcessor> processor(new HeapSortProcessor(handler));
shared_ptr<TServerTransport> serverTransport(new TServerSocket(port));
shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
TSimpleServer server(processor, serverTransport, transportFactory, protocolFactory);
server.serve();
return 0;
}