Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer 3 // * License and Disclaimer * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/ 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. 9 // * include a list of copyright holders. * 10 // * 10 // * * 11 // * Neither the authors of this software syst 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitatio 16 // * for the full disclaimer and the limitation of liability. * 17 // * 17 // * * 18 // * This code implementation is the result 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboratio 19 // * technical work of the GEANT4 collaboration. * 20 // * By using, copying, modifying or distri 20 // * By using, copying, modifying or distributing the software (or * 21 // * any work based on the software) you ag 21 // * any work based on the software) you agree to acknowledge its * 22 // * use in resulting scientific publicati 22 // * use in resulting scientific publications, and indicate your * 23 // * acceptance of all terms of the Geant4 Sof 23 // * acceptance of all terms of the Geant4 Software license. * 24 // ******************************************* 24 // ******************************************************************** 25 // 25 // 26 #include "G4KDMap.hh" 26 #include "G4KDMap.hh" 27 #include "globals.hh" 27 #include "globals.hh" 28 #include "G4KDNode.hh" 28 #include "G4KDNode.hh" 29 #include <algorithm> 29 #include <algorithm> 30 30 31 using namespace std; 31 using namespace std; 32 32 33 using _deq_iterator = std::deque<G4KDNode_Base << 33 typedef std::deque<G4KDNode_Base*>::iterator _deq_iterator; 34 34 35 G4bool __1DSortOut::sortOutNDim::operator()(G4 << 35 bool __1DSortOut::sortOutNDim::operator()(G4KDNode_Base* const & lhs, 36 G4 << 36 G4KDNode_Base* const & rhs) //const 37 { 37 { 38 return (*lhs)[fDimension] < (*rhs)[fDimensio 38 return (*lhs)[fDimension] < (*rhs)[fDimension]; 39 } 39 } 40 40 41 __1DSortOut::__1DSortOut(std::size_t dimension << 41 __1DSortOut::__1DSortOut(size_t dimension) : 42 fSortOutNDim(dimension) 42 fSortOutNDim(dimension) 43 { 43 { 44 } 44 } 45 45 46 __1DSortOut::__1DSortOut(const __1DSortOut& ri << 46 __1DSortOut::__1DSortOut(const __1DSortOut& right) : 47 = default; << 47 fContainer(right.fContainer), fSortOutNDim(right.fSortOutNDim) >> 48 { >> 49 } 48 50 49 G4int __1DSortOut::GetDimension() << 51 int __1DSortOut::GetDimension() 50 { 52 { 51 return (G4int)fSortOutNDim.fDimension; << 53 return fSortOutNDim.fDimension; 52 } 54 } 53 55 54 G4KDNode_Base* __1DSortOut::GetMidle(std::size << 56 G4KDNode_Base* __1DSortOut::GetMidle(int& main_middle) 55 { 57 { 56 std::size_t contSize = fContainer.size(); << 58 int contSize = fContainer.size(); 57 main_middle = (std::size_t) ceil(contSize / << 59 main_middle = (int) ceil(contSize / 2.); // ceil = round up 58 return fContainer[main_middle]; 60 return fContainer[main_middle]; 59 } 61 } 60 62 61 _deq_iterator __1DSortOut::Insert(G4KDNode_Bas 63 _deq_iterator __1DSortOut::Insert(G4KDNode_Base* pos) 62 { 64 { 63 return fContainer.insert(fContainer.end(), p 65 return fContainer.insert(fContainer.end(), pos); 64 } 66 } 65 67 66 G4KDNode_Base* __1DSortOut::PopOutMiddle() 68 G4KDNode_Base* __1DSortOut::PopOutMiddle() 67 { 69 { 68 std::size_t middle; << 70 int middle; 69 G4KDNode_Base* pos = GetMidle(middle); 71 G4KDNode_Base* pos = GetMidle(middle); 70 auto deq_pos = fContainer.begin() + middle; << 72 _deq_iterator deq_pos = fContainer.begin() + middle; 71 73 72 if(deq_pos == fContainer.end()) return nullp << 74 if(deq_pos == fContainer.end()) return 0; // this is a double check 73 75 74 fContainer.erase(deq_pos); 76 fContainer.erase(deq_pos); 75 return pos; 77 return pos; 76 } 78 } 77 79 78 void __1DSortOut::Sort() 80 void __1DSortOut::Sort() 79 { 81 { 80 sort(fContainer.begin(), fContainer.end(), f 82 sort(fContainer.begin(), fContainer.end(), fSortOutNDim); 81 } 83 } 82 84 83 void __1DSortOut::Erase(_deq_iterator& deq_pos 85 void __1DSortOut::Erase(_deq_iterator& deq_pos) 84 { 86 { 85 fContainer.erase(deq_pos); 87 fContainer.erase(deq_pos); 86 } 88 } 87 89 88 void G4KDMap::Insert(G4KDNode_Base* pos) 90 void G4KDMap::Insert(G4KDNode_Base* pos) 89 { 91 { 90 vector<_deq_iterator>& vit = fMap[pos]; 92 vector<_deq_iterator>& vit = fMap[pos]; 91 93 92 std::size_t maxSize = fSortOut.size(); << 94 size_t maxSize = fSortOut.size(); 93 95 94 G4cout << "G4KDMap::Insert : " << maxSize << 96 G4cout << "G4KDMap::Insert : " << maxSize << G4endl; 95 97 >> 98 96 vit.reserve(maxSize); 99 vit.reserve(maxSize); 97 100 98 for (std::size_t i = 0; i < fSortOut.size(); << 101 for (size_t i = 0; i < fSortOut.size(); ++i) 99 { 102 { 100 vit[i] = fSortOut[i].Insert(pos); 103 vit[i] = fSortOut[i].Insert(pos); 101 104 102 // if(*(vit[i]) != pos) 105 // if(*(vit[i]) != pos) 103 // { 106 // { 104 // G4cout << "insert wrong iterator" << G 107 // G4cout << "insert wrong iterator" << G4endl; 105 // abort(); 108 // abort(); 106 // } 109 // } 107 } 110 } 108 /* 111 /* 109 std::map<G4KDNode*, std::vector<_deq_iterat 112 std::map<G4KDNode*, std::vector<_deq_iterator> >::iterator fMap_it 110 = fMap.begin(); 113 = fMap.begin(); 111 114 112 for( ; fMap_it != fMap.end() ; fMap_it++) 115 for( ; fMap_it != fMap.end() ; fMap_it++) 113 { 116 { 114 std::vector<_deq_iterator>& vit = fMap_it- 117 std::vector<_deq_iterator>& vit = fMap_it->second; 115 118 116 G4KDNode* tmpNode = fMap_it->first; 119 G4KDNode* tmpNode = fMap_it->first; 117 120 118 for(std::size_t i = 0 ; i < fSortOut.size() << 121 for(size_t i = 0 ; i < fSortOut.size() ; i++) 119 { 122 { 120 G4cout << "i = " << i << G4endl; 123 G4cout << "i = " << i << G4endl; 121 G4cout << "vit[i] = " << *(vit[i]) << G4end 124 G4cout << "vit[i] = " << *(vit[i]) << G4endl; 122 if(*(vit[i]) != tmpNode) 125 if(*(vit[i]) != tmpNode) 123 { 126 { 124 G4cout << "!!!! Wrong iterator" << G4endl; 127 G4cout << "!!!! Wrong iterator" << G4endl; 125 abort(); 128 abort(); 126 } 129 } 127 } 130 } 128 131 129 } 132 } 130 */ 133 */ 131 134 132 fIsSorted = false; 135 fIsSorted = false; 133 } 136 } 134 137 135 G4KDNode_Base* G4KDMap::PopOutMiddle(std::size << 138 G4KDNode_Base* G4KDMap::PopOutMiddle(size_t dimension) 136 { 139 { 137 G4cout << "_____________" << G4endl; 140 G4cout << "_____________" << G4endl; 138 G4cout << "G4KDMap::PopOutMiddle ( "<< dimen 141 G4cout << "G4KDMap::PopOutMiddle ( "<< dimension << " )" << G4endl; 139 142 140 if(!fIsSorted) Sort(); << 143 if(fIsSorted == false) Sort(); 141 G4KDNode_Base* output_node = fSortOut[dimens 144 G4KDNode_Base* output_node = fSortOut[dimension].PopOutMiddle(); 142 145 143 if(output_node == nullptr) return nullptr; << 146 if(output_node == 0) return 0; 144 147 145 G4cout << "output_node : " << output_node << 148 G4cout << "output_node : " << output_node << G4endl; 146 G4cout << "output_node : " << output_node->G 149 G4cout << "output_node : " << output_node->GetAxis() << G4endl; 147 150 148 auto fMap_it << 151 std::map<G4KDNode_Base*, std::vector<_deq_iterator> >::iterator fMap_it 149 = fMap.find(output_node); 152 = fMap.find(output_node); 150 153 151 154 152 if(fMap_it == fMap.end()) 155 if(fMap_it == fMap.end()) 153 { 156 { 154 G4cout << "fMap_it == fMap.end()" << G4en 157 G4cout << "fMap_it == fMap.end()" << G4endl; 155 G4cout << "output_node = " << output_node 158 G4cout << "output_node = " << output_node << G4endl; 156 return output_node; 159 return output_node; 157 } 160 } 158 161 159 std::vector<_deq_iterator>& vit = fMap_it->s 162 std::vector<_deq_iterator>& vit = fMap_it->second; 160 163 161 /* 164 /* 162 if(fMap_it->first != output_node) 165 if(fMap_it->first != output_node) 163 { 166 { 164 G4cout << "fMap_it->first ! output_node"<< 167 G4cout << "fMap_it->first ! output_node"<< G4endl; 165 G4cout << "fMap_it->first = " << fMap_it->f 168 G4cout << "fMap_it->first = " << fMap_it->first << G4endl; 166 abort(); 169 abort(); 167 } 170 } 168 */ 171 */ 169 172 170 for(std::size_t i = 0; i < fSortOut.size(); << 173 for(size_t i = 0; i < fSortOut.size(); i++) 171 { 174 { 172 if(i != dimension) 175 if(i != dimension) 173 { 176 { 174 G4cout << "i = " << i << G4endl; 177 G4cout << "i = " << i << G4endl; 175 178 176 /* 179 /* 177 // G4cout << "i = " << i << G4endl; 180 // G4cout << "i = " << i << G4endl; 178 // G4cout << "vit[i] = " << *(vit[i]) < 181 // G4cout << "vit[i] = " << *(vit[i]) << G4endl; 179 if(*(vit[i]) != output_node) 182 if(*(vit[i]) != output_node) 180 { 183 { 181 G4cout << "deleting wrong iterator" << 184 G4cout << "deleting wrong iterator" << G4endl; 182 abort(); 185 abort(); 183 } 186 } 184 */ 187 */ 185 fSortOut[i].Erase(vit[i]); 188 fSortOut[i].Erase(vit[i]); 186 } 189 } 187 } 190 } 188 191 189 fMap.erase(fMap_it); 192 fMap.erase(fMap_it); 190 193 191 return output_node; 194 return output_node; 192 } 195 } 193 196 194 void G4KDMap::Sort() 197 void G4KDMap::Sort() 195 { 198 { 196 for (auto & i : fSortOut) << 199 for (size_t i = 0; i < fSortOut.size(); ++i) 197 { 200 { 198 i.Sort(); << 201 fSortOut[i].Sort(); 199 } 202 } 200 203 201 fIsSorted = true; 204 fIsSorted = true; 202 } 205 } 203 206