-
Notifications
You must be signed in to change notification settings - Fork 170
Add list.count #1676
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add list.count #1676
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
from lpython import i32, f64 | ||
|
||
def test_list_count(): | ||
i: i32 | ||
x: list[i32] = [] | ||
certik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
y: list[str] = [] | ||
z: list[tuple[i32, str, f64]] = [] | ||
|
||
for i in range(-5, 0): | ||
assert x.count(i) == 0 | ||
x.append(i) | ||
assert x.count(i) == 1 | ||
x.append(i) | ||
assert x.count(i) == 2 | ||
x.remove(i) | ||
assert x.count(i) == 1 | ||
|
||
assert x == [-5, -4, -3, -2, -1] | ||
|
||
for i in range(0, 5): | ||
assert x.count(i) == 0 | ||
x.append(i) | ||
assert x.count(i) == 1 | ||
|
||
assert x == [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4] | ||
|
||
while len(x) > 0: | ||
i = x[-1] | ||
x.remove(i) | ||
assert x.count(i) == 0 | ||
|
||
assert len(x) == 0 | ||
assert x.count(0) == 0 | ||
|
||
# str | ||
assert y.count('a') == 0 | ||
y = ['a', 'abc', 'a', 'b'] | ||
assert y.count('a') == 2 | ||
y.append('a') | ||
assert y.count('a') == 3 | ||
y.remove('a') | ||
assert y.count('a') == 2 | ||
|
||
# tuple, float | ||
assert z.count((i32(-1), 'b', f64(2))) == 0 | ||
z = [(i32(1), 'a', f64(2.01)), (i32(-1), 'b', f64(2)), (i32(1), 'a', f64(2.02))] | ||
assert z.count((i32(1), 'a', f64(2.00))) == 0 | ||
assert z.count((i32(1), 'a', f64(2.01))) == 1 | ||
z.append((i32(1), 'a', f64(2))) | ||
z.append((i32(1), 'a', f64(2.00))) | ||
assert z.count((i32(1), 'a', f64(2))) == 2 | ||
z.remove((i32(1), 'a', f64(2))) | ||
assert z.count((i32(1), 'a', f64(2.00))) == 1 | ||
|
||
test_list_count() |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2498,6 +2498,83 @@ namespace LCompilers { | |
return LLVM::CreateLoad(*builder, i); | ||
} | ||
|
||
llvm::Value* LLVMList::count(llvm::Value* list, llvm::Value* item, | ||
ASR::ttype_t* item_type, llvm::Module& module) { | ||
llvm::Type* pos_type = llvm::Type::getInt32Ty(context); | ||
llvm::Value* current_end_point = LLVM::CreateLoad(*builder, | ||
get_pointer_to_current_end_point(list)); | ||
llvm::AllocaInst *i = builder->CreateAlloca(pos_type, nullptr); | ||
LLVM::CreateStore(*builder, llvm::ConstantInt::get( | ||
context, llvm::APInt(32, 0)), i); | ||
llvm::AllocaInst *cnt = builder->CreateAlloca(pos_type, nullptr); | ||
LLVM::CreateStore(*builder, llvm::ConstantInt::get( | ||
context, llvm::APInt(32, 0)), cnt); | ||
llvm::Value* tmp = nullptr; | ||
|
||
/* Equivalent in C++: | ||
* int i = 0; | ||
* int cnt = 0; | ||
* while(end_point > i) { | ||
* if(list[i] == item) { | ||
* tmp = cnt+1; | ||
* cnt = tmp; | ||
* } | ||
* tmp = i+1; | ||
* i = tmp; | ||
* } | ||
*/ | ||
|
||
llvm::BasicBlock *loophead = llvm::BasicBlock::Create(context, "loop.head"); | ||
llvm::BasicBlock *loopbody = llvm::BasicBlock::Create(context, "loop.body"); | ||
llvm::BasicBlock *loopend = llvm::BasicBlock::Create(context, "loop.end"); | ||
|
||
// head | ||
llvm_utils->start_new_block(loophead); | ||
{ | ||
llvm::Value *cond = builder->CreateICmpSGT(current_end_point, | ||
LLVM::CreateLoad(*builder, i)); | ||
builder->CreateCondBr(cond, loopbody, loopend); | ||
} | ||
|
||
// body | ||
llvm_utils->start_new_block(loopbody); | ||
{ | ||
// if occurrence found, increment cnt | ||
llvm::Function *fn = builder->GetInsertBlock()->getParent(); | ||
llvm::BasicBlock *thenBB = llvm::BasicBlock::Create(context, "then", fn); | ||
llvm::BasicBlock *elseBB = llvm::BasicBlock::Create(context, "else"); | ||
llvm::BasicBlock *mergeBB = llvm::BasicBlock::Create(context, "ifcont"); | ||
|
||
llvm::Value* left_arg = read_item(list, LLVM::CreateLoad(*builder, i), | ||
false, module, LLVM::is_llvm_struct(item_type)); | ||
llvm::Value* cond = llvm_utils->is_equal_by_value(left_arg, item, module, item_type); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good that you figured it out on your own. |
||
builder->CreateCondBr(cond, thenBB, elseBB); | ||
builder->SetInsertPoint(thenBB); | ||
{ | ||
tmp = builder->CreateAdd( | ||
LLVM::CreateLoad(*builder, cnt), | ||
llvm::ConstantInt::get(context, llvm::APInt(32, 1))); | ||
LLVM::CreateStore(*builder, tmp, cnt); | ||
} | ||
builder->CreateBr(mergeBB); | ||
|
||
llvm_utils->start_new_block(elseBB); | ||
llvm_utils->start_new_block(mergeBB); | ||
|
||
// increment i | ||
tmp = builder->CreateAdd( | ||
LLVM::CreateLoad(*builder, i), | ||
llvm::ConstantInt::get(context, llvm::APInt(32, 1))); | ||
LLVM::CreateStore(*builder, tmp, i); | ||
} | ||
builder->CreateBr(loophead); | ||
|
||
// end | ||
llvm_utils->start_new_block(loopend); | ||
|
||
return LLVM::CreateLoad(*builder, cnt); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For now this is good. Later we should lift this into ASR and implement there, it will be a lot easier to maintain. |
||
|
||
void LLVMList::remove(llvm::Value* list, llvm::Value* item, | ||
ASR::ttype_t* item_type, llvm::Module& module) { | ||
llvm::Type* pos_type = llvm::Type::getInt32Ty(context); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from lpython import i32 | ||
|
||
def test_list_count_error(): | ||
a: list[i32] | ||
a = [1, 2, 3] | ||
a.count(1.0) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"basename": "asr-test_list_count-4b42498", | ||
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}", | ||
"infile": "tests/errors/test_list_count.py", | ||
"infile_hash": "01975bd7c4bba02fd811de536b218167da99b532fa955b7bf8339779", | ||
"outfile": null, | ||
"outfile_hash": null, | ||
"stdout": null, | ||
"stdout_hash": null, | ||
"stderr": "asr-test_list_count-4b42498.stderr", | ||
"stderr_hash": "f26efcc623b68ca43ef871eb01c8e3cbd1ae464baaa491c6e4969696", | ||
"returncode": 2 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
semantic error: Type mismatch in 'count', the types must be compatible | ||
--> tests/errors/test_list_count.py:6:13 | ||
| | ||
6 | a.count(1.0) | ||
| ^^^ type mismatch (found: 'f64', expected: 'i32') |
Uh oh!
There was an error while loading. Please reload this page.