Skip to content

Commit 196151b

Browse files
committed
list.reserve to reserve
1 parent a649c9a commit 196151b

File tree

5 files changed

+37
-44
lines changed

5 files changed

+37
-44
lines changed

integration_tests/test_list_reserve.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
1-
from lpython import i32, f64
1+
from lpython import i32, f64, reserve
22

33
def test_list_reserve():
44
l1: list[i32] = []
55
l2: list[list[tuple[f64, str, tuple[i32, f64]]]] = []
66
i: i32
77

8-
l1.reserve(100)
8+
reserve(l1, 100)
99
for i in range(50):
1010
l1.append(i)
1111
assert len(l1) == i + 1
1212

13-
l1.reserve(150)
13+
reserve(l1, 150)
1414

1515
for i in range(50):
1616
l1.pop(0)
1717
assert len(l1) == 49 - i
1818

19-
l2.reserve(100)
19+
reserve(l2, 100)
2020
for i in range(50):
2121
l2.append([(f64(i * i), str(i), (i, f64(i + 1))), (f64(i), str(i), (i, f64(i)))])
2222
assert len(l2) == i + 1
2323

24-
l2.reserve(150)
24+
reserve(l2, 150)
2525

2626
for i in range(50):
2727
l2.pop(0)

src/libasr/codegen/asr_to_llvm.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1680,7 +1680,8 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
16801680
tmp = list_api->pop_position(plist, pos, asr_el_type, module.get(), name2memidx);
16811681
}
16821682

