Back to home page

EIC code displayed by LXR

 
 

    


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

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 
0021 #include <string>
0022 #include <cstring>
0023 #include <cassert>
0024 #include <sstream>
0025 #include <iomanip>
0026 #include <iostream>
0027 
0028 #include "SLOG.hh"
0029 #include "STranche.hh"
0030 
0031 
0032 STranche::STranche(unsigned total_, unsigned max_tranche_) 
0033     :
0034     total(total_),
0035     max_tranche(max_tranche_),
0036     num_tranche((total+max_tranche-1)/max_tranche),
0037     last_tranche(total - (num_tranche-1)*max_tranche)  // <-- is max_tranche when  total % max_tranche == 0 
0038 {
0039 }
0040 
0041 unsigned STranche::tranche_size(unsigned i) const 
0042 {
0043     assert( i < num_tranche && " trance indices must be from 0 to tr.num_tranche - 1 inclusive  " ); 
0044     return i < num_tranche - 1 ? max_tranche : last_tranche  ; 
0045 }
0046 unsigned STranche::global_index(unsigned i, unsigned j ) const 
0047 {
0048     return max_tranche*i + j ; 
0049 }
0050 
0051 
0052 const char* STranche::desc() const 
0053 {
0054     std::stringstream ss ; 
0055 
0056     ss << "STranche"
0057        << " total " << total 
0058        << " max_tranche " << max_tranche 
0059        << " num_tranche " << num_tranche 
0060        << " last_tranche " << last_tranche 
0061        ;
0062 
0063     std::string s = ss.str();
0064     return strdup(s.c_str());
0065 }
0066 
0067 
0068 void STranche::dump(const char* msg)
0069 {
0070     LOG(info) << msg << " desc " << desc() ; 
0071 
0072     unsigned cumsum = 0 ; 
0073     for(unsigned i=0 ; i < num_tranche ; i++)
0074     {
0075          unsigned size = tranche_size(i) ; 
0076          cumsum += size ; 
0077 
0078          unsigned global_index_0 = global_index(i, 0);
0079          unsigned global_index_1 = global_index(i, size-1); 
0080 
0081          std::cout << " i " << std::setw(6) << i 
0082                    << " tranche_size " << std::setw(6) << size
0083                    << " global_index_0 " << std::setw(6) << global_index_0
0084                    << " global_index_1 " << std::setw(6) << global_index_1
0085                    << " cumsum " << std::setw(6) << cumsum
0086                    << std::endl 
0087                    ;
0088 
0089          assert( cumsum == global_index_1 + 1 );
0090     }
0091     assert( cumsum == total ); 
0092 }
0093 
0094 
0095