-
Notifications
You must be signed in to change notification settings - Fork 0
/
MySet.mojo
162 lines (135 loc) · 4.61 KB
/
MySet.mojo
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
from tools import eq_dynamic_vector
struct MySet[T: DType](CollectionElement, Sized, Stringable):
var data: List[Scalar[T]]
var is_sorted: Bool
@always_inline("nodebug")
fn __init__(inout self):
self.data = List[SIMD[T, 1]]()
self.is_sorted = True
# trait CollectionElement
@always_inline("nodebug")
fn __copyinit__(inout self, existing: Self):
self.data.__copyinit__(existing.data)
self.is_sorted = existing.is_sorted
# trait CollectionElement
@always_inline("nodebug")
fn __moveinit__(inout self, owned existing: Self):
self.data = existing.data^
self.is_sorted = existing.is_sorted
# trait CollectionElement
@always_inline("nodebug")
fn __del__(owned self: Self):
pass
# trait Stringable
@always_inline("nodebug")
fn __str__(self) -> String:
var result: String = "["
var size = len(self.data)
if size > 0:
for i in range(size - 1):
result += str(self.data[i]) + ","
result += str(self.data[size - 1])
return result + "]"
# trait Sized
@always_inline("nodebug")
fn __len__(self) -> Int:
return len(self.data)
fn __eq__(self, other: Self) -> Bool:
if self.is_sorted and other.is_sorted:
return eq_dynamic_vector[T](self.data, other.data)
print("WARNING performance: MySet:__eq__ on unsorted data")
var a = self
a.sort()
var b = other
b.sort()
return a == b
fn __ne__(self, other: Self) -> Bool:
return not (self == other)
@always_inline("nodebug")
fn add[CHECK_CONTAINS: Bool = True](inout self, value: SIMD[T, 1]):
@parameter
if CHECK_CONTAINS:
if self.contains(value):
return
self.data.append(value)
self.is_sorted = False
@always_inline("nodebug")
fn add[CHECK_CONTAINS: Bool = True](inout self, values: MySet[T]):
for i in range(len(values)):
# this can be done more efficient
self.add[CHECK_CONTAINS](values.data[i])
fn remove(inout self, value: Scalar[T]):
var size = len(self.data)
for i in range(size):
if value == self.data[i]:
if i == (size - 1):
_ = self.data.pop()
# NOTE this set is still sorted!
else:
self.data[i] = self.data.pop()
self.is_sorted = False
return
@always_inline("nodebug")
fn remove(inout self, values: MySet[T]):
for i in range(len(values)):
# this can be done more efficient
self.remove(values.data[i])
@always_inline("nodebug")
fn sort(inout self):
if self.is_sorted:
return
else:
sort[T](self.data)
self.is_sorted = True
@always_inline("nodebug")
fn contains(self, value: Scalar[T]) -> Bool:
if self.is_sorted:
for i in range(len(self.data)):
if value <= self.data[i]:
return value == self.data[i]
else:
for i in range(len(self.data)):
if value == self.data[i]:
return True
return False
struct MySetStr(CollectionElement, Sized, Stringable):
var data: List[String]
@always_inline("nodebug")
fn __init__(inout self):
self.data = List[String]()
# trait CollectionElement
@always_inline("nodebug")
fn __copyinit__(inout self, existing: Self):
self.data.__copyinit__(existing.data)
# trait CollectionElement
@always_inline("nodebug")
fn __moveinit__(inout self, owned existing: Self):
self.data = existing.data^
# trait CollectionElement
@always_inline("nodebug")
fn __del__(owned self: Self):
pass
# trait Stringable
@always_inline("nodebug")
fn __str__(self) -> String:
var result: String = "["
var size = len(self.data)
if size > 0:
for i in range(size - 1):
result += str(self.data[i]) + ","
result += str(self.data[size - 1])
return result + "]"
# trait Sized
@always_inline("nodebug")
fn __len__(self) -> Int:
return len(self.data)
fn add(inout self, value: String):
for i in range(len(self.data)):
if value == self.data[i]:
return
self.data.append(value)
fn contains(self, value: String) -> Bool:
for i in range(len(self.data)):
if value == self.data[i]:
return True
return False