1683-
void generate_ListReserve(ASR::expr_t* m_arg, ASR::expr_t* m_ele) {
1683+
void generate_Reserve(ASR::expr_t* m_arg, ASR::expr_t* m_ele) {
1684+
// For now, this only handles lists
16841685
ASR::ttype_t* asr_el_type = ASRUtils::get_contained_type(ASRUtils::expr_type(m_arg));
16851686
int64_t ptr_loads_copy = ptr_loads;
16861687
ptr_loads = 0;
@@ -1808,8 +1809,8 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
18081809
}
18091810
break;
18101811
}
1811-
case ASRUtils::IntrinsicFunctions::ListReserve: {
1812-
generate_ListReserve(x.m_args[0], x.m_args[1]);
1812+
case ASRUtils::IntrinsicFunctions::Reserve: {
1813+
generate_Reserve(x.m_args[0], x.m_args[1]);
18131814
break;
18141815
}
18151816
case ASRUtils::IntrinsicFunctions::DictKeys: {

src/libasr/pass/intrinsic_function_registry.h

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ enum class IntrinsicFunctions : int64_t {
4646
Partition,
4747
ListReverse,
4848
ListPop,
49-
ListReserve,
49+
Reserve,
5050
DictKeys,
5151
DictValues,
5252
SetAdd,
@@ -103,7 +103,7 @@ inline std::string get_intrinsic_name(int x) {
103103
INTRINSIC_NAME_CASE(Partition)
104104
INTRINSIC_NAME_CASE(ListReverse)
105105
INTRINSIC_NAME_CASE(ListPop)
106-
INTRINSIC_NAME_CASE(ListReserve)
106+
INTRINSIC_NAME_CASE(Reserve)
107107
INTRINSIC_NAME_CASE(DictKeys)
108108
INTRINSIC_NAME_CASE(DictValues)
109109
INTRINSIC_NAME_CASE(SetAdd)
@@ -1264,51 +1264,54 @@ static inline ASR::asr_t* create_ListPop(Allocator& al, const Location& loc,
12641264

12651265
} // namespace ListPop
12661266

1267-
namespace ListReserve {
1267+
namespace Reserve {
12681268

12691269
static inline void verify_args(const ASR::IntrinsicFunction_t& x, diag::Diagnostics& diagnostics) {
1270-
ASRUtils::require_impl(x.n_args == 2, "Call to list.reserve must have exactly one argument",
1270+
ASRUtils::require_impl(x.n_args == 2, "Call to reserve must have exactly one argument",
12711271
x.base.base.loc, diagnostics);
12721272
ASRUtils::require_impl(ASR::is_a<ASR::List_t>(*ASRUtils::expr_type(x.m_args[0])),
1273-
"Argument to list.reserve must be of list type",
1273+
"First argument to reserve must be of list type",
12741274
x.base.base.loc, diagnostics);
12751275
ASRUtils::require_impl(ASR::is_a<ASR::Integer_t>(*ASRUtils::expr_type(x.m_args[1])),
1276-
"Argument to list.reserve must be an integer",
1276+
"Second argument to reserve must be an integer",
12771277
x.base.base.loc, diagnostics);
12781278
ASRUtils::require_impl(x.m_type == nullptr,
1279-
"Return type of list.reserve must be empty",
1279+
"Return type of reserve must be empty",
12801280
x.base.base.loc, diagnostics);
12811281
}
12821282

1283-
static inline ASR::expr_t *eval_list_reserve(Allocator &/*al*/,
1283+
static inline ASR::expr_t *eval_reserve(Allocator &/*al*/,
12841284
const Location &/*loc*/, Vec<ASR::expr_t*>& /*args*/) {
12851285
// TODO: To be implemented for ListConstant expression
12861286
return nullptr;
12871287
}
12881288

1289-
static inline ASR::asr_t* create_ListReserve(Allocator& al, const Location& loc,
1289+
static inline ASR::asr_t* create_Reserve(Allocator& al, const Location& loc,
12901290
Vec<ASR::expr_t*>& args,
12911291
const std::function<void (const std::string &, const Location &)> err) {
12921292
if (args.size() != 2) {
1293-
err("Call to list.reserve must have exactly one argument", loc);
1293+
err("Call to reserve must have exactly two argument", loc);
1294+
}
1295+
if (!ASR::is_a<ASR::List_t>(*ASRUtils::expr_type(args[0]))) {
1296+
err("First argument to reserve must be of list type", loc);
12941297
}
12951298
if (!ASR::is_a<ASR::Integer_t>(*ASRUtils::expr_type(args[1]))) {
1296-
err("Argument to list.reserve must be an integer", loc);
1299+
err("Second argument to reserve must be an integer", loc);
12971300
}
12981301

12991302
Vec<ASR::expr_t*> arg_values;
13001303
arg_values.reserve(al, args.size());
13011304
for( size_t i = 0; i < args.size(); i++ ) {
13021305
arg_values.push_back(al, ASRUtils::expr_value(args[i]));
13031306
}
1304-
ASR::expr_t* compile_time_value = eval_list_reserve(al, loc, arg_values);
1307+
ASR::expr_t* compile_time_value = eval_reserve(al, loc, arg_values);
13051308
return ASR::make_Expr_t(al, loc,
13061309
ASRUtils::EXPR(ASRUtils::make_IntrinsicFunction_t_util(al, loc,
1307-
static_cast<int64_t>(ASRUtils::IntrinsicFunctions::ListReserve),
1310+
static_cast<int64_t>(ASRUtils::IntrinsicFunctions::Reserve),
13081311
args.p, args.size(), 0, nullptr, compile_time_value)));
13091312
}
13101313

1311-
} // namespace ListReserve
1314+
} // namespace Reserve
13121315

13131316
namespace DictKeys {
13141317

@@ -3172,8 +3175,8 @@ namespace IntrinsicFunctionRegistry {
31723175
{nullptr, &DictValues::verify_args}},
31733176
{static_cast<int64_t>(ASRUtils::IntrinsicFunctions::ListPop),
31743177
{nullptr, &ListPop::verify_args}},
3175-
{static_cast<int64_t>(ASRUtils::IntrinsicFunctions::ListReserve),
3176-
{nullptr, &ListReserve::verify_args}},
3178+
{static_cast<int64_t>(ASRUtils::IntrinsicFunctions::Reserve),
3179+
{nullptr, &Reserve::verify_args}},
31773180
{static_cast<int64_t>(ASRUtils::IntrinsicFunctions::SetAdd),
31783181
{nullptr, &SetAdd::verify_args}},
31793182
{static_cast<int64_t>(ASRUtils::IntrinsicFunctions::SetRemove),
@@ -3256,8 +3259,8 @@ namespace IntrinsicFunctionRegistry {
32563259
"list.reverse"},
32573260
{static_cast<int64_t>(ASRUtils::IntrinsicFunctions::ListPop),
32583261
"list.pop"},
3259-
{static_cast<int64_t>(ASRUtils::IntrinsicFunctions::ListReserve),
3260-
"list.reserve"},
3262+
{static_cast<int64_t>(ASRUtils::IntrinsicFunctions::Reserve),
3263+
"reserve"},
32613264
{static_cast<int64_t>(ASRUtils::IntrinsicFunctions::DictKeys),
32623265
"dict.keys"},
32633266
{static_cast<int64_t>(ASRUtils::IntrinsicFunctions::DictValues),
@@ -3342,7 +3345,7 @@ namespace IntrinsicFunctionRegistry {
33423345
{"list.index", {&ListIndex::create_ListIndex, &ListIndex::eval_list_index}},
33433346
{"list.reverse", {&ListReverse::create_ListReverse, &ListReverse::eval_list_reverse}},
33443347
{"list.pop", {&ListPop::create_ListPop, &ListPop::eval_list_pop}},
3345-
{"list.reserve", {&ListReserve::create_ListReserve, &ListReserve::eval_list_reserve}},
3348+
{"reserve", {&Reserve::create_Reserve, &Reserve::eval_reserve}},
33463349
{"dict.keys", {&DictKeys::create_DictKeys, &DictKeys::eval_dict_keys}},
33473350
{"dict.values", {&DictValues::create_DictValues, &DictValues::eval_dict_values}},
33483351
{"set.add", {&SetAdd::create_SetAdd, &SetAdd::eval_set_add}},

src/lpython/semantics/python_attribute_eval.h

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ struct AttributeHandler {
3030
{"list@pop", &eval_list_pop},
3131
{"list@clear", &eval_list_clear},
3232
{"list@insert", &eval_list_insert},
33-
{"list@reserve", &eval_list_reserve},
3433
{"set@pop", &eval_set_pop},
3534
{"set@add", &eval_set_add},
3635
{"set@remove", &eval_set_remove},
@@ -42,8 +41,7 @@ struct AttributeHandler {
4241

4342
modify_attr_set = {"list@append", "list@remove",
4443
"list@reverse", "list@clear", "list@insert", "list@pop",
45-
"list@reserve", "set@pop", "set@add", "set@remove",
46-
"dict@pop"};
44+
"set@pop", "set@add", "set@remove", "dict@pop"};
4745

4846
symbolic_attribute_map = {
4947
{"diff", &eval_symbolic_diff},
@@ -286,20 +284,6 @@ struct AttributeHandler {
286284
return make_ListClear_t(al, loc, s);
287285
}
288286

289-
static ASR::asr_t* eval_list_reserve(ASR::expr_t *s, Allocator &al, const Location &loc,
290-
Vec<ASR::expr_t*> &args, diag::Diagnostics &/*diag*/) {
291-
Vec<ASR::expr_t*> args_with_list;
292-
args_with_list.reserve(al, args.size() + 1);
293-
args_with_list.push_back(al, s);
294-
for(size_t i = 0; i < args.size(); i++) {
295-
args_with_list.push_back(al, args[i]);
296-
}
297-
ASRUtils::create_intrinsic_function create_function =
298-
ASRUtils::IntrinsicFunctionRegistry::get_create_function("list.reserve");
299-
return create_function(al, loc, args_with_list, [&](const std::string &msg, const Location &loc)
300-
{ throw SemanticError(msg, loc); });
301-
}
302-
303287
static ASR::asr_t* eval_set_pop(ASR::expr_t *s, Allocator &al, const Location &loc,
304288
Vec<ASR::expr_t*> &args, diag::Diagnostics &/*diag*/) {
305289
if (args.size() != 0) {

src/runtime/lpython/lpython.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,11 @@ def __lpython(*args, **kwargs):
760760
def bitnot(x, bitsize):
761761
return (~x) % (2 ** bitsize)
762762

763+
def reserve(data_structure, n):
764+
if isinstance(data_structure, list):
765+
data_structure = [None] * n
766+
# no-op
767+
763768
bitnot_u8 = lambda x: bitnot(x, 8)
764769
bitnot_u16 = lambda x: bitnot(x, 16)
765770
bitnot_u32 = lambda x: bitnot(x, 32)

0 commit comments

Comments
 (0)