-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtraits.affine
More file actions
180 lines (153 loc) · 3.87 KB
/
Copy pathtraits.affine
File metadata and controls
180 lines (153 loc) · 3.87 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
// SPDX-License-Identifier: PMPL-1.0-or-later
// SPDX-FileCopyrightText: 2024-2025 hyperpolymath
// Core traits for AffineScript standard library
/// Trait for types that can be compared for equality
pub trait Eq {
/// Check if two values are equal
pub fn eq(ref self, ref other: Self) -> Bool;
/// Check if two values are not equal (has default implementation)
pub fn ne(ref self, ref other: Self) -> Bool {
return !self.eq(other);
}
}
/// Ordering result for comparisons
pub enum Ordering {
Less,
Equal,
Greater
}
/// Trait for types that can be ordered (requires Eq)
pub trait Ord: Eq {
/// Compare two values and return ordering
pub fn cmp(ref self, ref other: Self) -> Ordering;
/// Check if self < other
pub fn lt(ref self, ref other: Self) -> Bool {
match self.cmp(other) {
Ordering::Less => return true,
_ => return false
}
}
/// Check if self <= other
pub fn le(ref self, ref other: Self) -> Bool {
match self.cmp(other) {
Ordering::Less => return true,
Ordering::Equal => return true,
_ => return false
}
}
/// Check if self > other
pub fn gt(ref self, ref other: Self) -> Bool {
match self.cmp(other) {
Ordering::Greater => return true,
_ => return false
}
}
/// Check if self >= other
pub fn ge(ref self, ref other: Self) -> Bool {
match self.cmp(other) {
Ordering::Greater => return true,
Ordering::Equal => return true,
_ => return false
}
}
}
/// Trait for types that can be hashed
pub trait Hash {
/// Compute hash value for this type
pub fn hash(ref self) -> Int;
}
/// Trait for types that can be converted to string
pub trait Display {
/// Convert value to string representation
pub fn to_string(ref self) -> String;
}
/// Trait for types that can be debugged (detailed output)
pub trait Debug {
/// Convert value to debug string representation
pub fn debug(ref self) -> String;
}
/// Trait for types that can be cloned
pub trait Clone {
/// Create a copy of this value
pub fn clone(ref self) -> Self;
}
/// Trait for types that can be converted to another type
pub trait Into[T] {
/// Convert self into target type
pub fn into(own self) -> T;
}
/// Trait for types that can be created from another type
pub trait From[T] {
/// Create self from source type
pub fn from(own value: T) -> Self;
}
/// Trait for types that have a default value
pub trait Default {
/// Create default value
pub fn default() -> Self;
}
/// Trait for iterators
pub trait Iterator {
type Item;
/// Get next item from iterator
pub fn next(mut self) -> Option[Item];
/// Count remaining items
pub fn count(mut self) -> Int {
let mut n = 0;
while let Some(_) = self.next() {
n = n + 1;
}
return n;
}
/// Collect into a list. (`Vec` is not a stdlib type; the stdlib
/// builds lists with `[]` + `++`, as in prelude/option/result —
/// #135 slice 4-traits.)
pub fn collect(mut self) -> [Item] {
let mut result = [];
while let Some(item) = self.next() {
result = result ++ [item];
}
return result;
}
}
// Implementations for built-in types
impl Eq for Int {
pub fn eq(ref self, ref other: Int) -> Bool {
return *self == *other;
}
}
impl Ord for Int {
pub fn cmp(ref self, ref other: Int) -> Ordering {
if *self < *other {
return Ordering::Less;
} else if *self > *other {
return Ordering::Greater;
} else {
return Ordering::Equal;
}
}
}
impl Hash for Int {
pub fn hash(ref self) -> Int {
return *self;
}
}
impl Display for Int {
pub fn to_string(ref self) -> String {
return int_to_string(*self);
}
}
impl Eq for Bool {
pub fn eq(ref self, ref other: Bool) -> Bool {
return *self == *other;
}
}
impl Display for Bool {
pub fn to_string(ref self) -> Bool {
if *self {
return "true";
} else {
return "false";
}
}
}