-
Notifications
You must be signed in to change notification settings - Fork 0
/
mySTL.hpp
339 lines (331 loc) · 12.1 KB
/
mySTL.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
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#pragma once
/*Author: Suryansh Dey
This is free library;
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
#include <bits/stdc++.h>
namespace MyStl
{
template <typename type>
void print1D(type array1D) noexcept;
template <typename type>
void print1D(type array1D, size_t size) noexcept;
/**
* @return
* - `(line_number, column_number)` of first difference
* - `std::pair(0, 0)` if both files are same
* - `std::pair(min(lines_in_outputFile, lines_in_testFile), 0)` if
* they have different number of lines
* @exception
* if cannot open the given outputFilePath or testFilePath
* @note
* it ignores one trailing space of outputFile lines (if present) and one trailing blank line
* of outputFile (if present)
*/
std::pair<uint32_t, uint32_t> fileFirstDifference(const char *outputFilePath, const char *testFilePath);
/**
* syntax:
* FOR(number_of_times_to_loop){
* cout << "Note: i is accessible here: " << i << '\n';
* }
* @note
* i is accessible inside the loop which varies from 1 to number_of_times_to_loop-1
* throught the loop as usual
*/
#define FOR(end) for (int i = 0; i < end; i++)
class BitArray
{
typedef std::vector<unsigned long long> Bits;
public:
BitArray(uint32_t size) noexcept;
/**
* @warning
* trying to excess index < 1 or index >= `this->size` will
* lead to exception from std::vector or undefined behaviour
*/
void set(uint32_t index);
/**
* @warning
* trying to excess index < 1 or index >= `this->size` will
* lead to exception from std::vector or undefined behaviour
*/
void unset(uint32_t index);
/**
* @warning
* trying to excess index < 1 or index >= `this->size` will
* lead to exception from std::vector or undefined behaviour
*/
bool get(uint32_t index);
void operator|=(BitArray &) noexcept;
void operator&=(BitArray &) noexcept;
void operator^=(BitArray &) noexcept;
void bitwiseNot() noexcept;
BitArray operator~() const noexcept;
void print() const noexcept;
inline uint32_t getSize() const noexcept;
protected:
static constexpr uint32_t elementSize = sizeof(unsigned long long) * 8;
uint32_t size;
Bits bits;
};
class Graph;
class Node
{
friend Graph;
public:
typedef unsigned int NodeId;
struct Neighbour
{
typedef int Weight;
NodeId nodeId;
Weight weight;
};
static constexpr uint32_t UNDEFINED_NODE = 0;
Node(NodeId nodeId);
/**
* - time complexity O(1)
*/
NodeId get_nodeId() const noexcept;
/**
* - creates an directed edge
* - time complexity O(1)
* @warning
* connecting with Node of NodeId = 0 will lead to undefined behaviour
*/
void point(const Node &node, Neighbour::Weight weight);
/**
* Disconnects `this` from `node`
* - time complexity O(`this->neighbourCount`)
* @return
* - `false` on failure due to no existing connection
* of `this` with `node`
* - `true` otherwise
*/
bool unpoint(const Node &node) noexcept;
/**
* Disconnects the `node` which `this` visited last time
* - time complexity O(1)
* @return
* - `false` if `this` never visited any node
* - `true` otherwise
*/
bool unpointLastVisit(Graph &graph) noexcept;
/**
* creates an undirected edge
* time complexity O(1)
* @warning
* connecting with Node of NodeId = 0 will lead to undefined behaviour
*/
void connect(Node &node, Neighbour::Weight weight);
/**
* Disconnects `this` from `node`
* - time complexity O(`this->neighbourCount`)
* @return
* - `false` on failure due to no existing connection
* of `this` with `node`
* - `true` otherwise
*/
bool disconnect(Node &node) noexcept;
/**
* Disconnects the `node` which `this` visited last time
* - time complexity O(1)
* @return
* - `false` if `this` never visited any node || on failure due to
* no existing connection of `this` with `node`
* - `true` otherwise
*/
bool disconnectLastVisit(Graph &graph) noexcept;
/**
* - average time complexity O(1)
* - worst time complexity O(`this->neighbourCount`)
* however, even if you call the function number of times before
* Graph::reset() is called, total time complexity will still be
* O(`this->neighbourCount`) instead of O(number of calls * this->neighbourCount)
* @return
* - `Node::UNDEFINED_NODE` if `this` is at dead end or all the neighbours
* are already visited by `this`
* - `NodeId` of newly visited `node` otherwise
* @note
* - it never returns the neighbour which visited `this` last time
* - it doesn't mark it to be visited
*/
Neighbour move(Graph &graph) noexcept;
/**
* - time complexity at worst case O(`this->neighbourCount`)
* however, even if you call the function number of times before
* Graph::reset() is called, total time complexity will still be
* O(`this->neighbourCount`) instead of O(number of calls * this->neighbourCount)
* @return
* - `Node::UNDEFINED_NODE` if `this` is at dead end or all the neighbours
* are already visited
* - `NodeId` of newly visited `node` otherwise
* @note
* - it never returns the neighbour which visited `this` last time
* - it doesn't mark it to be visited
*/
Neighbour moveUnvisited(Graph &graph) noexcept;
/**
* - time complexity O(1)
* @return
* - `Node::UNDEFINED_NODE` if `this` is unvisited `node`
* - `NodeId` of last visitor of `this` otherwise
*/
inline NodeId lastVisitorId() const noexcept;
/**
* - time complexity O(1)
* @return
* - `Neighbour` which visited `this` last time
* - `Neighbour{nodeId = UNDEFINED_NODE, weight = 0}` if `this` is unvisited
*/
inline Neighbour lastVisitor(const Graph &graph) const noexcept;
/**
* - time complexity O(1)
*/
inline void markVisited(NodeId visitorNodeId) noexcept;
/**
* - time complexity O(1)
*/
inline bool isVisited() const noexcept;
/**
* - time complexity O(1)
* @return
* - `Neighbour` which `this` visited last time
* - `Neighbour{nodeId = UNDEFINED_NODE, weight = 0}` if `this` never visited any node
*/
inline Neighbour lastVisited() const noexcept;
/**
* - time complexity O(`this->neighbourCount`)
* @return
* - `Neighbour` (whose `nodeId` is `true` if treated as boolean) which is visited by any node
* EXCEPT FOR THE NEIGHBOUR NODE WHICH VISITED `this` LAST TIME
* - `Neighbour{nodeId = UNDEFINED_NODE, weight = 0}` (whose `nodeId` is `false` if treated as boolean) if no such node found
*/
inline Neighbour hasVisitedNeighbour(const Graph &graph) const noexcept;
/**
* - time complexity O(1)
*/
inline uint32_t neighbourCount() const noexcept;
/**
* - time complexity O(1)
* @note
* higher the index of a neighbour lower the priorty
*/
inline const std::vector<Neighbour> &get_neighbours() const noexcept;
/**
* - time complexity O(priorty it has already)
* @return
* - higher the number lower the priorty
* - `0` representing ` highest priorty
* @exception
* - if there is no neighbour of given NodeId
*/
uint32_t get_priorty(Node::NodeId Neighbour) const;
/**
* - time complexity O(priorty it has already)
* @exception
* - if there is no neighbour of given NodeId
*/
inline void priortiseNeighbourHighest(Node::NodeId neighbour);
/**
* - time complexity O(priorty it has already)
* @exception
* - if there is no neighbour of given NodeId
*/
inline void priortiseNeighbourLowest(Node::NodeId neighbour);
/**
* - time complexity O(`this->neighbourCount` * log(`this->neighbourCount`))
*/
inline void priortiseNeighbourByHeighWeight() noexcept;
/**
* - time complexity O(`this->neighbourCount` * log(`this->neighbourCount`))
*/
inline void priortiseNeighbourByLowWeight() noexcept;
/**
* - time complexity O(`this->neighbourCount` * log(`this->neighbourCount`))
*/
inline void priortiseNeighbourByHeighNodeId() noexcept;
/**
* - time complexity O(`this->neighbourCount` * log(`this->neighbourCount`))
*/
inline void priortiseNeighbourByLowNodeId() noexcept;
protected:
inline void reset() noexcept;
NodeId nodeId;
std::vector<Neighbour> neighbours;
NodeId visitorNodeId = UNDEFINED_NODE;
uint32_t neighbourIndexToVisit = 0;
};
class Graph
{
public:
Graph(uint32_t nodeCount);
/**
* - time complexity O(1)
*/
uint32_t nodeCount() const noexcept;
/**
* @warning
* trying to excess index < 1 or index >= `this->nodeCount` will
* lead to exception from std::vector or undefined behaviour
*/
Node &operator[](Node::NodeId nodeId);
/**
* @warning
* trying to excess index < 1 or index >= `this->nodeCount` will
* lead to exception from std::vector or undefined behaviour
*/
const Node &operator[](Node::NodeId nodeId) const;
/**
* - resets the whole graph to just initialised state but don't disconnect or connect
* any pair of nodes
* - time complexity O(`this->nodeCount`)
*/
void reset() noexcept;
/**
* - time complexity O(`this->nodeCount` * log(`this->nodeCount`))
*/
void priortiseNeighbourByHeighWeight() noexcept;
/**
* - time complexity O(`this->nodeCount` * log(`this->nodeCount`))
*/
void priortiseNeighbourByLowWeight() noexcept;
/**
* - time complexity O(`this->nodeCount` * log(`this->nodeCount`))
*/
void priortiseNeighbourByHeighNodeId() noexcept;
/**
* - time complexity O(`this->nodeCount` * log(`this->nodeCount`))
*/
void priortiseNeighbourByLowNodeId() noexcept;
void inputEdges(uint32_t numberOfEdges) noexcept;
void inputDirectedEdges(uint32_t numberOfEdges) noexcept;
/**
* - time complexity O(next unvisited NodeId - last returned NodeId)
* - In long, even if you call the function number of times before
* last NodeId is reached and before Graph::reset() is called, total time complexity will still be
* O(last NodeId or number of nodes) instead of O(number of calls
* * last NodeId or number of nodes)
* @return
* - `NodeId` of first unvisited node in the graph
* - `UNDEFINED_NODE` if all nodes in the graph is visited already
*/
Node::NodeId unvisitedNode() noexcept;
/**
* - adds `nodeCount` number of nodes
* - time complexity O(nodeCount)
*/
void add(uint32_t nodeCount = 1);
/**
* - removes last node
* - time complexity O(1)
*/
void pop_back() noexcept;
protected:
std::vector<Node> graph;
Node::NodeId lastUnvisitedNodeId = 1;
};
}
// MYSTL_END
#include "src/others.cpp"
#include "src/bitArray.cpp"
#include "src/graph.cpp"