Skip to content

Commit edf7962

Browse files
committed
add extract() and replace() to flat_map (#1078)
1 parent 63e8626 commit edf7962

File tree

4 files changed

+196
-7
lines changed

4 files changed

+196
-7
lines changed

reference/flat_map/flat_map.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ namespace std {
6666
| [`try_emplace`](flat_map/try_emplace.md) | キーが存在しない場合のみ要素を直接構築する | C++23 |
6767
| [`erase`](flat_map/erase.md) | 要素を削除する | C++23 |
6868
| [`swap`](flat_map/swap.md) | コンテンツを交換する | C++23 |
69-
| [`extract`](flat_map/extract.md.nolink) | キーのコンテナ、値のコンテナを取得する | C++23 |
70-
| [`replace`](flat_map/replace.md.nolink) | キーのコンテナ、値のコンテナを置き換える | C++23 |
69+
| [`extract`](flat_map/extract.md) | キーのコンテナ、値のコンテナを取得する | C++23 |
70+
| [`replace`](flat_map/replace.md) | キーのコンテナ、値のコンテナを置き換える | C++23 |
7171
7272
7373
### 要素アクセス

reference/flat_map/flat_map/containers.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ namespace std {
2929
## 概要
3030
`flat_map`クラス内部のデータ保持方法として、キーのコンテナと値のコンテナをもつ。
3131
32-
この形式の内部表現は[`extract()`](extract.md.nolink)メンバ関数で取得でき、シリアライズなどの用途に使用できる。
32+
この形式の内部表現は [`extract()`](extract.md) メンバ関数で取得でき、シリアライズなどの用途に使用できる。
3333
3434
3535
## 例
@@ -38,6 +38,7 @@ namespace std {
3838
#include <iostream>
3939
#include <flat_map>
4040
#include <string>
41+
#include <utility>
4142
4243
int main()
4344
{
@@ -47,21 +48,21 @@ int main()
4748
{"Carol", 4}
4849
};
4950
50-
decltype(fm)::containers c = fm.extract();
51+
decltype(fm)::containers c = std::move(fm).extract();
5152
52-
std::cout << "keys:"
53+
std::cout << "keys:" << std::endl;
5354
for (const auto& key : c.keys) {
5455
std::cout << " " << key << std::endl;
5556
}
5657
57-
std::cout << "values:"
58+
std::cout << "values:" << std::endl;
5859
for (const auto& value : c.values) {
5960
std::cout << " " << value << std::endl;
6061
}
6162
}
6263
```
6364
* containers[color ff0000]
64-
* fm.extract()[link extract.md.nolink]
65+
* fm.extract()[link extract.md]
6566

6667
#### 出力
6768
```
@@ -84,3 +85,7 @@ values:
8485
- [Clang](/implementation.md#clang): ??
8586
- [GCC](/implementation.md#gcc): ??
8687
- [Visual C++](/implementation.md#visual_cpp): ??
88+
89+
90+
## 関連項目
91+
- [`extract`](extract.md)
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# extract
2+
* flat_map[meta header]
3+
* std[meta namespace]
4+
* flat_map[meta class]
5+
* function[meta id-type]
6+
* cpp23[meta cpp]
7+
8+
```cpp
9+
containers extract() &&; // C++23
10+
```
11+
12+
## 概要
13+
キーのコンテナ、値のコンテナを戻り値として返す。
14+
15+
16+
## 戻り値
17+
クラス内部のデータ保持形式である [`containers`](containers.md) オブジェクト。
18+
19+
20+
## 事後条件
21+
呼び出し側の `flat_map` は空になる(たとえ例外で関数が中断されたとしても)。
22+
23+
24+
## 計算量
25+
定数時間。
26+
27+
28+
## 備考
29+
本関数は右辺値修飾されているので、右辺値からのみ読み出し可能である。
30+
31+
32+
##
33+
```cpp example
34+
#include <flat_map>
35+
#include <iostream>
36+
#include <string>
37+
#include <utility>
38+
39+
int main()
40+
{
41+
std::flat_map<std::string, int> fm = {
42+
{"Alice", 3},
43+
{"Bob", 1},
44+
{"Carol", 4}
45+
};
46+
47+
std::cout << fm.size() << std::endl;
48+
49+
decltype(fm)::containers c = std::move(fm).extract();
50+
51+
std::cout << fm.size() << std::endl;
52+
std::cout << std::endl;
53+
54+
auto k = c.keys.cbegin();
55+
auto v = c.values.cbegin();
56+
std::cout << "{" << std::endl;
57+
for (; k != c.keys.cend() && v != c.values.cend(); ++k, ++v) {
58+
std::cout << " " << *k << ": " << *v << "," << std::endl;
59+
}
60+
std::cout << "}" << std::endl;
61+
}
62+
```
63+
* extract()[color ff0000]
64+
* fm.size()[link size.md]
65+
66+
### 出力
67+
```
68+
3
69+
0
70+
71+
{
72+
Alice: 3,
73+
Bob: 1,
74+
Carol: 4,
75+
}
76+
```
77+
78+
## バージョン
79+
### 言語
80+
- C++23
81+
82+
### 処理系
83+
- [Clang](/implementation.md#clang): ??
84+
- [GCC](/implementation.md#gcc): ??
85+
- [ICC](/implementation.md#icc): ??
86+
- [Visual C++](/implementation.md#visual_cpp): ??
87+
88+
89+
## 関連項目
90+
- [`containers`](containers.md)
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# replace
2+
* flat_map[meta header]
3+
* std[meta namespace]
4+
* flat_map[meta class]
5+
* function[meta id-type]
6+
* cpp23[meta cpp]
7+
8+
```cpp
9+
void replace(key_container_type&& key_cont, mapped_container_type&& mapped_cont); // C++23
10+
```
11+
12+
## 概要
13+
キーのコンテナ、値のコンテナをそれぞれ置き換える。
14+
15+
16+
## 効果
17+
`flat_map` クラスが内部で保持している [`containers`](containers.md) を `c` とすると、以下と等価。
18+
```cpp
19+
c.keys = std::move(key_cont);
20+
c.values = std::move(mapped_cont)
21+
```
22+
23+
24+
## 事前条件
25+
- `key_cont.size() == mapped_cont.size()` が真であること。
26+
- `key_cont` が `key_compare` に基づいてソートされていること。
27+
- `key_cont` に重複する要素がないこと。
28+
29+
30+
## 計算量
31+
`key_cont` および `mapped_cont` をムーブした計算量と同じ。
32+
33+
34+
## 例
35+
```cpp example
36+
#include <algorithm>
37+
#include <cassert>
38+
#include <flat_map>
39+
#include <iostream>
40+
#include <string>
41+
#include <utility>
42+
43+
int main()
44+
{
45+
std::vector<std::string> keys = {"Alice", "Bob", "Carol"};
46+
std::vector<int> values = {3, 1, 4};
47+
48+
// 事前条件の確認
49+
assert(keys.size() == values.size());
50+
assert(std::is_sorted(keys.begin(), keys.end()));
51+
assert(std::adjacent_find(keys.begin(), keys.end()) == keys.end());
52+
53+
std::flat_map<std::string, int> fm;
54+
55+
std::cout << fm.size() << std::endl;
56+
57+
fm.replace(std::move(keys), std::move(values));
58+
59+
std::cout << fm.size() << std::endl;
60+
std::cout << std::endl;
61+
62+
std::cout << "{" << std::endl;
63+
for (const auto& kv: fm) {
64+
std::cout << " " << kv.first << ": " << kv.second << "," << std::endl;
65+
}
66+
std::cout << "}" << std::endl;
67+
}
68+
```
69+
* replace[color ff0000]
70+
* fm.size()[link size.md]
71+
* std::is_sorted[link /reference/algorithm/is_sorted.md]
72+
* std::adjacent_find[link /reference/algorithm/adjacent_find.md]
73+
74+
### 出力
75+
```
76+
0
77+
3
78+
79+
{
80+
Alice: 3,
81+
Bob: 1,
82+
Carol: 4,
83+
}
84+
```
85+
86+
## バージョン
87+
### 言語
88+
- C++23
89+
90+
### 処理系
91+
- [Clang](/implementation.md#clang): ??
92+
- [GCC](/implementation.md#gcc): ??
93+
- [ICC](/implementation.md#icc): ??
94+
- [Visual C++](/implementation.md#visual_cpp): ??

0 commit comments

Comments
 (0)