Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:49:55

0001 /*
0002  * Copyright (c) 2019 Opticks Team. All Rights Reserved.
0003  *
0004  * This file is part of Opticks
0005  * (see https://bitbucket.org/simoncblyth/opticks).
0006  *
0007  * Licensed under the Apache License, Version 2.0 (the "License"); 
0008  * you may not use this file except in compliance with the License.  
0009  * You may obtain a copy of the License at
0010  *
0011  *   http://www.apache.org/licenses/LICENSE-2.0
0012  *
0013  * Unless required by applicable law or agreed to in writing, software 
0014  * distributed under the License is distributed on an "AS IS" BASIS, 
0015  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
0016  * See the License for the specific language governing permissions and 
0017  * limitations under the License.
0018  */
0019 
0020 #include <cassert>
0021 #include <cstring>
0022 #include <cmath>
0023 #include <iostream>
0024 #include <sstream>
0025 #include <iomanip>
0026 #include <iterator>
0027 #include <algorithm>
0028 #include <numeric>
0029 #include <sstream>
0030 
0031 #include "SStr.hh"
0032 #include "SVec.hh"
0033 
0034 
0035 template <typename T>
0036 void SVec<T>::Dump( const char* label, const std::vector<T>& a  )
0037 {
0038     std::cout << std::setw(10) << label  ;
0039     for(unsigned i=0 ; i < a.size() ; i++) std::cout << std::setw(10) << a[i] << " " ; 
0040     std::cout << std::endl ; 
0041 } 
0042 
0043 template <typename T>
0044 std::string SVec<T>::Desc( const char* label, const std::vector<T>& a, int width  )
0045 {
0046     std::stringstream ss ; 
0047     ss << std::setw(10) << label  ;
0048     for(unsigned i=0 ; i < a.size() ; i++) ss << std::setw(width) << a[i] << " " ; 
0049     return ss.str(); 
0050 } 
0051 
0052 
0053 
0054 
0055 
0056 
0057 template <typename T>
0058 void SVec<T>::Dump2( const char* label, const std::vector<T>& a  )
0059 {
0060     std::cout << std::setw(10) << label ;
0061     std::copy( a.begin(), a.end(), std::ostream_iterator<float>(std::cout, " ")) ;
0062     std::cout << std::endl ; 
0063 } 
0064 
0065 template <typename T>
0066 T SVec<T>::MaxDiff(const std::vector<T>& a, const std::vector<T>& b, bool dump)
0067 {
0068     assert( a.size() == b.size() );
0069     T mx = 0.f ;     
0070     for(unsigned i=0 ; i < a.size() ; i++)
0071     {
0072         T df = a[i] - b[i] ;   // std::abs ambiguous when T=unsigned 
0073         if( df < 0 ) df = -df ;  
0074 
0075         if(df > mx) mx = df ; 
0076 
0077         if(dump)
0078         std::cout 
0079             << " a " << a[i] 
0080             << " b " << b[i] 
0081             << " df " << df
0082             << " mx " << mx 
0083             << std::endl 
0084             ; 
0085 
0086     }
0087     return mx ; 
0088 }
0089 
0090 
0091 template <typename T>
0092 int SVec<T>::FindIndexOfValue(const std::vector<T>& a, T value, T tolerance)
0093 {
0094     int idx = -1 ; 
0095     for(unsigned i=0 ; i < a.size() ; i++)
0096     {   
0097         //T df = std::abs(a[i] - value) ;   // std::abs ambiguous when T=unsigned 
0098         T df = a[i] - value ; 
0099         if(df < 0) df = -df ; 
0100 
0101         if(df < tolerance)
0102         {   
0103             idx = i ; 
0104             break ; 
0105         }   
0106     }           
0107     return idx ; 
0108 }
0109 
0110 
0111 template <typename T>
0112 int SVec<T>::FindIndexOfValue(const std::vector<T>& a, T value )
0113 {
0114     size_t idx = std::distance( a.begin(), std::find( a.begin(), a.end(), value )) ; 
0115     return idx < a.size() ? idx : -1 ; 
0116 }
0117  
0118 
0119 
0120 template <typename T>
0121 void SVec<T>::MinMaxAvg(const std::vector<T>& t, T& mn, T& mx, T& av) 
0122 {
0123     typedef typename std::vector<T>::const_iterator IT ;    
0124     IT mn_ = std::min_element( t.begin(), t.end()  );  
0125     IT mx_ = std::max_element( t.begin(), t.end()  );  
0126     double sum = std::accumulate(t.begin(), t.end(), T(0.) );   
0127 
0128     mn = *mn_ ; 
0129     mx = *mx_ ; 
0130     av = t.size() > 0 ? sum/T(t.size()) : T(-1.) ;   
0131 }
0132 
0133 template <typename T>
0134 void SVec<T>::MinMax(const std::vector<T>& t, T& mn, T& mx ) 
0135 {
0136     typedef typename std::vector<T>::const_iterator IT ;    
0137     IT mn_ = std::min_element( t.begin(), t.end()  );  
0138     IT mx_ = std::max_element( t.begin(), t.end()  );  
0139     mn = *mn_ ; 
0140     mx = *mx_ ; 
0141 }
0142 
0143 template <typename T>
0144 void SVec<T>::Extract(std::vector<T>& a, const char* str0, const char* ignore ) 
0145 {
0146     char swap = ' '; 
0147     const char* str1 = SStr::ReplaceChars(str0, ignore, swap); 
0148     std::stringstream ss(str1);  
0149     std::string s ; 
0150     T value ; 
0151 
0152     while(std::getline(ss, s, ' '))
0153     {
0154         if(strlen(s.c_str()) == 0 ) continue;  
0155         //std::cout << "[" << s << "]" << std::endl ; 
0156         
0157         std::stringstream tt(s);  
0158         tt >> value ; 
0159         a.push_back(value); 
0160     }
0161 }
0162 
0163 
0164 template struct SVec<int>;
0165 template struct SVec<unsigned>;
0166 template struct SVec<float>;
0167 template struct SVec<double>;
0168 
0169