-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmart_list.h
More file actions
323 lines (307 loc) · 7.37 KB
/
Copy pathSmart_list.h
File metadata and controls
323 lines (307 loc) · 7.37 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#include<memory>
#include<iostream>
using namespace std;
#ifndef INIT_LIST
#define INIT_LIST 10
#include<initializer_list>
#endif
#ifndef MATH_H
#define MATH_H
#include<cmath>
#define NOT_FOUND pow(2,32)
#endif
#ifndef CCTYPE_H
#define CCTYPE_H
#include<cctype>
#endif
//deleter for char
template<class T>
class Deleter_for_string{
public:
void operator()(T* ptr){
if(ptr!=nullptr){
delete [ ] ptr;
}
}
};
#ifndef SMART_NODE
#define SMART_NODE 12
template<class T>
struct Node{
T value;
shared_ptr<Node<T>> next; // each next is a shared pointer...
};
template<class T>
void add_element(shared_ptr<Node<T>>&ptr,const T& value1){
if(ptr.get()==nullptr){
ptr.reset(new Node<T>);
ptr.get()->value=value1;
return;
}
add_element(ptr.get()->next,value1);
}
template<class T>
void print_out_node(const shared_ptr<Node<T>>& ptr){
shared_ptr<Node<T>> ptr1={ptr};
while(ptr1.get()!=nullptr){
if(ptr1.get()->next.get()!=nullptr){
cout<<ptr1.get()->value<<",";
}
else{
cout<<ptr1.get()->value<<endl;
}
ptr1=ptr1.get()->next;
}
}
#endif
#ifndef SMART_LIST
#define SMART_LIST
template<class T>
class Smart_list{//size of smart list is still the same as the size of the shared_ptr type..
private:
shared_ptr<Node<T>> ptr;
public:
Smart_list():ptr{}{}
void Insert(const T& value1){
add_element(ptr,value1);
}
Smart_list(const initializer_list<T>& list){
auto key=list.begin();
for(int i=0;i<list.size();i++){
this->Insert(key[i]);
}
}
shared_ptr<Node<T>> get_head() const{
return this->ptr;
}
void clear(){
ptr.reset();
}
const char* check_null(){
if(ptr.get()==nullptr){
const char*g={"it is null"};
return g;
}
const char*h={"it is not null"};
return h;
}
shared_ptr<Node<T>> get_begin(){
return this->ptr;
}
shared_ptr<Node<T>> get_end(){
shared_ptr<Node<T>> ptr1;
if(ptr1.get()==nullptr){
return ptr1;
}
}
};
#endif
template<class T>
ostream& operator<<(ostream& os,const Smart_list<T>& ptr){
shared_ptr<Node<T>> ptr1={ptr.get_head()};
while(ptr1.get()!=nullptr){
if(ptr1.get()->next.get()!=nullptr){
os<<ptr1.get()->value<<",";
}
else{
os<<ptr1.get()->value<<endl;
}
ptr1=ptr1.get()->next;
}
return os;
}
#ifndef SMART_ITERATOR
#define SMART_ITERATOR
template<class T>
class Smart_iterator{
public:
shared_ptr<Node<T>>iter;
public:
Smart_iterator():iter{}{}
Smart_iterator(const shared_ptr<Node<T>>& sptr){
iter=sptr;
}
Smart_iterator& operator=(const shared_ptr<Node<T>>& sptr){
*this={sptr};
return *this;
}
Smart_iterator& operator++(){
iter=iter.get()->next;
return *this;
}
const Smart_iterator operator++(int){
shared_ptr<Node<T>> ptr={iter};
iter=iter.get()->next;
return ptr;
}
};
#endif
template<class T>
ostream& operator<<(ostream& os,const Smart_iterator<T>& iter1){
os<<iter1.iter.get()->value<<endl;
return os;
}
#ifndef SMART_BUFFER_STRING
#define SMART_BUFFER_STRING 1000000
class Smart_string{
private:
shared_ptr<char>ptr;
size_t ssize;
public:
Smart_string():ptr{}{}
Smart_string(const size_t& sz,const char& c){
ptr.reset(new char[sz],Deleter_for_string<char>());
for(int i=0;i<sz;i++){
ptr.get()[i]=c;
}
ssize=sz;
}
//sets the size, allocate memory according to size,and initializes the allocated variables with ' '
void set_size(const size_t& sz){
if(ptr.get()!=nullptr){
ptr.reset();
}
ptr.reset(new char[sz],Deleter_for_string<char>());
for(int i=0;i<sz;i++){
ptr.get()[i]=' ';
}
ssize=sz;
}
Smart_string(const size_t& sz){
*this={sz,' '};
}
Smart_string(const char* ch){
size_t sz=strlen(ch);
ptr.reset(new char[sz],Deleter_for_string<char>());
for(int i=0;i<sz;i++){
ptr.get()[i]=ch[i];
}
ssize=sz;
}
//returns ptr;
shared_ptr<char> get_first_head() const{
return ptr;
}
char& operator [ ](const size_t& idx){
return ptr.get()[idx];
}
char operator [ ](const size_t& idx) const{
return ptr.get()[idx];
}
//for size.........
size_t get_smart_size() const {
return ssize;
}
Smart_string(const Smart_string& str){
ptr=str.ptr;
ssize=str.ssize;
}
void push_char(const char& ch){
if(ptr.get()==nullptr){
ptr.reset(new char[1],Deleter_for_string<char>());
ssize=1;
ptr.get()[0]=ch;
return;
}
Smart_string str1={*this};
this->ptr.reset();
ptr.reset(new char[str1.ssize+1],Deleter_for_string<char>());
this->ssize=str1.ssize+1;
for(int i=0;i<str1.get_smart_size();i++){
ptr.get()[i]=str1[i];
}
ptr.get()[str1.ssize]=ch;
str1.get_first_head().reset();
str1.ssize=0;
return;
}
Smart_string substring(const size_t& idx) const {
size_t sz=this->ssize- idx;
Smart_string str;
str.set_size(sz);
for(int i=0;i<sz;i++){
str[i]=this->ptr.get()[i+idx];
}
return str;
}
//it creates a substring starting at idx with len..
Smart_string substring(const size_t& idx,const size_t len) const {
Smart_string str;
for(int i=idx;i<len;i++){
str.push_char(this->get_first_head().get()[i]);
}
return str;
}
//overloading the (==) equality operator...
bool operator==(const Smart_string& str) const {
if(this->get_smart_size()==str.get_smart_size()){
for(int i=0;i<this->get_smart_size();i++){
if(this->get_first_head().get()[i]!=str[i]){
return false;
}
}
return true;
}
return false;
}
//returns the starting index of a substring within a string..
//The length of the substring must be less than the smart string object.
size_t find_idx(const char*c) const{
Smart_string str={c};
size_t sz=str.get_smart_size(); // returns the length of string of characters.
size_t sz1=this->get_smart_size()-sz;
++sz1;
for(int i=0;i<sz1;i++){
if(this->substring(i,sz)==str){
return i;
}
}
return NOT_FOUND;
}
Smart_string operator+(const Smart_string& str){
size_t sz=this->get_smart_size()+str.get_smart_size();
Smart_string str1={sz};
for(int i=0;i<this->get_smart_size();i++){
str1[i]=this->ptr.get()[i];
}
for(int i=0;i<str.get_smart_size();i++){
str1[this->get_smart_size()+i]=str[i];
}
return str1;
}
};
//overloading the output operator
ostream& operator<<(ostream& os,const Smart_string& str){
for(size_t sz=0;sz<str.get_smart_size();sz++){
os<<str[sz];
}
os<<endl;
os<<"string size: "<<str.get_smart_size()<<endl;
return os;
}
#endif // smart_string
enum WIDGET_CONTROL{
BUTTON,
CHECK_BOX,
LIST_BOX,
SLIDER,
RADIO_BOX,
LABEL,
BUTTON_WITH_LABEL,
PROGRESS_BAR,
COMBO_BOX,
CHECK_LIST_BOX,
};
#ifndef STRING_CONVERT
#define STRING_CONVERT 10
#include<string>
#endif
#ifdef STRING_CONVERT
string convert_sstring_to_string(const Smart_string& str1){
string str;
for(int i=0;i<str1.get_smart_size();i++){
str.push_back(str1[i]);
}
return str;
}
#endif