Skip to content
This repository has been archived by the owner on Feb 25, 2025. It is now read-only.

Commit

Permalink
Use ordered set
Browse files Browse the repository at this point in the history
  • Loading branch information
bdero committed May 14, 2021
1 parent d023abf commit 5d648fc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
5 changes: 2 additions & 3 deletions lib/ui/painting/image_generator_registry.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,8 @@ ImageGeneratorRegistry::~ImageGeneratorRegistry() = default;

void ImageGeneratorRegistry::AddFactory(ImageGeneratorFactory factory,
int32_t priority) {
image_generator_factories_.push_back({factory, priority, fml::tracing::TraceNonce()});
std::stable_sort(image_generator_factories_.begin(),
image_generator_factories_.end());
image_generator_factories_.insert(
{factory, priority, fml::tracing::TraceNonce()});
}

std::unique_ptr<ImageGenerator>
Expand Down
22 changes: 13 additions & 9 deletions lib/ui/painting/image_generator_registry.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#define FLUTTER_LIB_UI_PAINTING_IMAGE_GENERATOR_REGISTRY_H_

#include <functional>
#include <vector>
#include <set>

#include "flutter/fml/mapping.h"
#include "flutter/fml/memory/weak_ptr.h"
Expand Down Expand Up @@ -63,19 +63,23 @@ class ImageGeneratorRegistry {
int32_t priority = 0;
// Used as a fallback priority comparison when equal.
size_t ascending_nonce = 0;
};

// Order by descending priority.
constexpr bool operator<(const PrioritizedFactory& other) const {
// When priorities are equal, use the order by which the factories were
// registered.
if (priority == other.priority) {
return ascending_nonce < other.ascending_nonce;
struct Compare {
constexpr bool operator()(const PrioritizedFactory& lhs,
const PrioritizedFactory& rhs) const {
// When priorities are equal, factories registered earlier take
// precedent.
if (lhs.priority == rhs.priority) {
return lhs.ascending_nonce < rhs.ascending_nonce;
}
// Order by descending priority.
return priority > other.priority;
return lhs.priority > rhs.priority;
}
};
std::vector<PrioritizedFactory> image_generator_factories_;

using FactorySet = std::set<PrioritizedFactory, Compare>;
FactorySet image_generator_factories_;
fml::WeakPtrFactory<ImageGeneratorRegistry> weak_factory_;
};

Expand Down

0 comments on commit 5d648fc

Please sign in to comment.