-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmarter_list.hh
69 lines (58 loc) · 2.05 KB
/
smarter_list.hh
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
#ifndef SMARTER_LIST_H
#define SMARTER_LIST_H
#include <iostream>
#include <memory>
#include <vector>
namespace smarter_list {
struct ListNode {
// This declaration is needed.
ListNode() = default;
ListNode(::std::string namestr) : name(namestr) {}
ListNode(const ListNode *ln);
// Default did not move next pointer.
ListNode(ListNode &&ln) : name(ln.name), next(std::move(ln.next)) {}
ListNode &operator=(ListNode &&ln) = default;
bool empty() const { return (name.empty() && !next); }
std::string name;
std::unique_ptr<ListNode> next;
};
bool operator==(const ListNode &a, const ListNode &b);
bool operator!=(const ListNode &a, const ListNode &b);
class SmarterList {
public:
SmarterList() : head_(std::unique_ptr<ListNode>()), cursor_(nullptr) {}
SmarterList(ListNode &ln)
: head_(std::make_unique<ListNode>(std::move(ln))), cursor_(head_.get()) {
}
SmarterList(const SmarterList &sl) = delete;
SmarterList(SmarterList &&sl) = default;
SmarterList(::std::vector<::std::string> strvec);
void Reverse();
// Reset the list cursor to the head.
// Only changes cursor_, which is mutable.
void reset() const { cursor_ = head_.get(); }
ListNode *current() const { return cursor_; }
// Retrieve the list head.
ListNode *begin() const {
reset();
return cursor_;
}
bool empty() const { return (nullptr == head_); }
void operator+(const ListNode &ln);
std::pair<int, ListNode> operator[](const int i);
// These operators change only cursor_.
ListNode *operator++() const;
ListNode *operator--() const;
friend ::std::ostream &operator<<(::std::ostream &out, const SmarterList &sl);
private:
std::unique_ptr<ListNode> head_;
// Mutable cursor_ provides a "view" of the list without changing the
// contents.
mutable ListNode *cursor_;
void Prepend(std::unique_ptr<ListNode> ln);
};
bool operator==(const SmarterList &a, const SmarterList &b);
bool operator!=(const SmarterList &a, const SmarterList &b);
::std::ostream &operator<<(::std::ostream &out, const SmarterList &sl);
} // namespace smarter_list
#endif