AbstractLinAlgPack_SparseCOOReadMatrix.cpp

Go to the documentation of this file.
00001 #if 0
00002 
00003 // @HEADER
00004 // ***********************************************************************
00005 // 
00006 // Moocho: Multi-functional Object-Oriented arCHitecture for Optimization
00007 //                  Copyright (2003) Sandia Corporation
00008 // 
00009 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00010 // license for use of this work by or on behalf of the U.S. Government.
00011 // 
00012 // This library is free software; you can redistribute it and/or modify
00013 // it under the terms of the GNU Lesser General Public License as
00014 // published by the Free Software Foundation; either version 2.1 of the
00015 // License, or (at your option) any later version.
00016 //  
00017 // This library is distributed in the hope that it will be useful, but
00018 // WITHOUT ANY WARRANTY; without even the implied warranty of
00019 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020 // Lesser General Public License for more details.
00021 //  
00022 // You should have received a copy of the GNU Lesser General Public
00023 // License along with this library; if not, write to the Free Software
00024 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00025 // USA
00026 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov) 
00027 // 
00028 // ***********************************************************************
00029 // @HEADER
00030 
00031 #include <stdlib.h>
00032 
00033 #include "AbstractLinAlgPack_SparseCOOReadMatrix.hpp"
00034 
00035 // Throw an exception if the char is not ':'
00036 namespace {
00037 inline void assert_sep_char(char c) {
00038   if(c != ':')
00039     throw AbstractLinAlgPack::InputException("Sparse COO matrix input stream error:  The seperator between the element, row indice and column indice must be a \':\'");
00040 }
00041 inline void assert_eof(std::istream& istrm) {
00042   if(istrm.eof())
00043     throw AbstractLinAlgPack::InputException("Sparse COO matrix input stream error:  Premature end to the input file.");
00044 }
00045 }
00046 
00047 void AbstractLinAlgPack::read_coo_into_valarrays(std::istream& istrm, size_type& m, size_type& n, size_type& nz
00048   , std::valarray<value_type>& a, std::valarray<indice_type>& ivect
00049   , std::valarray<indice_type>& jvect)
00050 {
00051   // read in dimensions and resize
00052   istrm >> m;   assert_eof(istrm);
00053   istrm >> n;   assert_eof(istrm);
00054   istrm >> nz;
00055   a.resize(nz);
00056   ivect.resize(nz);
00057   jvect.resize(nz);
00058 
00059   // Read in the non-zero elements
00060   value_type  *p_a =      &a[0],
00061         *p_a_last =   p_a + nz;
00062   indice_type *p_ivect =    &ivect[0],
00063         *p_jvect =    &jvect[0];
00064 
00065   for(; p_a != p_a_last; ++p_a, ++p_ivect, ++p_jvect) {
00066     const int bs = 50;
00067     char num[bs];
00068     char c;
00069     assert_eof(istrm);
00070     istrm.get(num, bs-1, ':');  assert_eof(istrm);  *p_a = ::atof(num); // Read in ak 
00071     istrm.get(c); assert_eof(istrm); assert_sep_char(c);        // Read in ':'
00072     istrm.get(num, bs-1, ':'); assert_eof(istrm); *p_ivect = ::atoi(num);// Read in ik 
00073     istrm.get(c); assert_eof(istrm); assert_sep_char(c);        // Read in ':'
00074     istrm >> *p_jvect;                    // Read in jk
00075   }
00076 }
00077 
00078 #endif // 0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Generated on Wed Apr 13 10:10:57 2011 for MOOCHO (Single Doxygen Collection) by  doxygen 1.6.3