cython

Форк
0
/
cpp_stl_multimap.pyx 
159 строк · 5.4 Кб
1
# mode: run
2
# tag: cpp, cpp11
3

4
# cython: language_level=3
5

6
from libcpp.map cimport multimap
7
from libcpp.unordered_map cimport unordered_multimap
8
from libcpp.utility cimport pair
9

10
def test_multimap_insert(vals):
11
    """
12
    >>> test_multimap_insert([(1,1),(2,2),(2,2),(3,3),(-1,-1)])
13
    [(-1, -1), (1, 1), (2, 2), (2, 2), (3, 3)]
14
    """
15
    cdef multimap[int,int] mm = multimap[int, int]()
16
    cdef multimap[int, int].iterator it
17
    for v in vals:
18
        it = mm.insert(v)
19
    return [ (item.first, item.second) for item in mm ]
20

21
def test_multimap_insert_it(vals):
22
    """
23
    >>> test_multimap_insert_it([(1,1),(2,2),(2,2),(3,3),(-1,-1)])
24
    [(-1, -1), (1, 1), (2, 2), (2, 2), (3, 3)]
25
    """
26
    cdef unordered_multimap[int,int] umm = unordered_multimap[int,int]()
27
    cdef multimap[int,int] mm = multimap[int,int]()
28
    for k, v in vals:
29
        umm.insert(pair[int,int](k, v))
30
    mm.insert(umm.begin(), umm.end())
31
    return [ (item.first, item.second) for item in mm ]
32

33
def test_const_multimap_insert_it(vals):
34
    """
35
    >>> test_const_multimap_insert_it([(1,1),(2,2),(2,2),(3,3),(-1,-1)])
36
    [(-1, -1), (1, 1), (2, 2), (2, 2), (3, 3)]
37
    """
38
    cdef unordered_multimap[int,int] umm = unordered_multimap[int,int]()
39
    cdef multimap[int,int] mm = multimap[int,int]()
40
    for k, v in vals:
41
        umm.insert(pair[int,int](k, v))
42
    mm.insert(umm.cbegin(), umm.cend())
43
    return [ (item.first, item.second) for item in mm ]
44

45
def test_multimap_count(vals, to_find):
46
    """
47
    >>> test_multimap_count([(1,1),(2,2),(2,2),(3,3),(-1,-1)], 1)
48
    1
49
    >>> test_multimap_count([(1,1),(2,2),(2,2),(3,3),(-1,-1)], 2)
50
    2
51
    """
52
    cdef multimap[int,int] mm = multimap[int,int]()
53
    for v in vals:
54
        mm.insert(v)
55
    return mm.count(to_find)
56

57
def test_multimap_erase(vals, int to_remove):
58
    """
59
    >>> test_multimap_erase([(1,1),(2,2),(2,2),(3,3),(-1,-1)], 1)
60
    [(-1, -1), (2, 2), (2, 2), (3, 3)]
61
    >>> test_multimap_erase([(1,1),(2,2),(2,2),(3,3),(-1,-1)], 2)
62
    [(-1, -1), (1, 1), (3, 3)]
63
    """
64
    cdef multimap[int,int] mm = multimap[int,int]()
65
    for v in vals:
66
        mm.insert(v)
67
    cdef size_t ret = mm.erase(to_remove)
68
    return [ (item.first, item.second) for item in mm ]
69

70
def test_multimap_find_erase(vals, to_remove):
71
    """
72
    >>> test_multimap_find_erase([(1,1),(2,2),(2,2),(3,3),(-1,-1)], 1)
73
    [(-1, -1), (2, 2), (2, 2), (3, 3)]
74
    >>> test_multimap_find_erase([(1,1),(2,2),(2,2),(3,3),(-1,-1)], 2)
75
    [(-1, -1), (1, 1), (2, 2), (3, 3)]
76
    """
77
    cdef multimap[int,int] mm = multimap[int,int]()
78
    cdef multimap[int,int].iterator it
79
    for v in vals:
80
        mm.insert(v)
81
    it = mm.find(to_remove)
82
    it = mm.erase(it)
