-
Notifications
You must be signed in to change notification settings - Fork 0
/
map_template.h~
214 lines (190 loc) · 3.17 KB
/
map_template.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
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
#ifndef __ASSOCTAB_H__
#define __ASSOCTAB_H__
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <fstream>
using namespace std;
template <class Key, class ClassType> class map_template
{
public:
struct Item
{
ClassType value;
Key key;
};
vector<Item> data;
void Clear()
{
data.clear();
return;
}
int Size() const
{
return data.size();
}
Item *Find(Key key)
{
for(int i = 0; i < Size(); ++i)
{
if(data[i].key == key)
{
return &data[i];
}
}
return nullptr;
}
void Add(Key key, ClassType val)
{
if(Find(key))
{
cout << "klucz juz znajduje sie w bazie\n";
return;
}
Item tempItem;
tempItem.key = key;
tempItem.value = val;
data.push_back(tempItem);
return;
}
map_template()
{
}
~map_template()
{
Clear();
}
friend ostream& operator<< <>(ostream& out, const map_template<class Key, class ClassType>& mt);
};
template <class Key, class ClassType> ostream& operator<<(ostream& out, const map_template<class Key, class ClassType>& mt)
{
for(int i = 0; i < mt.Size(); ++i)
{
cout<<"test ";
//cout << mt.data[i].value << "\n";
}
return out;
}
/*template <class Key, class Value> class assocTab
{
private:
struct node
{
node *next;
char *key;
int val;
node (const char *k):next (NULL)
{
key = new char[strlen (k) + 1];
strcpy (key, k);
};
~node ()
{
delete[]key;
}
node (const node & s):next (NULL)
{
if (s.key == NULL)
key = NULL;
else
{
key = new char[strlen (s.key) + 1];
strcpy (key, s.key);
}
val=s.val;
};
private: //assignment not allowed
node & operator= (const node &);
};
node *head;
void insert (const char *key, int value);
void clear ();
node *find (const char *key) const;
void swap (assocTab & l);
public:
assocTab ();
assocTab (const assocTab & l);
assocTab & operator= (const assocTab & l);
~assocTab ();
int &operator[] (const char *);
};
assocTab::assocTab ()
{
head = NULL;
}
void assocTab::clear ()
{
while (head)
{
node *t = head->next;
delete head;
head = t;
};
}
assocTab::~assocTab ()
{
clear ();
}
void assocTab::insert (const char *key, int value)
{
node *nowy = new node (key);
nowy->next = head;
head = nowy;
head->val = value;
}
void assocTab::swap (assocTab & l)
{
node *t = head;
head = l.head;
l.head = t;
}
assocTab::assocTab (const assocTab & l)
{
node *src, **dst;
head = NULL;
src = l.head;
dst = &head;
try
{
while (src)
{
*dst = new node (*src);
src = src->next;
dst = &((*dst)->next);
}
}
catch (...)
{
clear ();
throw;
};
}
assocTab & assocTab::operator= (const assocTab & l)
{
if (&l == this)
return *this;
assocTab t (l);
swap (t);
return *this;
}
assocTab::node * assocTab::find (const char *key) const
{
node * c = head;
while (c)
{
if (!strcmp (c->key, key)) return c;
c = c->next;
};
return NULL;
}
int &assocTab::operator[] (const char *key)
{
node *c = find (key);
if (!c)
{
insert (key, 0);
c = head;
};
return c->val;
}*/
#endif /* __ASSOCTAB_H__ */