-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPartition.cpp
More file actions
72 lines (62 loc) · 1.8 KB
/
Partition.cpp
File metadata and controls
72 lines (62 loc) · 1.8 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
#ifndef PARTITION_CPP_
#define PARTITION_CPP_
#include "Partition.h"
#include "QuickSort.cpp"
template <class T, typename S>
bool Partition<T,S>::resize ()
{
if ( current == partition_size)
{
partition_size += increment;
data = (S *) realloc (data,partition_size*sizeof(S));
dataWrapper = (T **) realloc (dataWrapper,partition_size*sizeof(T *));
memset (((char*) data)+(current*sizeof(S)),0,increment*sizeof(S));
memset (((char*) dataWrapper)+(current*sizeof(T*)),0,increment*sizeof(T*));
return true;
}
return false;
}
template <class T, typename S>
Partition<T,S>::Partition (FILE * f,uint64_t p_partition_size,uint64_t p_increment)
{
partition_size = p_partition_size;
increment = p_increment;
current = 0;
output = f;
data = (S *) calloc (partition_size,sizeof(S));
dataWrapper = (T **) calloc (partition_size,sizeof(T *));
}
template <class T, typename S>
void Partition<T,S>::addItem(S * s)
{
//Do your implementation here
mtx.lock();
resize();
data[current] = *s;
//newly added
dataWrapper[current] = new T (&data[current]);
current++;
mtx.unlock();
}
template <class T, typename S>
void Partition<T,S>::sort()
{
for ( uint64_t i = 0 ; i < partition_size ; i ++)
dataWrapper[i] = new T (&data[i]);
cout<<"partition size is: "<<(unsigned)current<<endl;
QuickSort <T> quickSort2 (dataWrapper);
quickSort2.quickSort(0, current-1);
}
template <class T, typename S>
void Partition<T,S>::dump()
{
fwrite(data,sizeof(S),current,output);
}
template <class T, typename S>
Partition<T,S>::~Partition ()
{
if ( data != NULL ) free(data);
for ( uint64_t i = 0 ; i < partition_size ; i ++) delete (dataWrapper[i]);
free(dataWrapper);
}
#endif