83
    return [ (item.first, item.second) for item in mm ]
84

85

86
def test_unordered_multimap_insert(vals):
87
    """
88
    >>> test_unordered_multimap_insert([(1,1),(2,2),(2,2),(3,3),(-1,-1)])
89
    [(-1, -1), (1, 1), (2, 2), (2, 2), (3, 3)]
90
    """
91
    cdef unordered_multimap[int,int] umm = unordered_multimap[int,int]()
92
    cdef unordered_multimap[int,int].iterator it
93
    for v in vals:
94
        it = umm.insert(v)
95
    return sorted([ (item.first, item.second) for item in umm ])
96

97
def test_unordered_multimap_insert_it(vals):
98
    """
99
    >>> test_unordered_multimap_insert_it([(1,1),(2,2),(2,2),(3,3),(-1,-1)])
100
    [(-1, -1), (1, 1), (2, 2), (2, 2), (3, 3)]
101
    """
102
    cdef multimap[int,int] mm = multimap[int,int]()
103
    cdef unordered_multimap[int,int] umm = unordered_multimap[int,int]()
104
    for v in vals:
105
        mm.insert(v)
106
    umm.insert(mm.begin(), mm.end())
107
    return sorted([ (item.first, item.second) for item in umm ])
108

109
def test_const_unordered_multimap_insert_it(vals):
110
    """
111
    >>> test_const_unordered_multimap_insert_it([(1,1),(2,2),(2,2),(3,3),(-1,-1)])
112
    [(-1, -1), (1, 1), (2, 2), (2, 2), (3, 3)]
113
    """
114
    cdef multimap[int,int] mm = multimap[int,int]()
115
    cdef unordered_multimap[int,int] umm = unordered_multimap[int,int]()
116
    for v in vals:
117
        mm.insert(v)
118
    umm.insert(mm.cbegin(), mm.cend())
119
    return sorted([ (item.first, item.second) for item in umm ])
120

121
def test_unordered_multimap_count(vals, to_find):
122
    """
123
    >>> test_unordered_multimap_count([(1,1),(2,2),(2,2),(3,3),(-1,-1)], 1)
124
    1
125
    >>> test_unordered_multimap_count([(1,1),(2,2),(2,2),(3,3),(-1,-1)], 2)
126
    2
127
    """
128
    cdef unordered_multimap[int,int] umm = unordered_multimap[int,int]()
129
    for v in vals:
130
        umm.insert(v)
131
    return umm.count(to_find)
132

133
def test_unordered_multimap_erase(vals, int to_remove):
134
    """
135
    >>> test_unordered_multimap_erase([(1,1),(2,2),(2,2),(3,3),(-1,-1)], 1)
136
    [(-1, -1), (2, 2), (2, 2), (3, 3)]
137
    >>> test_unordered_multimap_erase([(1,1),(2,2),(2,2),(3,3),(-1,-1)], 2)
138
    [(-1, -1), (1, 1), (3, 3)]
139
    """
140
    cdef unordered_multimap[int,int] umm = unordered_multimap[int,int]()
141
    for v in vals:
142
        umm.insert(v)
143
    cdef size_t ret = umm.erase(to_remove)
144
    return sorted([ (item.first, item.second) for item in umm ])
145

146
def test_unordered_multimap_find_erase(vals, to_remove):
147
    """
148
    >>> test_unordered_multimap_find_erase([(1,1),(2,2),(2,2),(3,3),(-1,-1)], 1)
149
    [(-1, -1), (2, 2), (2, 2), (3, 3)]
150
    >>> test_unordered_multimap_find_erase([(1,1),(2,2),(2,2),(3,3),(-1,-1)], 2)
151
    [(-1, -1), (1, 1), (2, 2), (3, 3)]
152
    """
153
    cdef unordered_multimap[int,int] umm = unordered_multimap[int,int]()
154
    cdef unordered_multimap[int,int].iterator it
155
    for v in vals:
156
        umm.insert(v)
157
    it = umm.find(to_remove)
158
    it = umm.erase(it)
159
    return sorted([ item for item in umm ])
160

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.