ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/lsys/util.h
Revision: 1.2
Committed: Wed Nov 7 12:50:17 2018 UTC (5 years, 5 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.1: +3 -3 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 /*
2 * utilities...
3 */
4
5 #ifndef UTIL_H
6 #define UTIL_H
7
8 #include <cmath>
9 #include <string>
10 #include <sstream>
11 #include <iostream>
12 #include <vector>
13 #include <exception>
14
15 using namespace std;
16
17 /* these defines belongs into config.h */
18 #define abstract = 0
19
20 #define t_no noexcept
21 #define t_err // can only throw error
22
23 inline bool equal (double a, double b)
24 {
25 return abs (a - b) <= a * 1e-6;
26 }
27
28 template<typename T>
29 class shared;
30
31 template<typename T>
32 bool operator <(const shared<T> &a, const shared<T> &b) t_no;
33
34 template<typename T>
35 class shared {
36 struct storage : T {
37 int shared;
38
39 storage () t_no : shared(0) {};
40 storage (const storage &s) t_no : T(s), shared(0) {};
41 };
42
43 protected:
44 mutable storage *obj;
45
46 void unshare() const t_no
47 { if (obj->shared) { obj->shared--; obj = new storage(*obj); } };
48
49 public:
50 typedef typename T::value_type data_type;
51 typedef typename T::const_iterator const_iterator;
52 typedef typename T::iterator iterator;
53 typedef typename T::const_reference const_reference;
54 typedef typename T::reference reference;
55 typedef typename T::size_type size_type;
56
57 shared() t_no
58 { obj = new storage; };
59
60 shared(const shared<T> &v) t_no
61 { obj = v.obj; obj->shared++; };
62
63 shared<T> &operator =(const shared<T> &v) t_no
64 {
65 v.obj->shared++;
66 if (obj->shared)
67 obj->shared--;
68 else
69 delete obj;
70
71 obj = v.obj;
72 return *this;
73 };
74
75 ~shared() t_no
76 { if (obj->shared) obj->shared--; else delete obj; };
77
78 void erase () t_no
79 {
80 if (obj->shared)
81 {
82 obj->shared--;
83 obj = new storage;
84 }
85 else
86 obj->erase (obj->begin(), obj->end ());
87 };
88
89 const_iterator begin () const t_no { return obj->begin (); };
90 const_iterator end () const t_no { return obj->end (); };
91 iterator begin () t_no { unshare (); return obj->begin (); };
92 iterator end () t_no { unshare (); return obj->end (); };
93 size_type size () const t_no { return obj->size (); };
94 bool empty () const t_no { return obj->empty (); };
95
96 const T& operator ->() const t_no { return *obj; };
97 T& operator ->() t_no { unshare (); return *obj; };
98 const T& operator * () const t_no { return *obj; };
99 T& operator * () t_no { unshare (); return *obj; };
100 operator const T& () const t_no { return *obj; };
101 operator T& () t_no { unshare (); return *obj; };
102
103 void insert (iterator a, iterator b, iterator c) t_no
104 { unshare (); obj->insert (a, b, c); };
105
106 friend bool operator < <>(const shared<T> &a, const shared<T> &b) t_no;
107 };
108
109 template<typename T>
110 bool operator <(const shared<T> &a, const shared<T> &b) t_no
111 {
112 if (a.obj == b.obj)
113 return false;
114 else if (a.size () < b.size ())
115 return true;
116 else if (a.size () == b.size ())
117 {
118 typename shared<T>::const_iterator i, j;
119
120 for (i = a.begin (), j = b.begin (); i != a.end (); ++i, ++j)
121 if (*i < *j)
122 return true;
123 }
124
125 return false;
126 }
127
128 template<typename T>
129 struct shared_vector : public shared< vector<T> > {
130 T &operator [](int idx) t_no
131 { shared< vector<T> >::unshare (); return shared< vector<T> >::obj->begin()+idx; };
132
133 void push_back (typename shared_vector<T>::const_reference arg) t_no
134 { shared< vector<T> >::unshare (); shared< vector<T> >::obj->push_back (arg); };
135
136 };
137
138 struct error : exception {
139 string msg;
140
141 const char *what () const t_no
142 { return msg.c_str (); };
143
144 error (const string &s) t_no : msg(s) {};
145 error (const string &s, int line) t_no;
146 error (const string &s, const class module &m) t_no;
147
148 ~error () t_no
149 { };
150
151 template<typename T>
152 error &operator <<(const T &s) t_no
153 { msg.append (s); return *this; };
154 };
155
156 inline ostream &operator <<(ostream &o, const error &e) t_no
157 { return o << e.what () << endl; }
158
159 #endif
160