forked from Tzunami/Infoledger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathList.h
executable file
·170 lines (159 loc) · 4.62 KB
/
List.h
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
/*
* List.h
*
* Created on: Nov 25, 2015
* Author: tzunami
*/
#pragma once
#ifndef LIST_H_
#define LIST_H_
#include <vector>
#include <iostream> //delete
namespace Infoledger {
/****************************************************************/
template <class T>
class List {
public:
//members
std::vector<T*> list;
// Con/De-structors
List<T>();
List<T>(T& t);
virtual ~List<T>(){}
// operators
List<T> &operator+(T& p);
List<T> &operator-(T& p);
void operator+=(T& p);
void operator-=(T& p);
List<T> &operator=(List<T>* p);
List<T> &operator=(List<T>& p);
List<T> &operator+(List<T>& p);
List<T> &operator-(List<T>& p);
void operator+=(List<T>* p);
void operator+=(List<T>& p);
void operator-=(List<T>* p);
void operator-=(List<T>& p);
//function
void Print();
protected:
unsigned int GetDuplicate(T &p) const;
void RemoveDuplicate(const T &p);
void RemoveDuplicate(List<T> &p);
};
/******************************** cpp ********************************/
template<class T> List<T>::List() {}
/********************************************/
template<class T> List<T>::List(T& t) { list.push_back(&t); }
/***************************** operators *****************************/
template <class T>
List<T> &List<T>::operator+(T& p) {
unsigned int duplicate = GetDuplicate(p);
List<T>* pl = new List<T>();
*pl = *this;
if(duplicate) return *pl;
pl->list.push_back(&p);
return *pl;
}
/********************************************/
template <class T>
List<T> &List<T>::operator-(T& p) {
unsigned int remove = GetDuplicate(p);
List<T>* pl = new List<T>();
*pl = *this;
if(remove--) pl->list.erase(pl->list.begin() + remove);
return *pl;
}
/********************************************/
template <class T>
void List<T>::operator+=(T& p) {
unsigned int duplicate = GetDuplicate(p);
if(duplicate) return;
list.push_back(&p);
}
/********************************************/
template <class T>
void List<T>::operator-=(T& p) {
unsigned int remove = GetDuplicate(p);
if(remove--) list.erase(list.begin() + remove);
}
//operators List<T>
/********************************************/
template <class T>
List<T> &List<T>::operator=(List<T>* p) {
delete this;
*this = *p;
return *this;
}
/********************************************/
template <class T>
List<T> &List<T>::operator=(List<T>& p) {
list = p.list;
return *this;
}
/********************************************/
template <class T>
List<T> &List<T>::operator+(List<T>& p) {
List<T>* pl = new List<T>();
*pl = *this;
pl->RemoveDuplicate(p);
pl->list.insert(pl->list.end(), p.list.begin(), p.list.end());
return *pl;
}
/********************************************/
template <class T>
List<T> &List<T>::operator-(List<T>& p) {
List<T>* pl = new List<T>();
*pl = *this;
pl->RemoveDuplicate(p);
return *pl;
}
/********************************************/
template <class T>
void List<T>::operator+=(List<T>* p) {
RemoveDuplicate(*p);
this->list.insert(this->list.end(), p->list.begin(), p->list.end());
}
/********************************************/
template <class T>
void List<T>::operator+=(List<T>& p) {
RemoveDuplicate(p);
list.insert(list.end(), p.list.begin(), p.list.end());
}
/********************************************/
template <class T>
void List<T>::operator-=(List<T>* p) {
RemoveDuplicate(*p);
}
/********************************************/
template <class T>
void List<T>::operator-=(List<T>& p) {
RemoveDuplicate(p);
}
/********************************************/
template <class T>
unsigned int List<T>::GetDuplicate(T& p) const {
for(unsigned int i=0; i<this->list.size(); i++)
if(list[i]==&p)
return ++i;
return 0;
}
/********************************************/
template <class T>
void List<T>::RemoveDuplicate(List<T>& p) {
for(unsigned int x=0; x<this->list.size(); x++)
for(unsigned int y=0; y<p.list.size(); y++)
if(this->list[x] == p.list[y]) {
this->list.erase(this->list.begin() + x);
if(x>=this->list.size()) return;
}
}
/**************************************************/
template <class T>
void List<T>::Print() {
for(unsigned int i=0; i<list.size(); i++) {
list[i]->Print();
}//end for
}
/****************************************************************/
} /* namespace Infoledger */
#endif /* LIST_H_ */