-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist.hpp
More file actions
607 lines (515 loc) · 16.1 KB
/
list.hpp
File metadata and controls
607 lines (515 loc) · 16.1 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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
#ifndef __LIST_HPP
#define __LIST_HPP
namespace shedskin {
// Forward declarations
template<class K, class V> class dict;
template<class T> class set;
template<class T> class __iter;
template<typename T>
class list : public pyobj {
private:
struct Node {
T data;
Node* next;
Node(const T& value) : data(value), next(nullptr) {}
};
Node* head;
Node* tail; // Tail pointer for O(1) append
__ss_int size_;
void append_multiple() {}
template<typename First, typename... Rest>
void append_multiple(First first, Rest... rest) {
append(first); // Add current element
append_multiple(rest...); // Recursive call to add others
}
public:
// Default constructor
list() : head(nullptr), tail(nullptr), size_(0) {}
// Single value constructor
list(__ss_int count, const T& value) : head(nullptr), tail(nullptr), size_(0) {
if(count > 0) {
append(value);
if(count > 1) append(value);
}
}
// Two value constructor
list(__ss_int count, const T& value1, const T& value2) : head(nullptr), tail(nullptr), size_(0) {
append(value1);
if(count > 1) append(value2);
}
// Three value constructor
list(__ss_int count, const T& v1, const T& v2, const T& v3) : head(nullptr), tail(nullptr), size_(0) {
append(v1);
if(count > 1) append(v2);
if(count > 2) append(v3);
}
template<typename... Args>
list(__ss_int count, Args... args) : head(nullptr), tail(nullptr), size_(0) {
if (sizeof...(args) != count) {
throw std::invalid_argument("The number of arguments must correspond to the counter.");
}
append_multiple(args...);
}
// Copy constructor
list(list<T>* other) : head(nullptr), tail(nullptr), size_(0) {
if (other) {
for (__ss_int i = 0; i < other->size(); i++) {
append(other->__getfast__(i));
}
}
}
// Iterator constructor
list(__iter<T>* iter) : head(nullptr), tail(nullptr), size_(0) {
if (iter) {
while (!iter->__stop_iteration) {
T item = iter->__get_next();
if (!iter->__stop_iteration) {
append(item);
}
}
}
}
// Destructor
~list() {
clear();
}
// Assignment operator
list<T>& operator=(const list<T>& other) {
if (this != &other) {
clear();
Node* current = other.head;
while (current) {
append(current->data);
current = current->next;
}
}
return *this;
}
// O(1) append operation using tail pointer
void append(const T& value) {
Node* newNode = new Node(value);
if (!head) {
head = tail = newNode;
} else {
tail->next = newNode;
tail = newNode;
}
size_++;
}
// Get element at index
T __getfast__(__ss_int index) const {
if (index < 0 || index >= size_) {
return T();
}
Node* current = head;
for (__ss_int i = 0; i < index && current; i++) {
current = current->next;
}
return current ? current->data : T();
}
// Set element at index
void __setitem__(__ss_int index, const T& value) {
if (index < 0 || index >= size_) {
return;
}
Node* current = head;
for (__ss_int i = 0; i < index && current; i++) {
current = current->next;
}
if (current) {
current->data = value;
}
}
// Clear the list
void clear() {
while (head) {
Node* temp = head;
head = head->next;
delete temp;
}
tail = nullptr;
size_ = 0;
}
// Get size
__ss_int size() const {
return size_;
}
// Python-style len()
__ss_int __len__() const {
return size_;
}
// Python-style getitem
T __getitem__(__ss_int index) const {
if (index < 0) {
index = size_ + index;
}
return __getfast__(index);
}
// Remove element at index
void __remove__(__ss_int index) {
if (index < 0) {
index = size_ + index;
}
if (index < 0 || index >= size_) return;
Node* current = head;
Node* previous = nullptr;
for (__ss_int i = 0; i < index && current; i++) {
previous = current;
current = current->next;
}
if (!current) return;
if (previous) {
previous->next = current->next;
if (current == tail) {
tail = previous;
}
} else {
head = current->next;
if (head == nullptr) {
tail = nullptr;
}
}
delete current;
size_--;
}
// Insert element at index
void insert(__ss_int index, const T& value) {
if (index < 0) {
index = size_ + index;
}
if (index < 0) index = 0;
if (index >= size_) {
append(value);
return;
}
Node* newNode = new Node(value);
if (index == 0) {
newNode->next = head;
head = newNode;
} else {
Node* current = head;
for (__ss_int i = 0; i < index - 1; i++) {
current = current->next;
}
newNode->next = current->next;
current->next = newNode;
}
size_++;
}
// Check equality with another list
bool equals(const list<T>* other) const {
if (!other || size_ != other->size_) return false;
Node* n1 = head;
Node* n2 = other->head;
while (n1 && n2) {
if (!(n1->data == n2->data)) return false;
n1 = n1->next;
n2 = n2->next;
}
return true;
}
// Get first element
T __getfirst__() const {
return head ? head->data : T();
}
// Get last element
T __getlast__() const {
return tail ? tail->data : T();
}
// Extend list with elements from another list
void extend(list<T>* other) {
if (!other) return;
Node* current = other->head;
while (current) {
append(current->data);
current = current->next;
}
}
// Iterator support
class Iterator {
Node* current;
public:
Iterator(Node* node) : current(node) {}
Iterator& operator++() { if(current) current = current->next; return *this; }
bool operator!=(const Iterator& other) { return current != other.current; }
T& operator*() { return current->data; }
};
Iterator begin() { return Iterator(head); }
Iterator end() { return Iterator(nullptr); }
const Iterator begin() const { return Iterator(head); }
const Iterator end() const { return Iterator(nullptr); }
// Check if an element is in the list (Python 'in' operator)
bool __contains__(const T& value) const {
Node* current = head;
while (current) {
if (current->data == value) {
return true;
}
current = current->next;
}
return false;
}
// Support for Python slicing (list[start:end])
list<T>* __getslice__(__ss_int start, __ss_int end) const {
list<T>* slice = new list<T>();
if (start < 0) start = size_ + start;
if (end < 0) end = size_ + end;
// Ajuster les bornes sans std::min/max
if (start < 0) start = 0;
if (start > size_) start = size_;
if (end < 0) end = 0;
if (end > size_) end = size_;
Node* current = head;
for (__ss_int i = 0; i < start && current; i++) {
current = current->next;
}
for (__ss_int i = start; i < end && current; i++) {
slice->append(current->data);
current = current->next;
}
return slice;
}
// Support for Python slicing
list<T>* __slice__(__ss_int length, __ss_int start, __ss_int stop, __ss_int step) const {
list<T>* slice = new list<T>();
if (start < 0) start = size_ + start;
if (stop < 0) stop = size_ + stop;
if (start < 0) start = 0;
if (start > size_) start = size_;
if (stop < 0) stop = 0;
if (stop > size_) stop = size_;
Node* current = head;
for (__ss_int i = 0; i < start && current; i++) {
current = current->next;
}
for (__ss_int i = start; i < stop && current; i++) {
slice->append(current->data);
current = current->next;
}
return slice;
}
// Operator overload for list concatenation
list<T>* operator+(const list<T>* other) const {
list<T>* result = new list<T>(*this);
if (other) {
Node* current = other->head;
while (current) {
result->append(current->data);
current = current->next;
}
}
return result;
}
// Operator overload for list replication
list<T>* operator*(__ss_int n) const {
list<T>* result = new list<T>();
for (__ss_int i = 0; i < n; i++) {
Node* current = head;
while (current) {
result->append(current->data);
current = current->next;
}
}
return result;
}
// Python-style addition
list<T>* __add__(list<T>* other) const {
list<T>* result = new list<T>(*this);
if (other) {
Node* current = other->head;
while (current) {
result->append(current->data);
current = current->next;
}
}
return result;
}
// Python-style multiplication
list<T>* __mul__(__ss_int n) const {
list<T>* result = new list<T>();
for (__ss_int i = 0; i < n; i++) {
Node* current = head;
while (current) {
result->append(current->data);
current = current->next;
}
}
return result;
}
// Modify the for_in_loop class inside list class (around line 263):
class for_in_loop {
typename list<T>::Iterator it;
typename list<T>::Iterator end_it;
public:
// Ajout d'un constructeur par défaut
for_in_loop() : it(nullptr), end_it(nullptr) {}
// Constructeur principal
for_in_loop(list<T>& l) : it(l.begin()), end_it(l.end()) {}
bool __next__(T& ref) {
if (it != end_it) {
ref = *it;
++it;
return true;
}
return false;
}
};
// Deletes and returns the element to the given index (by default, the last element)
T pop(__ss_int index = -1) {
if (size_ == 0) {
throw std::out_of_range("pop from empty list"); // Error if list is empty
}
// If the index is negative, we convert it to positive
if (index < 0) {
index = size_ + index;
}
// Checking terminals
if (index < 0 || index >= size_) {
throw std::out_of_range("pop index out of range");
}
Node* current = head;
Node* previous = nullptr;
// Find element with given index
for (__ss_int i = 0; i < index; i++) {
previous = current;
current = current->next;
}
T value = current->data; // Save value before deletion
// Delete element
if (previous) {
previous->next = current->next;
if (current == tail) { // Update `tail` if last item deleted
tail = previous;
}
} else {
head = current->next; // Update `head` if first element deleted
if (!head) {
tail = nullptr; // Empty list after deletion
}
}
delete current;
size_--;
return value; // Returns the deleted element
}
// Count occurrences of a value
__ss_int count(const T& value) const {
__ss_int cnt = 0;
Node* current = head;
while (current) {
if (current->data == value) {
cnt++;
}
current = current->next;
}
return cnt;
}
// Find first index of value (supports negative start), raises if not found
__ss_int index(const T& value, __ss_int start = 0) const {
if (start < 0) {
start = size_ + start;
}
if (start < 0) start = 0;
if (start >= size_) {
throw std::out_of_range("list.index(x): x not in list");
}
Node* current = head;
for (__ss_int i = 0; i < start && current; i++) {
current = current->next;
}
__ss_int idx = start;
while (current) {
if (current->data == value) {
return idx;
}
current = current->next;
idx++;
}
throw std::out_of_range("list.index(x): x not in list");
}
// Remove first occurrence; raises if not found
void remove(const T& value) {
if (!head) {
throw std::out_of_range("list.remove(x): x not in list");
}
Node* current = head;
Node* previous = nullptr;
__ss_int idx = 0;
while (current) {
if (current->data == value) {
if (previous) {
previous->next = current->next;
if (current == tail) {
tail = previous;
}
} else {
head = current->next;
if (!head) {
tail = nullptr;
}
}
delete current;
size_--;
return;
}
previous = current;
current = current->next;
idx++;
}
throw std::out_of_range("list.remove(x): x not in list");
}
};
// Global helper functions
template<typename T>
__ss_int len(list<T>* lst) {
return lst ? lst->__len__() : 0;
}
template<class K, class V>
__ss_int len(dict<K,V>* d) {
return d ? d->__len__() : 0;
}
template<class T>
__ss_int len(set<T>* s) {
return s ? s->__len__() : 0;
}
template<typename T>
bool __eq(list<T>* a, list<T>* b) {
if (!a || !b) return false;
return a->equals(b);
}
// Default identity structure for basic types (int, float, etc.)
template<typename T>
struct Identity {
const T& operator()(const T& x) const { return x; }
};
// Main version of `sorted()` with 4 arguments
template<typename T, typename Compare>
list<T>* sorted(list<T>* lst, __ss_int start, Compare key, bool reverse) {
if (!lst) return new list<T>();
list<T>* sorted_list = new list<T>(*lst); // Copy the original list
// Bubble Sort
for (__ss_int i = 0; i < sorted_list->size() - 1; i++) {
for (__ss_int j = 0; j < sorted_list->size() - i - 1; j++) {
auto val1 = key(sorted_list->__getfast__(j));
auto val2 = key(sorted_list->__getfast__(j + 1));
bool condition = reverse ? (val1 < val2) : (val1 > val2);
if (condition) {
T temp = sorted_list->__getfast__(j);
sorted_list->__setitem__(j, sorted_list->__getfast__(j + 1));
sorted_list->__setitem__(j + 1, temp);
}
}
}
return sorted_list;
}
// Simplified version with `reverse` (for numeric types)
template<typename T>
list<T>* sorted(list<T>* lst, bool reverse = false) {
return sorted(lst, 0, Identity<T>(), reverse);
}
// Version compatible with 4 arguments (to avoid ShedSkin errors)
template<typename T>
list<T>* sorted(list<T>* lst, __ss_int start, __ss_int key, __ss_int reverse) {
return sorted(lst, start, Identity<T>(), reverse);
}
} // namespace shedskin
#endif