AbstractLinAlgPack_SparseCOOReadMatrix.cpp

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

Generated on Thu Sep 18 12:35:12 2008 for MOOCHO (Single Doxygen Collection) by doxygen 1.3.9.1