-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsequence_kernel_2.h
More file actions
682 lines (577 loc) · 19.6 KB
/
sequence_kernel_2.h
File metadata and controls
682 lines (577 loc) · 19.6 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
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
// Copyright (C) 2003 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_SEQUENCE_KERNEl_2_
#define DLIB_SEQUENCE_KERNEl_2_
#include "sequence_kernel_abstract.h"
#include "../algs.h"
#include "../interfaces/enumerable.h"
#include "../interfaces/remover.h"
#include "../serialize.h"
namespace dlib
{
template <
typename T,
typename mem_manager = default_memory_manager
>
class sequence_kernel_2 : public enumerable<T>,
public remover<T>
{
/*!
INITIAL VALUE
sequence_size == 0
at_start_ == true
current_enumeration_node == 0
CONVENTION
sequence_size == the number of elements in the sequence
at_start_ == at_start()
(current_enumeration_node!=0) == current_element_valid()
if (current_enumeration_node!=0) then
current_enumeration_node->item == element()
current_enumeration_pos == the position of the node pointed to by
current_enumeration_node
if ( sequence_size > 0 )
{
current_node == pointer to a node in the linked list and
current_node->right->right->... eventually == current_node and
current_node->left->left->... eventually == current_node and
current_pos == the position in the sequence of
current_node->item
}
!*/
struct node {
T item;
node* right;
node* left;
};
public:
typedef T type;
typedef mem_manager mem_manager_type;
sequence_kernel_2 (
) :
sequence_size(0),
at_start_(true),
current_enumeration_node(0)
{}
virtual ~sequence_kernel_2 (
);
inline void clear (
);
void add (
unsigned long pos,
T& item
);
void remove (
unsigned long pos,
T& item
);
void cat (
sequence_kernel_2& item
);
const T& operator[] (
unsigned long pos
) const;
T& operator[] (
unsigned long pos
);
void swap (
sequence_kernel_2& item
);
// functions from the remover interface
inline void remove_any (
T& item
);
// functions from the enumerable interface
inline size_t size (
) const;
bool at_start (
) const;
inline void reset (
) const;
bool current_element_valid (
) const;
const T& element (
) const;
T& element (
);
bool move_next (
) const;
private:
void delete_nodes (
node* current_node,
unsigned long sequence_size
);
/*!
requires
CONVENTION IS CORRECT
ensures
all memory associated with the ring of nodes has been freed
!*/
void move_to_pos (
node*& current_node,
unsigned long& current_pos,
unsigned long pos,
unsigned long size
) const;
/*!
requires
everything in the CONVENTION is correct and
there is a node corresponding to pos in the CONVENTION and
0 <= pos < size
ensures
current_pos == pos and
current_node->item is the item in the sequence associated with
position pos
!*/
// data members
unsigned long sequence_size;
mutable node* current_node;
mutable unsigned long current_pos;
mutable bool at_start_;
mutable node* current_enumeration_node;
mutable unsigned long current_enumeration_pos;
// restricted functions
sequence_kernel_2(sequence_kernel_2&); // copy constructor
sequence_kernel_2& operator=(sequence_kernel_2&); // assignment operator
};
template <
typename T,
typename mem_manager
>
inline void swap (
sequence_kernel_2<T,mem_manager>& a,
sequence_kernel_2<T,mem_manager>& b
) { a.swap(b); }
template <
typename T,
typename mem_manager
>
void deserialize (
sequence_kernel_2<T,mem_manager>& item,
std::istream& in
)
{
try
{
item.clear();
unsigned long size;
deserialize(size,in);
T temp;
for (unsigned long i = 0; i < size; ++i)
{
deserialize(temp,in);
item.add(i,temp);
}
}
catch (serialization_error& e)
{
item.clear();
throw serialization_error(e.info + "\n while deserializing object of type sequence_kernel_2");
}
}
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// member function definitions
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
template <
typename T,
typename mem_manager
>
sequence_kernel_2<T,mem_manager>::
~sequence_kernel_2 (
)
{
delete_nodes(current_node,sequence_size);
}
// ----------------------------------------------------------------------------------------
template <
typename T,
typename mem_manager
>
void sequence_kernel_2<T,mem_manager>::
clear (
)
{
if (sequence_size != 0)
{
delete_nodes(current_node,sequence_size);
sequence_size = 0;
}
// reset the enumerator
reset();
}
// ----------------------------------------------------------------------------------------
template <
typename T,
typename mem_manager
>
void sequence_kernel_2<T,mem_manager>::
add (
unsigned long pos,
T& item
)
{
// make new node and swap item into it
node* new_node = new node;
exchange(item,new_node->item);
if (sequence_size > 0)
{
if (pos == sequence_size)
{
move_to_pos(current_node,current_pos,pos-1,sequence_size);
node& n_node = *new_node;
node& c_node = *current_node;
// make new node point to the nodes to its left and right
n_node.right = c_node.right;
n_node.left = current_node;
// make the left node point back to new_node
c_node.right->left = new_node;
// make the right node point back to new_node
c_node.right = new_node;
current_pos = pos;
}
else
{
move_to_pos(current_node,current_pos,pos,sequence_size);
node& n_node = *new_node;
node& c_node = *current_node;
// make new node point to the nodes to its left and right
n_node.right = current_node;
n_node.left = c_node.left;
// make the left node point back to new_node
c_node.left->right = new_node;
// make the right node point back to new_node
c_node.left = new_node;
}
}
else
{
current_pos = 0;
new_node->left = new_node;
new_node->right = new_node;
}
// make the new node the current node
current_node = new_node;
++sequence_size;
// reset the enumerator
reset();
}
// ----------------------------------------------------------------------------------------
template <
typename T,
typename mem_manager
>
void sequence_kernel_2<T,mem_manager>::
remove (
unsigned long pos,
T& item
)
{
move_to_pos(current_node,current_pos,pos,sequence_size);
node& c_node = *current_node;
exchange(c_node.item,item);
node* temp = current_node;
// close up gap left by remove
c_node.left->right = c_node.right;
c_node.right->left = c_node.left;
current_node = c_node.right;
--sequence_size;
delete temp;
// reset the enumerator
reset();
}
// ----------------------------------------------------------------------------------------
template <
typename T,
typename mem_manager
>
const T& sequence_kernel_2<T,mem_manager>::
operator[] (
unsigned long pos
) const
{
move_to_pos(current_node,current_pos,pos,sequence_size);
return current_node->item;
}
// ----------------------------------------------------------------------------------------
template <
typename T,
typename mem_manager
>
void sequence_kernel_2<T,mem_manager>::
cat (
sequence_kernel_2<T,mem_manager>& item
)
{
if (item.sequence_size > 0)
{
if (sequence_size > 0)
{
// move both sequences to a convenient location
move_to_pos(current_node,current_pos,0,sequence_size);
item.move_to_pos (
item.current_node,
item.current_pos,
item.sequence_size-1,
item.sequence_size
);
// make copies of poitners
node& item_right = *item.current_node->right;
node& left = *current_node->left;
item.current_node->right = current_node;
current_node->left = item.current_node;
left.right = &item_right;
item_right.left = &left;
// set sizes
sequence_size += item.sequence_size;
item.sequence_size = 0;
}
else
{
// *this is empty so just swap
item.swap(*this);
}
}
item.clear();
// reset the enumerator
reset();
}
// ----------------------------------------------------------------------------------------
template <
typename T,
typename mem_manager
>
T& sequence_kernel_2<T,mem_manager>::
operator[] (
unsigned long pos
)
{
move_to_pos(current_node,current_pos,pos,sequence_size);
return current_node->item;
}
// ----------------------------------------------------------------------------------------
template <
typename T,
typename mem_manager
>
size_t sequence_kernel_2<T,mem_manager>::
size (
) const
{
return sequence_size;
}
// ----------------------------------------------------------------------------------------
template <
typename T,
typename mem_manager
>
void sequence_kernel_2<T,mem_manager>::
swap (
sequence_kernel_2<T,mem_manager>& item
)
{
unsigned long sequence_size_temp = item.sequence_size;
node* current_node_temp = item.current_node;
unsigned long current_pos_temp = item.current_pos;
bool at_start_temp = item.at_start_;
node* current_enumeration_node_temp = item.current_enumeration_node;
unsigned long current_enumeration_pos_temp = item.current_enumeration_pos;
item.sequence_size = sequence_size;
item.current_node = current_node;
item.current_pos = current_pos;
item.at_start_ = at_start_;
item.current_enumeration_node = current_enumeration_node;
item.current_enumeration_pos = current_enumeration_pos;
sequence_size = sequence_size_temp;
current_node = current_node_temp;
current_pos = current_pos_temp;
at_start_ = at_start_temp;
current_enumeration_node = current_enumeration_node_temp;
current_enumeration_pos = current_enumeration_pos_temp;
}
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// enumerable function definitions
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
template <
typename T,
typename mem_manager
>
bool sequence_kernel_2<T,mem_manager>::
at_start (
) const
{
return at_start_;
}
// ----------------------------------------------------------------------------------------
template <
typename T,
typename mem_manager
>
void sequence_kernel_2<T,mem_manager>::
reset (
) const
{
at_start_ = true;
current_enumeration_node = 0;
}
// ----------------------------------------------------------------------------------------
template <
typename T,
typename mem_manager
>
bool sequence_kernel_2<T,mem_manager>::
current_element_valid (
) const
{
return (current_enumeration_node!=0);
}
// ----------------------------------------------------------------------------------------
template <
typename T,
typename mem_manager
>
const T& sequence_kernel_2<T,mem_manager>::
element (
) const
{
return current_enumeration_node->item;
}
// ----------------------------------------------------------------------------------------
template <
typename T,
typename mem_manager
>
T& sequence_kernel_2<T,mem_manager>::
element (
)
{
return current_enumeration_node->item;
}
// ----------------------------------------------------------------------------------------
template <
typename T,
typename mem_manager
>
bool sequence_kernel_2<T,mem_manager>::
move_next (
) const
{
if (at_start_ && sequence_size>0)
{
move_to_pos(current_node,current_pos,0,sequence_size);
current_enumeration_node = current_node;
current_enumeration_pos = 0;
}
else if (current_enumeration_node!=0)
{
++current_enumeration_pos;
if (current_enumeration_pos<sequence_size)
{
current_enumeration_node = current_enumeration_node->right;
}
else
{
// we have reached the end of the sequence
current_enumeration_node = 0;
}
}
at_start_ = false;
return (current_enumeration_node!=0);
}
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// remover function definitions
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
template <
typename T,
typename mem_manager
>
void sequence_kernel_2<T,mem_manager>::
remove_any (
T& item
)
{
remove(0,item);
}
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// private member function definitions
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
template <
typename T,
typename mem_manager
>
void sequence_kernel_2<T,mem_manager>::
delete_nodes (
node* current_node,
unsigned long sequence_size
)
{
node* temp;
while (sequence_size)
{
temp = current_node->right;
delete current_node;
current_node = temp;
--sequence_size;
}
}
// ----------------------------------------------------------------------------------------
template <
typename T,
typename mem_manager
>
void sequence_kernel_2<T,mem_manager>::
move_to_pos (
node*& current_node,
unsigned long& current_pos,
unsigned long pos,
unsigned long size
) const
{
if ( current_pos > pos)
{
// number of hops in each direction needed to reach pos
unsigned long right = size + pos - current_pos;
unsigned long left = current_pos - pos;
current_pos = pos;
if (left < right)
{
// move left to position pos
for (; left > 0; --left)
current_node = current_node->left;
}
else
{
// move left to position pos
for (; right > 0; --right)
current_node = current_node->right;
}
}
else if (current_pos != pos)
{
// number of hops in each direction needed to reach pos
unsigned long right = pos - current_pos;
unsigned long left = size - pos + current_pos;
current_pos = pos;
if (left < right)
{
// move left to position pos
for (; left > 0; --left)
current_node = current_node->left;
}
else
{
// move left to position pos
for (; right > 0; --right)
current_node = current_node->right;
}
}
}
// ----------------------------------------------------------------------------------------
}
#endif // DLIB_SEQUENCE_KERNEl_2_