]>
Commit | Line | Data |
---|---|---|
4569a895 AT |
1 | // -*- C++ -*- |
2 | ||
83ffe9cd | 3 | // Copyright (C) 2005-2023 Free Software Foundation, Inc. |
4569a895 AT |
4 | // |
5 | // This file is part of the GNU ISO C++ Library. This library is free | |
6 | // software; you can redistribute it and/or modify it under the terms | |
7 | // of the GNU General Public License as published by the Free Software | |
748086b7 | 8 | // Foundation; either version 3, or (at your option) any later |
4569a895 AT |
9 | // version. |
10 | ||
11 | // This library is distributed in the hope that it will be useful, but | |
12 | // WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | // General Public License for more details. | |
15 | ||
16 | // You should have received a copy of the GNU General Public License | |
748086b7 JJ |
17 | // along with this library; see the file COPYING3. If not see |
18 | // <http://www.gnu.org/licenses/>. | |
19 | ||
4569a895 AT |
20 | |
21 | // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL. | |
22 | ||
23 | // Permission to use, copy, modify, sell, and distribute this software | |
24 | // is hereby granted without fee, provided that the above copyright | |
25 | // notice appears in all copies, and that both that copyright notice | |
26 | // and this permission notice appear in supporting documentation. None | |
27 | // of the above authors, nor IBM Haifa Research Laboratories, make any | |
28 | // representation about the suitability of this software for any | |
29 | // purpose. It is provided "as is" without express or implied | |
30 | // warranty. | |
31 | ||
32 | /** | |
33 | * @file text_populate.hpp | |
34 | * Contains a function for populating a vector with text words from | |
35 | * a file. | |
36 | */ | |
37 | ||
38 | #ifndef PB_DS_TEXT_POPULATE_HPP | |
39 | #define PB_DS_TEXT_POPULATE_HPP | |
40 | ||
41 | #include <io/illegal_input_error.hpp> | |
42 | #include <set> | |
43 | #include <fstream> | |
44 | #include <string> | |
45 | #include <iostream> | |
46 | ||
5e11f978 | 47 | namespace __gnu_pbds |
4569a895 AT |
48 | { |
49 | ||
50 | namespace test | |
51 | { | |
52 | ||
53 | template<typename Vec> | |
54 | void | |
55 | text_populate(const std::string& r_f_name, Vec& r_a_txt) | |
56 | { | |
57 | const size_t size = r_a_txt.size(); | |
58 | ||
59 | try | |
60 | { | |
61 | std::ifstream f(r_f_name.c_str()); | |
62 | ||
63 | if (!f.good()) | |
64 | { | |
65 | std::cerr << "Cannot open file " << r_f_name.c_str() << | |
66 | std::endl; | |
67 | ||
5e11f978 | 68 | throw __gnu_pbds::test::illegal_input_error(); |
4569a895 AT |
69 | } |
70 | ||
71 | size_t i = 0; | |
72 | ||
73 | while (f.good()&& i < size) | |
74 | { | |
75 | f >> r_a_txt[i].first; | |
76 | r_a_txt[i].second = 0; | |
77 | ||
78 | ++i; | |
79 | } | |
80 | ||
81 | if (i < size) | |
82 | { | |
83 | std::cerr << "Read only " << static_cast<unsigned long>(i) << | |
84 | " words" << std::endl; | |
85 | ||
5e11f978 | 86 | throw __gnu_pbds::test::illegal_input_error(); |
4569a895 AT |
87 | } |
88 | } | |
89 | catch(...) | |
90 | { | |
91 | std::cerr << "Error reading from file" << std::endl; | |
92 | ||
93 | throw; | |
94 | } | |
95 | } | |
96 | ||
97 | template<typename Vec> | |
98 | void | |
99 | distinct_text_populate(const std::string& r_f_name, Vec& r_a_txt) | |
100 | { | |
101 | const size_t size = r_a_txt.size(); | |
102 | ||
103 | try | |
104 | { | |
105 | std::ifstream f(r_f_name.c_str()); | |
106 | ||
107 | if (!f.good()) | |
108 | { | |
109 | std::cerr << "Cannot open file " << r_f_name.c_str() << | |
110 | std::endl; | |
111 | ||
5e11f978 | 112 | throw __gnu_pbds::test::illegal_input_error(); |
4569a895 AT |
113 | } |
114 | ||
115 | typedef std::set< typename Vec::value_type::first_type> set_t; | |
116 | ||
117 | set_t s; | |
118 | ||
119 | while (f.good()&& s.size() < size) | |
120 | { | |
121 | typename Vec::value_type::first_type v; | |
122 | ||
123 | f >> v; | |
124 | ||
125 | if (s.find(v) == s.end()) | |
126 | { | |
127 | r_a_txt[s.size()] = std::make_pair(v, 0); | |
128 | ||
129 | s.insert(v); | |
130 | } | |
131 | } | |
132 | ||
133 | if (s.size() < size) | |
134 | { | |
135 | std::cerr << "Read only " << static_cast<unsigned long>(s.size()) << | |
136 | " words" << std::endl; | |
137 | ||
5e11f978 | 138 | throw __gnu_pbds::test::illegal_input_error(); |
4569a895 AT |
139 | } |
140 | } | |
141 | catch(...) | |
142 | { | |
143 | std::cerr << "Error reading from file" << std::endl; | |
144 | ||
145 | throw; | |
146 | } | |
147 | } | |
148 | ||
149 | } // namespace test | |
150 | ||
5e11f978 | 151 | } // namespace __gnu_pbds |
4569a895 AT |
152 | |
153 | #endif // #ifndef PB_DS_TEXT_POPULATE_HPP |