Skip to content

Commit 43c2934

Browse files
[SET] Add c support for clear (pyccel#1923)
Add C and Fortran support for the `set` method `clear`. Fixes pyccel#1918 --------- Co-authored-by: Emily Bourne <[email protected]>
1 parent ea865d2 commit 43c2934

File tree

5 files changed

+22
-13
lines changed

5 files changed

+22
-13
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ All notable changes to this project will be documented in this file.
3737
- #1876 : Add C support for indexing lists.
3838
- #1690 : Add C support for list method `pop()`.
3939
- #1877 : Add C Support for set method `pop()`.
40-
- #1917 : Add C and Fortran Support for set method `add()`.
40+
- #1917 : Add C and Fortran support for set method `add()`.
41+
- #1918 : Add C and Fortran support for set method `clear()`.
4142
- #1936 : Add missing C output for inline decorator example in documentation
4243
- #1937 : Optimise `pyccel.ast.basic.PyccelAstNode.substitute` method.
4344
- #1544 : Add support for `typing.TypeAlias`.

docs/builtin-functions.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ Python contains a limited number of builtin functions defined [here](https://doc
9797
| Method | Supported |
9898
|----------|-----------|
9999
| **`add`** | **Yes** |
100-
| `clear` | Python-only |
100+
| **`clear`** | **Yes** |
101101
| `copy` | Python-only |
102102
| `difference` | No |
103103
| `difference_update` | No |
@@ -107,7 +107,7 @@ Python contains a limited number of builtin functions defined [here](https://doc
107107
| `isdisjoint` | No |
108108
| `issubset` | No |
109109
| `issuperset` | No |
110-
| `pop` | Python-only |
110+
| `pop` | C and Python |
111111
| `remove` | Python-only |
112112
| `symmetric_difference` | No |
113113
| `symmetric_difference_update` | No |

pyccel/codegen/printing/ccode.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2617,6 +2617,11 @@ def _print_SetPop(self, expr):
26172617
set_var = self._print(ObjectAddress(expr.set_variable))
26182618
return f'{var_type}_pop({set_var})'
26192619

2620+
def _print_SetClear(self, expr):
2621+
var_type = self.get_declare_type(expr.set_variable)
2622+
set_var = self._print(ObjectAddress(expr.set_variable))
2623+
return f'{var_type}_clear({set_var});\n'
2624+
26202625
#=================== MACROS ==================
26212626

26222627
def _print_MacroShape(self, expr):

pyccel/codegen/printing/fcode.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1242,14 +1242,17 @@ def _print_ListAppend(self, expr):
12421242
arg = self._print(expr.args[0])
12431243
return f'call {target} % push_back({arg})\n'
12441244

1245-
12461245
#========================== Set Methods ================================#
12471246

12481247
def _print_SetAdd(self, expr):
12491248
var = self._print(expr.set_variable)
12501249
insert_obj = self._print(expr.args[0])
12511250
return f'call {var} % insert( {insert_obj} )\n'
12521251

1252+
def _print_SetClear(self, expr):
1253+
var = self._print(expr.set_variable)
1254+
return f'call {var} % clear()\n'
1255+
12531256
#========================== Numpy Elements ===============================#
12541257

12551258
def _print_NumpySum(self, expr):

tests/epyccel/test_epyccel_sets.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,32 +47,32 @@ def add_element_range():
4747
python_result = add_element_range()
4848
assert python_result == pyccel_result
4949

50-
def test_clear_int(python_only_language):
50+
def test_clear_int(language):
5151
def clear_int():
5252
se = {1,2,4,5}
5353
se.clear()
54-
return se
55-
epyccel_clear = epyccel(clear_int, language = python_only_language)
54+
return len(se)
55+
epyccel_clear = epyccel(clear_int, language = language)
5656
pyccel_result = epyccel_clear()
5757
python_result = clear_int()
5858
assert python_result == pyccel_result
5959

60-
def test_clear_float(python_only_language):
60+
def test_clear_float(language):
6161
def clear_float():
6262
se = {7.2, 2.1, 9.8, 6.4}
6363
se.clear()
64-
return se
65-
epyccel_clear = epyccel(clear_float, language = python_only_language)
64+
return len(se)
65+
epyccel_clear = epyccel(clear_float, language = language)
6666
pyccel_result = epyccel_clear()
6767
python_result = clear_float()
6868
assert python_result == pyccel_result
6969

70-
def test_clear_complex(python_only_language):
70+
def test_clear_complex(language):
7171
def clear_complex():
7272
se = {3j, 6j, 2j}
7373
se.clear()
74-
return se
75-
epyccel_clear = epyccel(clear_complex, language = python_only_language)
74+
return len(se)
75+
epyccel_clear = epyccel(clear_complex, language = language)
7676
pyccel_result = epyccel_clear()
7777
python_result = clear_complex()
7878
assert python_result == pyccel_result

0 commit comments

Comments
 (0)