From 6f2a04e5ae3ed891716c207fcb9a346f04a29af8 Mon Sep 17 00:00:00 2001 From: John Preston Date: Fri, 12 Apr 2019 16:33:21 +0400 Subject: [PATCH] Add ordering to file location types. --- .../SourceFiles/ui/image/image_location.cpp | 51 +++++++++ .../SourceFiles/ui/image/image_location.h | 101 ++++++++++++++++++ 2 files changed, 152 insertions(+) diff --git a/Telegram/SourceFiles/ui/image/image_location.cpp b/Telegram/SourceFiles/ui/image/image_location.cpp index 1e36c140d..8bae472c7 100644 --- a/Telegram/SourceFiles/ui/image/image_location.cpp +++ b/Telegram/SourceFiles/ui/image/image_location.cpp @@ -496,6 +496,57 @@ bool operator==(const StorageFileLocation &a, const StorageFileLocation &b) { Unexpected("Type in StorageFileLocation::operator==."); } +bool operator<(const StorageFileLocation &a, const StorageFileLocation &b) { + const auto valid = a.valid(); + if (valid != b.valid()) { + return !valid; + } else if (!valid) { + return false; + } + const auto type = a._type; + if (type != b._type) { + return (type < b._type); + } + + using Type = StorageFileLocation::Type; + switch (type) { + case Type::Legacy: + return std::tie(a._localId, a._volumeId, a._dcId) + < std::tie(b._localId, b._volumeId, b._dcId); + + case Type::Encrypted: + case Type::Secure: + return std::tie(a._id, a._dcId) < std::tie(b._id, b._dcId); + + case Type::Photo: + case Type::Document: + return std::tie(a._id, a._dcId, a._sizeLetter) + < std::tie(b._id, b._dcId, b._sizeLetter); + + case Type::Takeout: + return false; + + case Type::PeerPhoto: + return std::tie( + a._id, + a._sizeLetter, + a._localId, + a._volumeId, + a._dcId) + < std::tie( + b._id, + b._sizeLetter, + b._localId, + b._volumeId, + b._dcId); + + case Type::StickerSetThumb: + return std::tie(a._id, a._localId, a._volumeId, a._dcId) + < std::tie(b._id, b._localId, b._volumeId, b._dcId); + }; + Unexpected("Type in StorageFileLocation::operator==."); +} + InMemoryKey inMemoryKey(const StorageFileLocation &location) { const auto key = location.cacheKey(); return { key.high, key.low }; diff --git a/Telegram/SourceFiles/ui/image/image_location.h b/Telegram/SourceFiles/ui/image/image_location.h index c65f64616..f43067b61 100644 --- a/Telegram/SourceFiles/ui/image/image_location.h +++ b/Telegram/SourceFiles/ui/image/image_location.h @@ -99,6 +99,9 @@ private: friend bool operator==( const StorageFileLocation &a, const StorageFileLocation &b); + friend bool operator<( + const StorageFileLocation &a, + const StorageFileLocation &b); uint16 _dcId = 0; Type _type = Type::Legacy; @@ -119,6 +122,24 @@ inline bool operator!=( return !(a == b); } +inline bool operator>( + const StorageFileLocation &a, + const StorageFileLocation &b) { + return (b < a); +} + +inline bool operator<=( + const StorageFileLocation &a, + const StorageFileLocation &b) { + return !(b < a); +} + +inline bool operator>=( + const StorageFileLocation &a, + const StorageFileLocation &b) { + return !(a < b); +} + class StorageImageLocation { public: StorageImageLocation() = default; @@ -184,6 +205,11 @@ private: const StorageImageLocation &b) { return (a._file == b._file); } + friend inline bool operator<( + const StorageImageLocation &a, + const StorageImageLocation &b) { + return (a._file < b._file); + } StorageFileLocation _file; int _width = 0; @@ -197,6 +223,24 @@ inline bool operator!=( return !(a == b); } +inline bool operator>( + const StorageImageLocation &a, + const StorageImageLocation &b) { + return (b < a); +} + +inline bool operator<=( + const StorageImageLocation &a, + const StorageImageLocation &b) { + return !(b < a); +} + +inline bool operator>=( + const StorageImageLocation &a, + const StorageImageLocation &b) { + return !(a < b); +} + class WebFileLocation { public: WebFileLocation() = default; @@ -226,6 +270,12 @@ private: return (a._accessHash == b._accessHash) && (a._url == b._url); } + friend inline bool operator<( + const WebFileLocation &a, + const WebFileLocation &b) { + return std::tie(a._accessHash, a._url) + < std::tie(b._accessHash, b._url); + } }; @@ -233,6 +283,18 @@ inline bool operator!=(const WebFileLocation &a, const WebFileLocation &b) { return !(a == b); } +inline bool operator>(const WebFileLocation &a, const WebFileLocation &b) { + return (b < a); +} + +inline bool operator<=(const WebFileLocation &a, const WebFileLocation &b) { + return !(b < a); +} + +inline bool operator>=(const WebFileLocation &a, const WebFileLocation &b) { + return !(a < b); +} + struct GeoPointLocation { float64 lat = 0.; float64 lon = 0.; @@ -255,12 +317,51 @@ inline bool operator==( && (a.scale == b.scale); } +inline bool operator<( + const GeoPointLocation &a, + const GeoPointLocation &b) { + return std::tie( + a.access, + a.lat, + a.lon, + a.width, + a.height, + a.zoom, + a.scale) + < std::tie( + b.access, + b.lat, + b.lon, + b.width, + b.height, + b.zoom, + b.scale); +} + inline bool operator!=( const GeoPointLocation &a, const GeoPointLocation &b) { return !(a == b); } +inline bool operator>( + const GeoPointLocation &a, + const GeoPointLocation &b) { + return (b < a); +} + +inline bool operator<=( + const GeoPointLocation &a, + const GeoPointLocation &b) { + return !(b < a); +} + +inline bool operator>=( + const GeoPointLocation &a, + const GeoPointLocation &b) { + return !(a < b); +} + class Image; class ImagePtr { public: