3 // Copyright (C) 2005-2025 Free Software Foundation, Inc.
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
8 // Foundation; either version 3, or (at your option) any later
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.
16 // You should have received a copy of the GNU General Public License
17 // along with this library; see the file COPYING3. If not see
18 // <http://www.gnu.org/licenses/>.
21 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
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
33 * @file text_populate.hpp
34 * Contains a function for populating a vector with text words from
38 #ifndef PB_DS_TEXT_POPULATE_HPP
39 #define PB_DS_TEXT_POPULATE_HPP
41 #include <io/illegal_input_error.hpp>
53 template<typename Vec
>
55 text_populate(const std::string
& r_f_name
, Vec
& r_a_txt
)
57 const size_t size
= r_a_txt
.size();
61 std::ifstream
f(r_f_name
.c_str());
65 std::cerr
<< "Cannot open file " << r_f_name
.c_str() <<
68 throw __gnu_pbds::test::illegal_input_error();
73 while (f
.good()&& i
< size
)
75 f
>> r_a_txt
[i
].first
;
76 r_a_txt
[i
].second
= 0;
83 std::cerr
<< "Read only " << static_cast<unsigned long>(i
) <<
84 " words" << std::endl
;
86 throw __gnu_pbds::test::illegal_input_error();
91 std::cerr
<< "Error reading from file" << std::endl
;
97 template<typename Vec
>
99 distinct_text_populate(const std::string
& r_f_name
, Vec
& r_a_txt
)
101 const size_t size
= r_a_txt
.size();
105 std::ifstream
f(r_f_name
.c_str());
109 std::cerr
<< "Cannot open file " << r_f_name
.c_str() <<
112 throw __gnu_pbds::test::illegal_input_error();
115 typedef std::set
< typename
Vec::value_type::first_type
> set_t
;
119 while (f
.good()&& s
.size() < size
)
121 typename
Vec::value_type::first_type v
;
125 if (s
.find(v
) == s
.end())
127 r_a_txt
[s
.size()] = std::make_pair(v
, 0);
135 std::cerr
<< "Read only " << static_cast<unsigned long>(s
.size()) <<
136 " words" << std::endl
;
138 throw __gnu_pbds::test::illegal_input_error();
143 std::cerr
<< "Error reading from file" << std::endl
;
151 } // namespace __gnu_pbds
153 #endif // #ifndef PB_DS_TEXT_POPULATE_HPP