00001 // ***************************************************************************** 00002 // 00003 // Copyright (c) 2015, Southwest Research Institute® (SwRI®) 00004 // All rights reserved. 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 // * Redistributions of source code must retain the above copyright 00009 // notice, this list of conditions and the following disclaimer. 00010 // * Redistributions in binary form must reproduce the above copyright 00011 // notice, this list of conditions and the following disclaimer in the 00012 // documentation and/or other materials provided with the distribution. 00013 // * Neither the name of Southwest Research Institute® (SwRI®) nor the 00014 // names of its contributors may be used to endorse or promote products 00015 // derived from this software without specific prior written permission. 00016 // 00017 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00018 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00019 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00020 // ARE DISCLAIMED. IN NO EVENT SHALL Southwest Research Institute® BE LIABLE 00021 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00022 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00023 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00024 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00025 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 00026 // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 00027 // DAMAGE. 00028 // 00029 // ***************************************************************************** 00030 00031 #ifndef SWRI_PROFILER_TOOLS_DATABASE_KEY_H_ 00032 #define SWRI_PROFILER_TOOLS_DATABASE_KEY_H_ 00033 00034 namespace swri_profiler_tools 00035 { 00036 struct DatabaseKey 00037 { 00038 private: 00039 int profile_key_; 00040 int node_key_; 00041 00042 public: 00043 DatabaseKey() : profile_key_(-1), node_key_(-1) {} 00044 DatabaseKey(int profile_key, int node_key) 00045 : profile_key_(profile_key), node_key_(node_key) {} 00046 00047 bool isValid() const { return profile_key_ >= 0 && node_key_ >= 0; } 00048 00049 int profileKey() const { return profile_key_; } 00050 int nodeKey() const { return node_key_; } 00051 00052 00053 bool operator==(const DatabaseKey &other) const 00054 { 00055 return (profile_key_ == other.profile_key_ && 00056 node_key_ == other.node_key_); 00057 } 00058 00059 bool operator!=(const DatabaseKey &other) const 00060 { 00061 return !(*this == other); 00062 } 00063 00064 bool operator<(const DatabaseKey &other) const 00065 { 00066 if (profile_key_ == other.profile_key_) { 00067 return node_key_ < other.node_key_; 00068 } else { 00069 return profile_key_ < other.profile_key_; 00070 } 00071 } 00072 }; 00073 } // namespace swri_profiler_tools 00074 00075 // Define a hash function on a DatabaseKey to support 00076 // std::unordered_map and std::unordered_set. 00077 namespace std { 00078 template <> 00079 struct hash<swri_profiler_tools::DatabaseKey> { 00080 size_t operator () (const swri_profiler_tools::DatabaseKey &key) const 00081 { 00082 // This should be a fine hash function because it's very unlikely 00083 // someone would be working with hundreds of profiles at once. 00084 return key.profileKey()*1000 + key.nodeKey(); 00085 } 00086 }; 00087 } // namespace std 00088 #endif // SWRI_PROFILER_TOOLS_DATABASE_KEY_H_ 00089