DenseLinAlgPack: Concreate C++ Classes for Dense Blas-Compatible Linear Algebra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
DenseLinAlgPack_DVectorInFunc.cpp
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Moocho: Multi-functional Object-Oriented arCHitecture for Optimization
5 // Copyright (2003) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #include <sstream>
43 
44 #include "DenseLinAlgPack_DVectorInFunc.hpp"
45 #include "DenseLinAlgPack_DVectorClass.hpp"
46 #include "DenseLinAlgPack_AssertOp.hpp"
47 
48 namespace { // Local implementation
49 std::istream& input_vs(std::istream& is, DenseLinAlgPack::DVectorSlice* vs, const char func[]);
50 }
51 
52 std::istream& DenseLinAlgPack::input(std::istream& is, DVector* v, LinAlgPackIO::fmtflags extra_flags) {
53  if( !(extra_flags & LinAlgPackIO::ignore_dim_bit) ) {
54  size_type n;
55  is >> n;
56  if(is.fail())
57  throw LinAlgPackIO::InputException("DenseLinAlgPack::input() {DVector}: Input operation of vector dimension failed. Check that the constant n is a valid integer.");
58  if(is.bad())
59  throw std::ios_base::failure("DenseLinAlgPack::input() {DVector}: Input operation failed because the stream became currupted.");
60  v->resize(n);
61  }
62  return input_vs(is,&(*v)(),"DenseLinAlgPack::input() {DVector}");
63 }
64 
65 std::istream& DenseLinAlgPack::input(std::istream& is, DVectorSlice* vs, LinAlgPackIO::fmtflags extra_flags) {
66  if( !(extra_flags & LinAlgPackIO::ignore_dim_bit) ) {
67  size_type n;
68  is >> n;
69  if(is.fail())
70  throw LinAlgPackIO::InputException("DenseLinAlgPack::input() {DVectorSlice}: Input operation of vector dimension failed. Check that the constant n is a valid integer.");
71  if(is.bad())
72  throw std::ios_base::failure("DenseLinAlgPack::input() {DVectorSlice}: Input operation failed because the stream became currupted.");
73  DenseLinAlgPack::Vp_V_assert_sizes( vs->dim(), n );
74  }
75  return input_vs(is,vs,"DenseLinAlgPack::input() {DVectorSlice}");
76 }
77 
78 
79 // ///////////////////////////////////
80 // Local implementation
81 
82 namespace {
83 
84 // Read in a specified number of elements into a DVectorSlice object.
85 // The dim of vs is not checked. If an element input operation fails or the end of the file
86 // is reached before all of the elements are read in then a LinAlgPackIO::InputException is thrown.
87 // If the stream becomes currupted durring the input then a std::ios_base::failure exception
88 // is thrown. The state of the input steam remains the same on return accept for the char's
89 // that have been extracted.
90 std::istream& input_vs(std::istream& is, DenseLinAlgPack::DVectorSlice* vs, const char func[]) {
91  using std::ios_base;
93  if(!vs->dim()) return is; // If there are no elements to read in just return
94  ios_base::iostate old_state = is.exceptions(); // save the old state
95  is.exceptions(ios_base::badbit | ios_base::failbit);
96  try {
97  // Read in the elements
98  for(DVectorSlice::iterator itr = vs->begin(); itr != vs->end(); ++itr)
99  is >> *itr;
100  }
101  catch(std::ios_base::failure& excpt) {
102  is.exceptions(old_state);
103  if(is.bad()) throw; // The stream was bad so rethrow the exception
104  if(is.fail()) {
105  std::ostringstream os;
106  os << func << ": An vector element input failed. Check that the vector element is a valid C number. "
107  << excpt.what();
109  }
110  if(is.eof()) {
111  std::ostringstream os;
112  os << func << ": DVector input failed. The end of the file was found before all of the elements where read in. "
113  << excpt.what();;
115  }
116  }
117  catch(...) {
118  is.exceptions(old_state);
119  throw;
120  }
121  is.exceptions(old_state);
122  return is;
123 }
124 
125 } // end namespace
size_type dim() const
Returns the number of elements of the VectorSliceTmpl.
size_t size_type