-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlist.cpp
More file actions
139 lines (131 loc) · 2.82 KB
/
list.cpp
File metadata and controls
139 lines (131 loc) · 2.82 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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include "list.h"
list::list(int lnodesz,uint64_t rowsz)
{
this->first=NULL;
this->last=NULL;
this->lnodesz=lnodesz;
this->rowsz=rowsz;
this->rows=0;
this->tmpcntr=0;
}
list::~list()
{
listnode* t=this->first;
while(this->first!=NULL)
{
this->first=t->next;
delete t;
t=this->first;
}
}
bool list::insert(uint64_t num)
{
if(this->last==NULL)
this->first=this->last=new listnode(this->lnodesz);
if(sizeof(num)+this->last->size>this->lnodesz)
this->last=this->last->next=new listnode(this->lnodesz);
memcpy(this->last->content+this->last->size,&num,sizeof(num));
tmpcntr++;
if(tmpcntr==rowsz)
{
tmpcntr=0;
rows++;
}
this->last->size+=sizeof(num);
return true;
}
void list::print()
{
if(this->first==NULL)
{
std::cout<<"No joined pairs"<<std::endl;
return;
}
listnode* t=this->first;
int cntr=0;
while(t!=NULL)
{
uint64_t n;
for(int i=0;i<t->size;i+=sizeof(uint64_t))
{
memcpy(&n,t->content+i,sizeof(uint64_t));
std::cout<<n<<" ";
cntr++;
if(cntr==this->rowsz)
{
std::cout<<std::endl;
cntr=0;
}
}
t=t->next;
}
}
uint64_t** list::lsttoarr()
{
if(this->first==NULL)
return NULL;
uint64_t** arr;
arr=new uint64_t*[rowsz];
for(int i=0;i<rowsz;i++)
arr[i]=new uint64_t[rows];
listnode* t=this->first;
int cntr=0;
uint64_t row=0;
while(t!=NULL)
{
uint64_t n;
for(int i=0;i<t->size;i+=sizeof(uint64_t))
{
memcpy(&n,t->content+i,sizeof(uint64_t));
arr[cntr][row]=n;
cntr++;
if(cntr==this->rowsz)
{
cntr=0;
row++;
}
}
t=t->next;
}
return arr;
}
bool list::insert(char ch)
{
if(this->last==NULL)
{
this->first=this->last=new listnode(this->lnodesz);
this->tmpcntr++;
}
if(sizeof(ch)+this->last->size>this->lnodesz)
{
this->last=this->last->next=new listnode(this->lnodesz);
this->tmpcntr++;
}
memcpy(this->last->content+this->last->size,&ch,sizeof(ch));
this->last->size+=sizeof(ch);
return true;
}
char* list::lsttocharr()
{
if(this->first==NULL)
return NULL;
char* arr;
arr=new char[tmpcntr*lnodesz +1];
listnode* t=this->first;
for(uint64_t i=0;i<tmpcntr;i++,t=t->next)
{
memcpy(arr+i*lnodesz,t->content,t->size);
arr[i*lnodesz+t->size]='\0';
}
return arr;
}
listnode::listnode(int sz)
{
this->next=NULL;
this->content=new char[sz];
this->size=0;
}
listnode::~listnode()
{
delete[] this->content;
}