Skip to content

Commit 8230484

Browse files
committed
Add initial implementation of std::map
1 parent 0e44905 commit 8230484

File tree

3 files changed

+126
-2
lines changed

3 files changed

+126
-2
lines changed

Examples/test-suite/fortran/Makefile.in

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ FAILING_CPP_TESTS += \
5252
smart_pointer_member \
5353
smart_pointer_template_defaults_overload \
5454
typedef_classforward_same_name \
55-
li_std_map \
5655
cpp11_result_of \
5756
cpp11_rvalue_reference2 \
5857
cpp11_rvalue_reference3 \
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
! File : li_std_map_runme.f90
2+
3+
#include "fassert.h"
4+
5+
program li_std_map_runme
6+
use ISO_C_BINDING
7+
implicit none
8+
9+
call test_map_int_int
10+
call test_map_string_int
11+
contains
12+
13+
subroutine test_map_int_int
14+
use ISO_C_BINDING
15+
! Note that the test instantiates *multiset* as a set_int
16+
use li_std_map, only : MapIntInt => IntIntMap
17+
implicit none
18+
type(MapIntInt) :: m
19+
integer :: num_erased
20+
logical :: inserted = .false.
21+
22+
m = MapIntInt()
23+
ASSERT(m%empty())
24+
25+
call m%insert(10, 4)
26+
call m%insert(3, 5, inserted)
27+
ASSERT(inserted .eqv. .true.)
28+
ASSERT(m%size() == 2)
29+
ASSERT(m%count(3) == 1)
30+
ASSERT(m%count(5) == 0)
31+
32+
! Insertion of existing element will *not* replace value
33+
call m%insert(3, 6, inserted)
34+
ASSERT(inserted .eqv. .false.)
35+
ASSERT(m%count(3) == 1)
36+
ASSERT(m%size() == 2)
37+
ASSERT(m%get(3) == 5)
38+
39+
! Use "set" method to unconditionally assign
40+
call m%set(3, 6)
41+
ASSERT(m%get(3) == 6)
42+
43+
call m%erase(10)
44+
ASSERT(m%size() == 1)
45+
ASSERT(m%count(10) == 0)
46+
47+
call m%erase(3, num_erased)
48+
ASSERT(num_erased == 1)
49+
ASSERT(m%count(3) == 0)
50+
51+
call m%release()
52+
end subroutine
53+
54+
subroutine test_map_string_int
55+
use ISO_C_BINDING
56+
use li_std_map, only : MapStringInt => StringIntMap
57+
implicit none
58+
type(MapStringInt) :: m
59+
60+
m = MapStringInt()
61+
ASSERT(m%empty())
62+
63+
call m%insert("yoohoo", 10)
64+
call m%insert("howdy", 5)
65+
ASSERT(m%size() == 2)
66+
ASSERT(m%count("yoohoo") == 1)
67+
ASSERT(m%count("hiya") == 0)
68+
ASSERT(m%get("yoohoo") == 10)
69+
call m%release()
70+
end subroutine
71+
72+
end program
73+

Lib/fortran/std_map.i

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,60 @@
22
* std_map.i
33
* ------------------------------------------------------------------------- */
44

5+
%include "std_common.i"
6+
57
%{
68
#include <map>
79
%}
8-
#error "std::map support is not yet implemented"
910

11+
%define %swig_std_map(_Key, _Value, _Compare)
12+
13+
public:
14+
typedef size_t size_type;
15+
typedef ptrdiff_t difference_type;
16+
typedef _Key key_type;
17+
typedef _Value mapped_type;
18+
typedef std::pair<const _Key, _Value> value_type;
19+
typedef value_type *pointer;
20+
typedef const value_type *const_pointer;
21+
typedef value_type &reference;
22+
typedef const value_type &const_reference;
23+
24+
map();
25+
26+
// - Use native Fortran integers in proxy code
27+
%apply int FORTRAN_INT {size_type};
28+
29+
bool empty() const;
30+
size_type size() const;
31+
void clear();
32+
33+
%fortransubroutine erase;
34+
35+
size_type erase(const key_type &x);
36+
size_type count(const key_type &x) const;
37+
38+
%fortransubroutine insert;
39+
40+
%extend {
41+
bool insert(const _Key& k, const _Value& v) {
42+
std::pair<std::map<_Key, _Value, _Compare >::iterator, bool> result = $self->insert(std::pair<_Key, _Value>(k, v));
43+
return result.second;
44+
}
45+
46+
const _Value& get(const _Key& k) {
47+
return (*$self)[k];
48+
}
49+
void set(const _Key& k, const _Value& v) {
50+
(*$self)[k] = v;
51+
}
52+
}
53+
54+
%enddef
55+
56+
namespace std {
57+
template<class _Key, class _Value, class _Compare = std::less<_Key > >
58+
class map {
59+
%swig_std_map(_Key, _Value, _Compare);
60+
};
61+
} // namespace std

0 commit comments

Comments
 (0)