-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrb_method.h
More file actions
76 lines (65 loc) · 1.77 KB
/
rb_method.h
File metadata and controls
76 lines (65 loc) · 1.77 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
#ifndef _RB_METHOD_H_
#define _RB_METHOD_H_
#define RB_RANGE_EACH(from, to, num, exp) \
for (typeof(from) num = (from); num <= (to); ++num) \
exp
#define RB_TIMES(count, num, exp) \
RB_RANGE_EACH(0, (count) - 1, num, exp)
#define RB_EACH(array, length, elm, exp) \
for (typeof(&*(array)) elm = (array), end = elm + (length); elm < end; ++elm) \
exp
#define RB_EACH_ROTATE(array, length, from, elm, exp) \
RB_TIMES(length, i__, \
({ \
typeof(&*(array)) elm = (array) + ((from) + i__) % (length); \
exp; \
}))
#define RB_INJECT(array, length, init, sum, elm, func) \
({ \
typeof(init) sum = (init); \
RB_EACH(array, length, elm, sum = func); \
sum; \
})
#define RB_GENERAL_MAX_BY(array, length, elm, exp, opr) \
({ \
typeof(&*(array)) elm = array; \
typeof(exp) max__ = exp; \
typeof(&*(array)) ret__ = elm; \
RB_EACH((array) + 1, (length) - 1, elm, \
({ \
if (exp opr max__) { \
max__ = exp; \
ret__ = elm; \
} \
})); \
ret__; \
})
#define RB_MAX_BY(array, length, elm, exp) \
RB_GENERAL_MAX_BY(array, length, elm, exp, >)
#define RB_MIN_BY(array, length, elm, exp) \
RB_GENERAL_MAX_BY(array, length, elm, exp, <) \
#define RB_FIND(array, length, elm, cond) \
({ \
typeof(&*(array)) ret__ = NULL; \
RB_EACH(array, length, elm, \
({ \
if (cond) { \
ret__ = elm; \
break; \
} \
})); \
ret__; \
})
#define RB_FIND_ROTATE(array, length, from, elm, cond) \
({ \
typeof(&*(array)) ret__ = NULL; \
RB_EACH_ROTATE(array, length, from, elm, \
({ \
if (cond) { \
ret__ = elm; \
break; \
} \
})); \
ret__; \
})
#endif /* _RB_METHOD_H_ */