File indexing completed on 2024-05-19 05:42:29

0001 // ct_lvtshr_uniqueid.cpp                                    -*-C++-*-
0002 
0003 /*
0004 // Copyright 2023 Codethink Ltd <codethink@codethink.co.uk>
0005 // SPDX-License-Identifier: Apache-2.0
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 <ct_lvtshr_uniqueid.h>
0021 
0022 #include <functional>
0023 
0024 namespace Codethink::lvtshr {
0025 
0026 std::size_t UniqueId::Hash::operator()(UniqueId const& id) const noexcept
0027 {
0028     std::size_t lhs = std::hash<decltype(id.m_diagramType)>{}(id.m_diagramType);
0029     std::size_t rhs = std::hash<decltype(id.m_recordNumber)>{}(id.m_recordNumber);
0030     // Based on the work from boost::hash_combine
0031     lhs ^= rhs + 0x9e3779b9 + (lhs << 6) + (lhs >> 2);
0032     return lhs;
0033 }
0034 
0035 UniqueId::UniqueId(DiagramType diagramType, long long recordNumber):
0036     m_diagramType(diagramType), m_recordNumber(recordNumber)
0037 {
0038 }
0039 
0040 bool UniqueId::operator==(UniqueId const& other) const
0041 {
0042     return this->m_diagramType == other.m_diagramType && this->m_recordNumber == other.m_recordNumber;
0043 }
0044 
0045 DiagramType UniqueId::diagramType() const
0046 {
0047     return m_diagramType;
0048 }
0049 
0050 long long UniqueId::recordNumber() const
0051 {
0052     return m_recordNumber;
0053 }
0054 
0055 std::ostream& operator<<(std::ostream& stream, const UniqueId& uid)
0056 {
0057     stream << "{" << static_cast<int>(uid.m_diagramType) << ", " << uid.m_recordNumber << "}";
0058     return stream;
0059 }
0060 
0061 } // namespace Codethink::lvtshr