This repository has been archived by the owner on Nov 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 76
/
list.hpp
163 lines (162 loc) · 4.22 KB
/
list.hpp
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#ifndef __PEREGRINE__LIST__
#define __PEREGRINE__LIST__
//TODO: Use peregrine exception instead of c++ throw
#include <cstddef>
#include <cstdint>
#include <iostream>
namespace Peregrine{
template<typename T>
class list{
T* m_data;
size_t m_size=0;
size_t m_capacity=0;
size_t m_iter_index=0;
public:
list(){
m_data=nullptr;
}
list(size_t size){
m_data=new T[size];
this->m_capacity=size;
}
list(const list<T>& other){
m_data=new T[other.m_size];
this->m_size=other.m_size;
this->m_capacity=other.m_capacity;
for(size_t i=0;i<m_size;i++){
m_data[i]=other.m_data[i];
}
}
list(list<T>&& other){
m_data=other.m_data;
other.m_data=nullptr;
this->m_size=other.m_size;
this->m_capacity=other.m_capacity;
}
~list(){
if(m_data!=nullptr){
delete [] m_data;
m_data=nullptr;
}
}
list<T>& operator=(const list<T>& other){
if(this!=&other){
if(m_data!=nullptr){
delete[] m_data;
m_data=nullptr;
}
m_data=new T[other.m_size];
this->m_size=other.m_size;
this->m_capacity=other.m_capacity;
for(size_t i=0;i<m_size;i++){
m_data[i]=other.m_data[i];
}
}
return *this;
}
list<T>& operator=(list<T>&& other){
if(this!=&other){
if(m_data!=nullptr){
delete[] m_data;
m_data=nullptr;
}
m_data=new T[other.m_size];
this->m_size=other.m_size;
this->m_capacity=other.m_capacity;
for(size_t i=0;i<m_size;i++){
m_data[i]=other.m_data[i];
}
if(m_data!=nullptr){
delete[] other.m_data;
other.m_data=nullptr;
}
}
return *this;
}
T& ____mem____P____P______getitem__(int64_t index){
if(index<0){
index+=(int64_t)m_size;
}
if(index<0||index>=m_capacity){
throw std::out_of_range("index out of range");
}
if(index>=m_size){
m_size=index+1;
}
return m_data[index];
}
const T& ____mem____P____P______getitem__(int64_t index)const{
if(index<0){
index+=(int64_t)m_size;
}
if(index<0||index>=m_capacity){
throw std::out_of_range("index out of range");
}
return m_data[index];
}
//TODO: list[1:5]
size_t ____mem____P____P______len__()const{
return m_size;
}
size_t ____mem____P____P______iter__(){
m_iter_index=0;//reseting it to 0.Dont remove this line
return m_size;
}
T ____mem____P____P______iterate__(){
m_iter_index++;
return m_data[m_iter_index-1];
}
list<T>& ____mem____P____P______enter__(){
return *this;
}
void ____mem____P____P______end__(){}
//TODO: __reverse__
void extend(const list<T>& other){
if(m_capacity<=(m_size+other.m_size)){
m_capacity+=other.m_capacity;
T* new_data=new T[m_capacity];
for(size_t i=0;i<m_size;i++){
new_data[i]=m_data[i];
}
if(m_data!=nullptr){
delete[] m_data;
m_data=nullptr;
}
m_data=new_data;
}
for (size_t i = 0; i < other.m_size; i++)
{
m_size++;
m_data[m_size-1]=other.m_data[i];
}
}
void append(T value){
if(m_size==m_capacity){
if(m_capacity==0){
m_capacity=1;
}
else{
m_capacity*=2;
}
T* new_data=new T[m_capacity];
for(size_t i=0;i<m_size;i++){
new_data[i]=m_data[i];
}
if(m_data!=nullptr){
delete[] m_data;
m_data=nullptr;
}
m_data=new_data;
}
m_size++;
m_data[m_size-1]=value;
}
void clear(){
m_size=0;
delete[] m_data;
m_data=nullptr;
m_capacity=0;
}
};
}
#endif