-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtype_functions.h
More file actions
384 lines (251 loc) · 7.71 KB
/
Copy pathtype_functions.h
File metadata and controls
384 lines (251 loc) · 7.71 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
// type_functions.h
// Copyright (c) 2009 Alexander Stepanov and Paul McJones
//
// Permission to use, copy, modify, distribute and sell this software
// and its documentation for any purpose is hereby granted without
// fee, provided that the above copyright notice appear in all copies
// and that both that copyright notice and this permission notice
// appear in supporting documentation. The authors make no
// representations about the suitability of this software for any
// purpose. It is provided "as is" without express or implied
// warranty.
// Implementations of type functions for
// Elements of Programming
// by Alexander Stepanov and Paul McJones
// Addison-Wesley Professional, 2009
#ifndef TYPE_FUNCTIONS
#define TYPE_FUNCTIONS
#include "intrinsics.h"
// As explained in Appendix B.2, to allow the language defined above
// to compile as a valid C++ program, a few macros and structure
// definitions are necessary.
// Type functions
// Type functions are implemented using a C++ technique called a
// trait class. For each type function, say ValueType, we define a
// corresponding structure template, say value_type<T>. The structure
// template contains one typedef, named type by convention; if
// appropriate, a default can be provided in the base structure
// template:
// template<typename T>
// struct value_type
// {
// typedef T type;
// };
// To provide a convenient notation, we define a macro that extracts
// the typedef as the result of the type function (such a macro works
// only inside a template definition because of the use of the keyword
// typename):
// #define ValueType(T) typename value_type< T >::type
// We refine the global definition for a particular type by
// specializing:
// template<typename T>
// struct value_type<pointer(T)>
// {
// typedef T type;
// };
// Chapter 1 - Foundations
// Codomain : FunctionalProcedure -> Regular
template<typename T>
requires(FunctionalProcedure(T))
struct codomain_type;
#define Codomain(T) typename codomain_type< T >::type
// InputType : FunctionalProcedure x unsigned int -> Regular
template<typename T, int i>
requires(FunctionalProcedure(T))
struct input_type;
#define InputType(T, i) typename input_type< T, i >::type
// Domain : HomogeneousFunction -> Regular
#define Domain(T) InputType(T, 0)
// Chapter 2 - Transformations and their orbits
// See the discussion of distance types in section 2.2.
// DistanceType : Transformation -> Integer
template<typename F>
requires(Transformation(F))
struct distance_type;
// If all transformations on a type T have the same distance type,
// then DistanceType(T) is defined and returns that type.
// For any fixed-size type T, there is an integral type of the same
// size that is a valid distance type for T.
template<>
struct distance_type<int>
{
typedef unsigned int type;
};
template<>
struct distance_type<long long>
{
typedef unsigned long long type;
};
#define DistanceType(T) typename distance_type< T >::type
// Chapter 3 - Associative operations
template<typename T>
requires(Regular(T))
struct input_type<T (*)(T x, T y), 0>
{
typedef T type;
};
template<typename T>
requires(Regular(T))
struct codomain_type<T (*)(T x, T y)>
{
typedef T type;
};
template<typename T>
requires(Regular(T))
struct input_type<T (*)(const T& x, const T& y), 0>
{
typedef T type;
};
template<typename T>
requires(Regular(T))
struct codomain_type<T (*)(const T& x, const T& y)>
{
typedef T type;
};
// Chapter 4 - Linear orderings
// Domain type function for Predicate
template<typename T>
requires(Regular(T))
struct input_type<bool (*)(T x), 0>
{
typedef T type;
};
template<typename T>
requires(Regular(T))
struct input_type<bool (*)(const T& x), 0>
{
typedef T type;
};
// Domain type function for Relation
template<typename T>
requires(Regular(T))
struct input_type<bool (*)(T x, T y), 0>
{
typedef T type;
};
template<typename T>
requires(Regular(T))
struct input_type<bool (*)(const T& x, const T& y), 0>
{
typedef T type;
};
// Chapter 5 - Ordered algebraic structures
template<typename T>
requires(ArchimedeanMonoid(T))
struct quotient_type;
#define QuotientType(T) typename quotient_type< T >::type
// Lemma: For an integral type T, QuotientType(T) has to be at least as large as T.
template<>
struct quotient_type<int>
{
typedef int type;
};
template<>
struct quotient_type<long>
{
typedef long type;
};
// Chapter 6 - Iterators
// ValueType : Readable -> Regular
template<typename T>
requires(Regular(T))
struct value_type
{
typedef T type;
};
#define ValueType(T) typename value_type< T >::type
// DifferenceType : RandomAccessIterator -> Integer
template<typename I>
requires(RandomAccessIterator(I))
struct difference_type;
#define DifferenceType(T) typename difference_type< T >::type
// Chapter 7 - Coordinate structures
// WeightType : BifurcateCoordinate -> Integer
template<typename T>
requires(WeakBifurcateCoordinate(T))
struct weight_type;
#define WeightType(T) typename weight_type< T >::type
// Chapter 8 - Coordinates with mutable successors
// IteratorType : ForwardLinker -> ForwardIterator
// IteratorType : BackwardLinker -> BidirectionalIterator
template<typename T>
requires(ImplementsIteratorType(T))
struct iterator_type;
#define IteratorType(T) typename iterator_type< T >::type
// Chapter 10 - Rearrangements
// The IteratorTag concept has the following models:
struct iterator_tag {};
struct forward_iterator_tag {};
struct bidirectional_iterator_tag {};
struct indexed_iterator_tag {};
struct random_access_iterator_tag {};
// IteratorConcept : Iterator -> IteratorTag
template<typename T>
requires(Iterator(T))
struct iterator_concept
{
typedef iterator_tag concept;
};
#define IteratorConcept(T) typename iterator_concept< T >::concept
// Chapter 12 - Composite objects
// SizeType : Linearizeable -> Integer
template<typename W>
requires(Linearizable(W))
struct size_type;
#define SizeType(W) typename size_type<W>::type
// Size : ConstantSizeSequence -> Integer
// Size is a type attribute
template<typename S>
requires(ConstantSizeSequence(S))
struct size_value;
#define Size(S) size_value<S>::value
// BaseType : Position -> DynamicSequence
template<typename S>
requires(DynamicSequence(S))
struct base_type;
#define BaseType(T) typename base_type<T>::type
// concept BooleanType(T) means T represents a boolean value within the type system
// BooleanType has the following two models:
struct true_type {};
struct false_type {};
// NeedsConstruction : Regular -> BooleanType
// NeedsDestruction : Regular -> BooleanType
template<typename T>
requires(Regular(T))
struct needs_construction_type
{
typedef true_type type; // default
};
#define NeedsConstruction(T) typename needs_construction_type<T>::type
template<typename T>
requires(Regular(T))
struct needs_destruction_type
{
typedef true_type type; // default
};
#define NeedsDestruction(T) typename needs_destruction_type<T>::type
template<>
struct needs_construction_type<int>
{
typedef false_type type;
};
template<>
struct needs_destruction_type<int>
{
typedef false_type type;
};
// NeedsConstruction and NeedsDestruction should be similarly overloaded
// for every POD type
// CoordinateType : Container -> Coordinate
template<typename T>
requires(Container(T))
struct coordinate_type;
#define CoordinateType(T) typename coordinate_type<T>::type
// UnderlyingType : Regular -> Regular
template<typename T> requires(Regular(T))
struct underlying_type
{
typedef T type; // default
};
#define UnderlyingType(T) typename underlying_type<T>::type
#endif // TYPE_FUNCTIONS