00001 /* 00002 * Copyright (c) 2013, Fraunhofer FKIE 00003 * 00004 * Authors: Bastian Gaspers 00005 * 00006 * Redistribution and use in source and binary forms, with or without 00007 * modification, are permitted provided that the following conditions are met: 00008 * 00009 * * Redistributions of source code must retain the above copyright 00010 * notice, this list of conditions and the following disclaimer. 00011 * * Redistributions in binary form must reproduce the above copyright 00012 * notice, this list of conditions and the following disclaimer in the 00013 * documentation and/or other materials provided with the distribution. 00014 * * Neither the name of the Fraunhofer FKIE nor the names of its 00015 * contributors may be used to endorse or promote products derived from 00016 * this software without specific prior written permission. 00017 * 00018 * This file is part of the StructureColoring ROS package. 00019 * 00020 * The StructureColoring ROS package is free software: 00021 * you can redistribute it and/or modify it under the terms of the 00022 * GNU Lesser General Public License as published by the Free 00023 * Software Foundation, either version 3 of the License, or 00024 * (at your option) any later version. 00025 * 00026 * The StructureColoring ROS package is distributed in the hope that it will be useful, 00027 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00028 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00029 * GNU Lesser General Public License for more details. 00030 * 00031 * You should have received a copy of the GNU Lesser General Public License 00032 * along with The StructureColoring ROS package. 00033 * If not, see <http://www.gnu.org/licenses/>. 00034 */ 00035 #ifndef WEIGHTEDIDXVECTOR_H_ 00036 #define WEIGHTEDIDXVECTOR_H_ 00037 00038 #include <algorithm> 00039 00040 class WeightedIdx{ 00041 public: 00042 WeightedIdx(float w, int idx): mWeight(w), mIdx(idx) 00043 {} 00044 00045 float& weight(){return mWeight;} 00046 float weight() const {return mWeight;} 00047 00048 int& index(){return mIdx;} 00049 int index() const {return mIdx;} 00050 private: 00051 float mWeight; 00052 int mIdx; 00053 }; 00054 00055 class WeightedIdxVector{ 00056 public: 00057 typedef std::vector<int> IdxVector; 00058 00059 WeightedIdxVector(): mWeight(0.0f) 00060 {} 00061 00062 void operator+=(const WeightedIdx& add){ 00063 mWeight += add.weight(); 00064 mNodeIndices.push_back(add.index()); 00065 } 00066 00067 void operator-=(const WeightedIdx& sub){ 00068 for(IdxVector::iterator i_it = mNodeIndices.begin(); i_it != mNodeIndices.end(); ++i_it){ 00069 if (*i_it == sub.index()){ 00070 std::swap(*i_it, mNodeIndices.back()); 00071 mNodeIndices.resize(mNodeIndices.size() -1); 00072 mWeight -= sub.weight(); 00073 return; 00074 } 00075 } 00076 } 00077 00078 float& weight(){return mWeight;} 00079 float weight() const {return mWeight;} 00080 00081 IdxVector& nodeIndices(){return mNodeIndices;} 00082 const IdxVector& nodeIndices() const {return mNodeIndices;} 00083 private: 00084 float mWeight; 00085 IdxVector mNodeIndices; 00086 }; 00087 00088 class CompareWeightedIdxVector{ 00089 public: 00090 bool operator()(WeightedIdxVector wiv1, WeightedIdxVector wiv2){ 00091 return wiv1.weight() < wiv2.weight(); 00092 } 00093 }; 00094 00095 #endif /* WEIGHTEDIDXVECTOR_H_ */