Multiscale Universal Interface  2.0
A Concurrent Framework for Coupling Heterogeneous Solvers
stream_unordered.h
Go to the documentation of this file.
1 /*****************************************************************************
2 * Multiscale Universal Interface Code Coupling Library *
3 * *
4 * Copyright (C) 2019 Y. H. Tang, S. Kudo, X. Bian, Z. Li, G. E. Karniadakis *
5 * *
6 * This software is jointly licensed under the Apache License, Version 2.0 *
7 * and the GNU General Public License version 3, you may use it according *
8 * to either. *
9 * *
10 * ** Apache License, version 2.0 ** *
11 * *
12 * Licensed under the Apache License, Version 2.0 (the "License"); *
13 * you may not use this file except in compliance with the License. *
14 * You may obtain a copy of the License at *
15 * *
16 * http://www.apache.org/licenses/LICENSE-2.0 *
17 * *
18 * Unless required by applicable law or agreed to in writing, software *
19 * distributed under the License is distributed on an "AS IS" BASIS, *
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
21 * See the License for the specific language governing permissions and *
22 * limitations under the License. *
23 * *
24 * ** GNU General Public License, version 3 ** *
25 * *
26 * This program is free software: you can redistribute it and/or modify *
27 * it under the terms of the GNU General Public License as published by *
28 * the Free Software Foundation, either version 3 of the License, or *
29 * (at your option) any later version. *
30 * *
31 * This program is distributed in the hope that it will be useful, *
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
34 * GNU General Public License for more details. *
35 * *
36 * You should have received a copy of the GNU General Public License *
37 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
38 *****************************************************************************/
39 
48 #ifndef MUI_STREAM_UNORDERED_H
49 #define MUI_STREAM_UNORDERED_H
50 
51 #include <unordered_map>
52 #include <unordered_set>
53 
54 #include "stream.h"
55 
56 namespace mui {
57 template<typename K, typename V>
58 inline istream& operator>>(istream& stream, std::unordered_map<K,V>& ret)
59 {
60  std::size_t size;
61  stream >> size;
62  std::unordered_map<K,V> map;
63  map.reserve(size);
64  for( std::uint64_t i=0; i<size; ++i ) {
65  std::pair<K,V> p;
66  stream >> p;
67  map.emplace(std::move(p));
68  }
69  ret.swap(map);
70  return stream;
71 }
72 template<typename K, typename V>
73 inline ostream& operator<<(ostream& stream, const std::unordered_map<K,V>& map)
74 {
75  stream << map.size();
76  for( const auto& p : map ) stream << p;
77  return stream;
78 }
79 
80 template<typename K, typename V>
81 inline istream& operator>>(istream& stream, std::unordered_multimap<K,V>& ret)
82 {
83  std::size_t size;
84  stream >> size;
85  std::unordered_multimap<K,V> map;
86  map.reserve(size);
87  for( std::uint64_t i=0; i<size; ++i ) {
88  std::pair<K,V> p;
89  stream >> p;
90  map.emplace(std::move(p));
91  }
92  ret.swap(map);
93  return stream;
94 }
95 template<typename K, typename V>
96 inline ostream& operator<<(ostream& stream, const std::unordered_multimap<K,V>& map)
97 {
98  stream << map.size();
99  for( const auto& p : map ) stream << p;
100  return stream;
101 }
102 
103 template<typename K>
104 inline istream& operator>>(istream& stream, std::unordered_set<K>& ret)
105 {
106  std::size_t size;
107  stream >> size;
108  std::unordered_set<K> set;
109  set.reserve(size);
110  for( std::uint64_t i=0; i<size; ++i ) {
111  K k;
112  stream >> k;
113  set.emplace(std::move(k));
114  }
115  ret.swap(set);
116  return stream;
117 }
118 
119 template<typename K>
120 inline ostream& operator<<(ostream& stream, const std::unordered_set<K>& set)
121 {
122  stream << set.size();
123  for( const auto& k : set ) stream << k;
124  return stream;
125 }
126 
127 
128 template<typename K>
129 inline istream& operator>>(istream& stream, std::unordered_multiset<K>& ret)
130 {
131  std::size_t size;
132  stream >> size;
133  std::unordered_multiset<K> set;
134  set.reserve(size);
135  for( std::uint64_t i=0; i<size; ++i ) {
136  K k;
137  stream >> k;
138  set.emplace(std::move(k));
139  }
140  ret.swap(set);
141  return stream;
142 }
143 
144 template<typename K>
145 inline ostream& operator<<(ostream& stream, const std::unordered_multiset<K>& set)
146 {
147  stream << set.size();
148  for( const auto& k : set ) stream << k;
149  return stream;
150 }
151 }
152 
153 #endif
Definition: stream.h:61
Definition: stream.h:67
Definition: comm.h:54
istream & operator>>(istream &stream, smalluint &sml)
Definition: comm_tcp.h:103
ostream & operator<<(ostream &stream, const smalluint &sml)
Definition: comm_tcp.h:127
Defines base stream class container_stream and associated functors.