Skip to content

Commit 5fc88e7

Browse files
committed
feat: add min/max methods to List and Tuple objects
1 parent c46bf8b commit 5fc88e7

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

argon/vm/datatype/list.cpp

+40
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
#include <argon/vm/runtime.h>
88

9+
#include <argon/vm/datatype/support/common.h>
10+
911
#include <argon/vm/datatype/boolean.h>
1012
#include <argon/vm/datatype/bounds.h>
1113
#include <argon/vm/datatype/option.h>
@@ -114,6 +116,42 @@ ARGON_METHOD(list_insert, insert,
114116
return ok ? (ArObject *) IncRef(self) : nullptr;
115117
}
116118

119+
ARGON_METHOD(list_max, max,
120+
"Returns the item with the highest value.\n"
121+
"\n"
122+
"- Returns: Highest value.\n"
123+
"\n"
124+
"# SEE\n"
125+
"- min\n",
126+
nullptr, false, false) {
127+
auto *self = (List *) _self;
128+
ArObject *max = nullptr;
129+
130+
std::shared_lock _(self->rwlock);
131+
132+
support::MaxMin(self->objects, &max, self->length, false);
133+
134+
return max;
135+
}
136+
137+
ARGON_METHOD(list_min, min,
138+
"Returns the item with the lowest value.\n"
139+
"\n"
140+
"- Returns: Lowest value.\n"
141+
"\n"
142+
"# SEE\n"
143+
"- max\n",
144+
nullptr, false, false) {
145+
auto *self = (List *) _self;
146+
ArObject *min = nullptr;
147+
148+
std::shared_lock _(self->rwlock);
149+
150+
support::MaxMin(self->objects, &min, self->length, true);
151+
152+
return min;
153+
}
154+
117155
ARGON_METHOD(list_pop, pop,
118156
"Remove and returns the item at the end of the list.\n"
119157
"\n"
@@ -193,6 +231,8 @@ const FunctionDef list_methods[] = {
193231
list_extend,
194232
list_find,
195233
list_insert,
234+
list_max,
235+
list_min,
196236
list_pop,
197237
list_remove,
198238
list_reverse,

argon/vm/datatype/support/common.h

+27
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#define ARGON_VM_DATATYPE_SUPPORT_COMMON_H_
77

88
#include <argon/vm/datatype/arobject.h>
9+
#include <argon/vm/datatype/boolean.h>
910
#include <argon/vm/datatype/list.h>
1011

1112
#include <argon/vm/datatype/support/byteops.h>
@@ -134,6 +135,32 @@ namespace argon::vm::datatype::support {
134135

135136
return (ArObject *) ret;
136137
}
138+
139+
inline bool MaxMin(ArObject **list, ArObject **out, ArSize length, bool min) {
140+
auto mode = min ? CompareMode::LE : CompareMode::GR;
141+
*out = nullptr;
142+
143+
if (length == 0) {
144+
ErrorFormat(kValueError[0], "%s on empty sequence", min ? "min" : "max");
145+
146+
return false;
147+
}
148+
149+
ArObject *ret = *list;
150+
151+
for (ArSize i = 1; i < length; i++) {
152+
auto *res = Compare(ret, list[i], mode);
153+
if (res == nullptr)
154+
return false;
155+
156+
if (res == (ArObject *) False)
157+
ret = list[i];
158+
}
159+
160+
*out = IncRef(ret);
161+
162+
return true;
163+
}
137164
}
138165

139166
#endif // !ARGON_VM_DATATYPE_SUPPORT_COMMON_H_

argon/vm/datatype/tuple.cpp

+36
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
#include <argon/vm/runtime.h>
66

7+
#include <argon/vm/datatype/support/common.h>
8+
79
#include <argon/vm/datatype/boolean.h>
810
#include <argon/vm/datatype/bounds.h>
911
#include <argon/vm/datatype/decimal.h>
@@ -47,10 +49,44 @@ ARGON_METHOD(tuple_find, find,
4749
return (ArObject *) IntNew(-1);
4850
}
4951

52+
ARGON_METHOD(tuple_max, max,
53+
"Returns the item with the highest value.\n"
54+
"\n"
55+
"- Returns: Highest value.\n"
56+
"\n"
57+
"# SEE\n"
58+
"- min\n",
59+
nullptr, false, false) {
60+
auto *self = (Tuple *) _self;
61+
ArObject *max = nullptr;
62+
63+
support::MaxMin(self->objects, &max, self->length, false);
64+
65+
return max;
66+
}
67+
68+
ARGON_METHOD(tuple_min, min,
69+
"Returns the item with the lowest value.\n"
70+
"\n"
71+
"- Returns: Lowest value.\n"
72+
"\n"
73+
"# SEE\n"
74+
"- max\n",
75+
nullptr, false, false) {
76+
auto *self = (Tuple *) _self;
77+
ArObject *min = nullptr;
78+
79+
support::MaxMin(self->objects, &min, self->length, true);
80+
81+
return min;
82+
}
83+
5084
const FunctionDef tuple_methods[] = {
5185
tuple_tuple,
5286

5387
tuple_find,
88+
tuple_max,
89+
tuple_min,
5490
ARGON_METHOD_SENTINEL
5591
};
5692

0 commit comments

Comments
 (0)