removing empty AppliedClassProperties objects

This commit is contained in:
Konstantin Käfer
2014-07-01 12:50:08 +02:00
parent b9fbb4f80b
commit 475f0481b3
4 changed files with 29 additions and 3 deletions

View File

@@ -31,6 +31,7 @@ public:
void add(ClassID class_id, timestamp begin, timestamp end, const PropertyValue &value);
bool hasTransitions() const;
void cleanup(timestamp now);
bool empty() const;
};
}

View File

@@ -24,13 +24,29 @@ bool AppliedClassProperties::hasTransitions() const {
// Erase all items in the property list that are before a completed transition.
// Then, if the only remaining property is a Fallback value, remove it too.
void AppliedClassProperties::cleanup(timestamp now) {
// Iterate backwards, but without using the rbegin/rend interface since we need forward
// iterators to use .erase().
for (auto it = properties.end(), begin = properties.begin(); it != begin;) {
// If the property is finished, break iteration and delete all remaining items.
if ((--it)->end <= now) {
// Removes all items that precede the current iterator, but *not* the element currently
// pointed to by the iterator. This preserves the last completed transition as the
// first element in the property list.
properties.erase(begin, it);
// Also erase the pivot element if it's a fallback value. This means we can remove the
// entire applied properties object as well, because we already have the fallback
// value set as the default.
if (it->name == ClassID::Fallback) {
properties.erase(it);
}
break;
}
}
}
bool AppliedClassProperties::empty() const {
return properties.empty();
}
}

View File

@@ -5,7 +5,6 @@ namespace llmr {
const PropertyTransition &ClassProperties::getTransition(PropertyKey key, const PropertyTransition &defaultTransition) const {
auto it = transitions.find(key);
if (it == transitions.end()) {
fprintf(stderr, "using default transition: %d/%d\n", defaultTransition.duration, defaultTransition.delay);
return defaultTransition;
} else {
return it->second;

View File

@@ -290,8 +290,18 @@ bool StyleLayer::hasTransitions() const {
void StyleLayer::cleanupAppliedStyleProperties(timestamp now) {
for (std::pair<const PropertyKey, AppliedClassProperties> &pair : appliedStyle) {
pair.second.cleanup(now);
auto it = appliedStyle.begin();
const auto end = appliedStyle.end();
while (it != end) {
AppliedClassProperties &properties = it->second;
properties.cleanup(now);
// If the current properties object is empty, remove it from the map entirely.
if (properties.empty()) {
appliedStyle.erase(it++);
} else {
++it;
}
}
}