Skip to content
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

C: Implement Dict pop #1568

Merged
merged 3 commits into from
Mar 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add dict_pop
  • Loading branch information
Smit-create committed Mar 9, 2023
commit 386da87b01530e08aa7dfaf6905a1f01423da1d4
19 changes: 19 additions & 0 deletions src/libasr/codegen/asr_to_c_cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -1179,8 +1179,27 @@ R"(#include <stdio.h>
ASR::ttype_t* t_ttype = ASRUtils::expr_type(x.m_arg);
ASR::Dict_t* t = ASR::down_cast<ASR::Dict_t>(t_ttype);
std::string dict_len_fun = c_ds_api->get_dict_len_func(t);
bracket_open++;
self().visit_expr(*x.m_arg);
src = dict_len_fun + "(&" + src + ")";
bracket_open--;
}

void visit_DictPop(const ASR::DictPop_t& x) {
if ( x.m_value ) {
self().visit_expr(*x.m_value);
return ;
}
ASR::ttype_t* t_ttype = ASRUtils::expr_type(x.m_a);
ASR::Dict_t* t = ASR::down_cast<ASR::Dict_t>(t_ttype);
std::string dict_pop_fun = c_ds_api->get_dict_pop_func(t);
bracket_open++;
self().visit_expr(*x.m_a);
std::string d = std::move(src);
self().visit_expr(*x.m_key);
std::string k = std::move(src);
src = dict_pop_fun + "(&" + d + ", " + k + ")";
bracket_open--;
}

void visit_ListItem(const ASR::ListItem_t& x) {
Expand Down
30 changes: 30 additions & 0 deletions src/libasr/codegen/c_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -1136,6 +1136,11 @@ class CCPPDSUtils {
return typecodeToDSfuncs[dict_type_code]["dict_len"];
}

std::string get_dict_pop_func(ASR::Dict_t* d_type) {
std::string dict_type_code = ASRUtils::get_type_code((ASR::ttype_t*)d_type, true);
return typecodeToDSfuncs[dict_type_code]["dict_pop"];
}

std::string get_dict_init_func(ASR::Dict_t* d_type) {
std::string dict_type_code = ASRUtils::get_type_code((ASR::ttype_t*)d_type, true);
return typecodeToDSfuncs[dict_type_code]["dict_init"];
Expand Down Expand Up @@ -1173,6 +1178,7 @@ class CCPPDSUtils {
dict_insert(dict_type, dict_struct_type, dict_type_code);
dict_get_item(dict_type, dict_struct_type, dict_type_code);
dict_len(dict_type, dict_struct_type, dict_type_code);
dict_pop(dict_type, dict_struct_type, dict_type_code);
dict_deepcopy(dict_type, dict_struct_type, dict_type_code);
return dict_struct_type;
}
Expand Down Expand Up @@ -1311,6 +1317,30 @@ class CCPPDSUtils {
generated_code += indent + "}\n\n";
}

void dict_pop(ASR::Dict_t *dict_type, std::string dict_struct_type,
std::string dict_type_code) {
std::string indent(indentation_level * indentation_spaces, ' ');
std::string tab(indentation_spaces, ' ');
std::string dict_pop_func = global_scope->get_unique_name("dict_pop_" + dict_type_code);
typecodeToDSfuncs[dict_type_code]["dict_pop"] = dict_pop_func;
std::string key = CUtils::get_c_type_from_ttype_t(dict_type->m_key_type);
std::string val = CUtils::get_c_type_from_ttype_t(dict_type->m_value_type);
std::string signature = val + " " + dict_pop_func + "(" + dict_struct_type + "* x, " + key + " k)";
func_decls += indent + "inline " + signature + ";\n";
signature = indent + signature;
generated_code += indent + signature + " {\n";
generated_code += indent + tab + "int j = k % x->capacity;\n";
generated_code += indent + tab + "for(int i=0; i < x->capacity; i++) {\n";
generated_code += indent + tab + tab + "if (x->present[j] && x->key[j] == k) {\n";
generated_code += indent + tab + tab + tab + "x->present[j] = false;\n";
generated_code += indent + tab + tab + tab + "return x->value[j];\n";
generated_code += indent + tab + tab + "}\n";
generated_code += indent + tab + tab + "j = (j+1)%x->capacity;\n";
generated_code += indent + tab + "}\n";
generated_code += indent + tab + "printf(\"Key not found\\n\"); exit(1);\n";
generated_code += indent + "}\n\n";
}

void dict_deepcopy(ASR::Dict_t *dict_type, std::string dict_struct_type,
std::string dict_type_code) {
std::string indent(indentation_level * indentation_spaces, ' ');
Expand Down