Skip to content

Commit ea865d2

Browse files
[SET] Add c support for add (pyccel#1922)
Add C and Fortran support for the `set` method `add`. Fixes pyccel#1917 --------- Co-authored-by: Emily Bourne <[email protected]>
1 parent 5e36ad2 commit ea865d2

File tree

5 files changed

+28
-28
lines changed

5 files changed

+28
-28
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ 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()`.
4041
- #1936 : Add missing C output for inline decorator example in documentation
4142
- #1937 : Optimise `pyccel.ast.basic.PyccelAstNode.substitute` method.
4243
- #1544 : Add support for `typing.TypeAlias`.

docs/builtin-functions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ Python contains a limited number of builtin functions defined [here](https://doc
9696

9797
| Method | Supported |
9898
|----------|-----------|
99-
| `add` | Python-only |
99+
| **`add`** | **Yes** |
100100
| `clear` | Python-only |
101101
| `copy` | Python-only |
102102
| `difference` | No |

pyccel/codegen/printing/ccode.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2519,6 +2519,12 @@ def _print_CommentBlock(self, expr):
25192519
def _print_EmptyNode(self, expr):
25202520
return ''
25212521

2522+
def _print_SetAdd(self, expr):
2523+
var_type = self.get_declare_type(expr.set_variable)
2524+
set_var = self._print(ObjectAddress(expr.set_variable))
2525+
arg = self._print(expr.args[0])
2526+
return f'{var_type}_push({set_var}, {arg});\n'
2527+
25222528
#=================== OMP ==================
25232529

25242530
def _print_OmpAnnotatedComment(self, expr):

pyccel/codegen/printing/fcode.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,7 +1243,12 @@ def _print_ListAppend(self, expr):
12431243
return f'call {target} % push_back({arg})\n'
12441244

12451245

1246+
#========================== Set Methods ================================#
12461247

1248+
def _print_SetAdd(self, expr):
1249+
var = self._print(expr.set_variable)
1250+
insert_obj = self._print(expr.args[0])
1251+
return f'call {var} % insert( {insert_obj} )\n'
12471252

12481253
#========================== Numpy Elements ===============================#
12491254

tests/epyccel/test_epyccel_sets.py

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,33 @@
1616
def python_only_language(request):
1717
return request.param
1818

19-
def test_add_literal_int(python_only_language) :
19+
def test_add_literal_int(language) :
2020
def add_int():
2121
a = {1,3,45}
2222
a.add(4)
23-
return a
24-
epyc_add_element = epyccel(add_int, language = python_only_language)
23+
return len(a)
24+
epyc_add_element = epyccel(add_int, language = language)
2525
pyccel_result = epyc_add_element()
2626
python_result = add_int()
2727
assert python_result == pyccel_result
2828

29-
def test_add_literal_complex(python_only_language) :
29+
def test_add_literal_complex(language) :
3030
def add_complex():
3131
a = {6j,7j,8j}
3232
a.add(9j)
33-
return a
34-
epyc_add_element = epyccel(add_complex, language = python_only_language)
33+
return len(a)
34+
epyc_add_element = epyccel(add_complex, language = language)
3535
pyccel_result = epyc_add_element()
3636
python_result = add_complex()
3737
assert python_result == pyccel_result
3838

39-
def test_add_variable_int(python_only_language):
39+
def test_add_variable_int(language):
4040
def add_element_range():
4141
a = {1, 2, 3}
4242
for i in range(50, 100):
4343
a.add(i)
44-
return a
45-
epyc_add_element = epyccel(add_element_range, language = python_only_language)
44+
return len(a)
45+
epyc_add_element = epyccel(add_element_range, language = language)
4646
pyccel_result = epyc_add_element()
4747
python_result = add_element_range()
4848
assert python_result == pyccel_result
@@ -335,51 +335,39 @@ def copy_from_arg2(a : 'set[float]'):
335335
assert isinstance(python_result, type(pyccel_result))
336336
assert python_result == pyccel_result
337337

338-
@pytest.fixture( params=[
339-
pytest.param("fortran", marks = [
340-
pytest.mark.skip(reason="set methods not implemented in fortran"),
341-
pytest.mark.fortran]),
342-
pytest.param("c", marks = pytest.mark.c),
343-
pytest.param("python", marks = pytest.mark.python)
344-
],
345-
scope = "module"
346-
)
347-
def language_without_fortran(request):
348-
return request.param
349-
350-
def test_Pop_int(language_without_fortran):
338+
def test_Pop_int(stc_language):
351339
def Pop_int():
352340
se = {2, 4, 9}
353341
el1 = se.pop()
354342
el2 = se.pop()
355343
el3 = se.pop()
356344
return el1, el2, el3
357-
epyccel_remove = epyccel(Pop_int, language = language_without_fortran)
345+
epyccel_remove = epyccel(Pop_int, language = stc_language)
358346
pyccel_result = set(epyccel_remove())
359347
python_result = set(Pop_int())
360348
assert python_result == pyccel_result
361349

362-
def test_Pop_float(language_without_fortran):
350+
def test_Pop_float(stc_language):
363351
def Pop_float():
364352
se = {2.3 , 4.1, 9.5}
365353
el1 = se.pop()
366354
el2 = se.pop()
367355
el3 = se.pop()
368356
return el1, el2, el3
369-
epyccel_remove = epyccel(Pop_float, language = language_without_fortran)
357+
epyccel_remove = epyccel(Pop_float, language = stc_language)
370358
pyccel_result = set(epyccel_remove())
371359
python_result = set(Pop_float())
372360
assert python_result == pyccel_result
373361

374362

375-
def test_Pop_complex(language_without_fortran):
363+
def test_Pop_complex(stc_language):
376364
def Pop_complex():
377365
se = {4j , 1j, 7j}
378366
el1 = se.pop()
379367
el2 = se.pop()
380368
el3 = se.pop()
381369
return el1, el2, el3
382-
epyccel_remove = epyccel(Pop_complex, language = language_without_fortran)
370+
epyccel_remove = epyccel(Pop_complex, language = stc_language)
383371
pyccel_result = set(epyccel_remove())
384372
python_result = set(Pop_complex())
385373
assert python_result == pyccel_result

0 commit comments

Comments
 (0)