-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathE12.h
85 lines (74 loc) · 2.31 KB
/
E12.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
#ifndef E12_H
#define E12_H
#include <initializer_list>
#include <vector>
#include <string>
#include <iostream>
#include <memory>
#include <exception>
using std::cin;
using std::cout;
using std::endl;
using std::vector;
using std::string;
using std::shared_ptr;
using std::make_shared;
using std::initializer_list;
using std::out_of_range;
using std::weak_ptr;
template<typename> class BlobPtr;
template<typename> class Blob;
template<typename T> bool operator==(const Blob<T>&, const Blob<T>&);
template<typename T> class Blob{
friend class BlobPtr<T>;
friend bool operator==<T>(const Blob<T>&, const Blob<T>&);
public:
typedef T value_type;
typedef typename vector<T>::size_type size_type;
Blob();
Blob(initializer_list<T> il);
size_type size() const { return data->size(); }
bool empty() const { return data->empty(); }
void push_back(const T &t) { data->push_back(t); }
void push_back(const T &&t) { data->push_back(std::move(t)); }
void pop_back();
T &front() { return data->front(); }
T &back() { return data->back(); }
T& operator[] (size_type i);
private:
shared_ptr<vector<T>> data;
void check(size_type i, const string &msg) const;
};
template<typename T> void Blob<T>::check(size_type i, const string &msg) const {
if (i >= data->size()){
throw out_of_range(msg);
}
}
template<typename T> T& Blob<T>::operator[](size_type i){
check(i, "subscript out of");
return (*data)[i];
}
template<typename T> Blob<T>::Blob():data(make_shared<vector<T>>()) {}
template<typename T> Blob<T>::Blob(initializer_list<T> il):data(make_shared<vector<T>>(il)) {}
template <typename T> class BlobPtr{
public:
BlobPtr():curr(0) {}
BlobPtr(Blob<T> &a, size_t sz = 0):
wptr(a.data), curr(sz) {}
T& operator*() const { auto p = check(curr, "deference past end"); return (*p)[curr]; }
BlobPtr &operator++();
BlobPtr &operator--();
BlobPtr operator++(int);
BlobPtr operator--(int);
private:
shared_ptr<vector<T>> check(size_t, const string&) const;
weak_ptr<vector<T>> wptr;
size_t curr;
};
template<typename T>
BlobPtr<T> BlobPtr<T>::operator++(int){
BlobPtr ret = *this;
++*this;
return ret;
}
#endif