Changeset 179791 in webkit


Ignore:
Timestamp:
Feb 7, 2015 6:28:48 PM (9 years ago)
Author:
Chris Dumez
Message:

Add Vector::removeFirstMatching() / removeAllMatching() methods taking lambda functions
https://bugs.webkit.org/show_bug.cgi?id=141321

Reviewed by Darin Adler.

Source/JavaScriptCore:

Use new Vector::removeFirstMatching() / removeAllMatching() methods.

Source/WebCore:

Use new Vector::removeFirstMatching() / removeAllMatching() methods.

Source/WebKit/win:

Use new Vector::removeFirstMatching() / removeAllMatching() methods.

Source/WebKit2:

Use new Vector::removeFirstMatching() / removeAllMatching() methods.

Source/WTF:

Add Vector::removeFirstMatching() / removeAllMatching() methods taking
lambda functions to match the element(s) to remove. This simplifies the
code a bit. Vector::removeAllMatching() is also more efficient than the
manual removal alternative.

  • wtf/Vector.h:

Tools:

Use new Vector::removeFirstMatching() / removeAllMatching() methods.

Location:
trunk
Files:
23 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/JavaScriptCore/ChangeLog

    r179767 r179791  
     12015-02-07  Chris Dumez  <cdumez@apple.com>
     2
     3        Add Vector::removeFirstMatching() / removeAllMatching() methods taking lambda functions
     4        https://bugs.webkit.org/show_bug.cgi?id=141321
     5
     6        Reviewed by Darin Adler.
     7
     8        Use new Vector::removeFirstMatching() / removeAllMatching() methods.
     9
    1102015-02-06  Filip Pizlo  <fpizlo@apple.com>
    211
  • trunk/Source/JavaScriptCore/profiler/ProfileNode.cpp

    r173262 r179791  
    7676        return;
    7777
    78     for (size_t i = 0; i < m_children.size(); ++i) {
    79         if (*node == m_children[i].get()) {
    80             m_children.remove(i);
    81             break;
    82         }
    83     }
     78    m_children.removeFirstMatching([node] (const RefPtr<ProfileNode>& current) {
     79        return *node == current.get();
     80    });
    8481
    8582#ifndef NDEBUG
  • trunk/Source/WTF/ChangeLog

    r179751 r179791  
     12015-02-07  Chris Dumez  <cdumez@apple.com>
     2
     3        Add Vector::removeFirstMatching() / removeAllMatching() methods taking lambda functions
     4        https://bugs.webkit.org/show_bug.cgi?id=141321
     5
     6        Reviewed by Darin Adler.
     7
     8        Add Vector::removeFirstMatching() / removeAllMatching() methods taking
     9        lambda functions to match the element(s) to remove. This simplifies the
     10        code a bit. Vector::removeAllMatching() is also more efficient than the
     11        manual removal alternative.
     12
     13        * wtf/Vector.h:
     14
    1152015-02-06  Commit Queue  <commit-queue@webkit.org>
    216
  • trunk/Source/WTF/wtf/Vector.h

    r179751 r179791  
    737737    void remove(size_t position, size_t length);
    738738    template<typename U> bool removeFirst(const U&);
     739    template<typename MatchFunction> bool removeFirstMatching(const MatchFunction&);
    739740    template<typename U> unsigned removeAll(const U&);
     741    template<typename MatchFunction> unsigned removeAllMatching(const MatchFunction&);
    740742
    741743    void removeLast()
     
    13151317inline bool Vector<T, inlineCapacity, OverflowHandler>::removeFirst(const U& value)
    13161318{
    1317     size_t index = find(value);
    1318     if (index != notFound) {
    1319         remove(index);
    1320         return true;
     1319    return removeFirstMatching([&value] (const T& current) {
     1320        return current == value;
     1321    });
     1322}
     1323
     1324template<typename T, size_t inlineCapacity, typename OverflowHandler>
     1325template<typename MatchFunction>
     1326inline bool Vector<T, inlineCapacity, OverflowHandler>::removeFirstMatching(const MatchFunction& matches)
     1327{
     1328    for (size_t i = 0; i < size(); ++i) {
     1329        if (matches(at(i))) {
     1330            remove(i);
     1331            return true;
     1332        }
    13211333    }
    13221334    return false;
     
    13261338template<typename U>
    13271339inline unsigned Vector<T, inlineCapacity, OverflowHandler>::removeAll(const U& value)
     1340{
     1341    return removeAllMatching([&value] (const T& current) {
     1342        return current == value;
     1343    });
     1344}
     1345
     1346template<typename T, size_t inlineCapacity, typename OverflowHandler>
     1347template<typename MatchFunction>
     1348inline unsigned Vector<T, inlineCapacity, OverflowHandler>::removeAllMatching(const MatchFunction& matches)
    13281349{
    13291350    iterator holeBegin = end();
     
    13311352    unsigned matchCount = 0;
    13321353    for (auto it = begin(), itEnd = end(); it != itEnd; ++it) {
    1333         if (*it == value) {
     1354        if (matches(*it)) {
    13341355            if (holeBegin == end())
    13351356                holeBegin = it;
  • trunk/Source/WebCore/ChangeLog

    r179785 r179791  
     12015-02-07  Chris Dumez  <cdumez@apple.com>
     2
     3        Add Vector::removeFirstMatching() / removeAllMatching() methods taking lambda functions
     4        https://bugs.webkit.org/show_bug.cgi?id=141321
     5
     6        Reviewed by Darin Adler.
     7
     8        Use new Vector::removeFirstMatching() / removeAllMatching() methods.
     9
    1102015-02-07  Darin Adler  <darin@apple.com>
    211
  • trunk/Source/WebCore/css/CSSParser.cpp

    r179693 r179791  
    1207812078{
    1207912079    ASSERT(m_hasFontFaceOnlyValues);
    12080     for (unsigned i = 0; i < m_parsedProperties.size();) {
    12081         CSSProperty& property = m_parsedProperties[i];
    12082         if (property.id() == CSSPropertyFontVariant && property.value()->isValueList()) {
    12083             m_parsedProperties.remove(i);
    12084             continue;
    12085         }
    12086         ++i;
    12087     }
     12080    m_parsedProperties.removeAllMatching([] (const CSSProperty& property) {
     12081        return property.id() == CSSPropertyFontVariant && property.value()->isValueList();
     12082    });
    1208812083}
    1208912084
  • trunk/Source/WebCore/css/CSSValueList.cpp

    r175513 r179791  
    5151}
    5252
    53 bool CSSValueList::removeAll(CSSValue* val)
     53bool CSSValueList::removeAll(CSSValue* value)
    5454{
    5555    // FIXME: Why even take a pointer?
    56     if (!val)
     56    if (!value)
    5757        return false;
    5858
    59     bool found = false;
    60     for (unsigned i = 0; i < m_values.size(); ++i) {
    61         if (m_values[i].get().equals(*val)) {
    62             m_values.remove(i);
    63             found = true;
    64         }
    65     }
    66 
    67     return found;
     59    return m_values.removeAllMatching([value] (const Ref<CSSValue>& current) {
     60        return current->equals(*value);
     61    }) > 0;
    6862}
    6963
  • trunk/Source/WebCore/css/MediaList.cpp

    r174300 r179791  
    186186        return false;
    187187   
    188     for (size_t i = 0; i < m_queries.size(); ++i) {
    189         MediaQuery* query = m_queries[i].get();
    190         if (*query == *parsedQuery) {
    191             m_queries.remove(i);
    192             return true;
    193         }
    194     }
    195     return false;
     188    return m_queries.removeFirstMatching([&parsedQuery] (const std::unique_ptr<MediaQuery>& query) {
     189        return *query == *parsedQuery;
     190    });
    196191}
    197192
  • trunk/Source/WebCore/css/MediaQueryMatcher.cpp

    r165547 r179791  
    131131        return;
    132132
    133     for (size_t i = 0; i < m_listeners.size(); ++i) {
    134         if (*m_listeners[i]->listener() == *listener && m_listeners[i]->query() == query) {
    135             m_listeners.remove(i);
    136             return;
    137         }
    138     }
     133    m_listeners.removeFirstMatching([listener, query] (const std::unique_ptr<Listener>& current) {
     134        return *current->listener() == *listener && current->query() == query;
     135    });
    139136}
    140137
  • trunk/Source/WebCore/css/StyleResolver.cpp

    r179368 r179791  
    11921192
    11931193        Vector<RefPtr<MaskImageOperation>>& pendingResources = m_state.maskImagesWithPendingSVGDocuments();
    1194         for (int i = pendingResources.size() - 1; i >= 0; i--) {
    1195             if (removedExternalResources.contains(pendingResources[i]))
    1196                 pendingResources.remove(i);
    1197         }
     1194        pendingResources.removeAllMatching([&removedExternalResources] (const RefPtr<MaskImageOperation>& resource) {
     1195            return removedExternalResources.contains(resource);
     1196        });
    11981197    }
    11991198}
  • trunk/Source/WebCore/dom/Element.cpp

    r179770 r179791  
    29662966    attrNode->detachFromElementWithValue(value);
    29672967
    2968     auto* attrNodeList = attrNodeListForElement(*this);
    2969     for (unsigned i = 0; i < attrNodeList->size(); ++i) {
    2970         if (attrNodeList->at(i)->qualifiedName() == attrNode->qualifiedName()) {
    2971             attrNodeList->remove(i);
    2972             if (attrNodeList->isEmpty())
    2973                 removeAttrNodeListForElement(*this);
    2974             return;
    2975         }
    2976     }
    2977     ASSERT_NOT_REACHED();
     2968    auto& attrNodeList = *attrNodeListForElement(*this);
     2969    bool found = attrNodeList.removeFirstMatching([attrNode] (const RefPtr<Attr>& attribute) {
     2970        return attribute->qualifiedName() == attrNode->qualifiedName();
     2971    });
     2972    ASSERT_UNUSED(found, found);
     2973    if (attrNodeList.isEmpty())
     2974        removeAttrNodeListForElement(*this);
    29782975}
    29792976
  • trunk/Source/WebCore/dom/EventListenerMap.cpp

    r173240 r179791  
    157157    }
    158158
    159     return 0;
    160 }
    161 
    162 static void removeFirstListenerCreatedFromMarkup(EventListenerVector* listenerVector)
    163 {
    164     bool foundListener = false;
    165 
    166     for (size_t i = 0; i < listenerVector->size(); ++i) {
    167         if (!listenerVector->at(i).listener->wasCreatedFromMarkup())
    168             continue;
    169         foundListener = true;
    170         listenerVector->remove(i);
    171         break;
    172     }
    173 
     159    return nullptr;
     160}
     161
     162static void removeFirstListenerCreatedFromMarkup(EventListenerVector& listenerVector)
     163{
     164    bool foundListener = listenerVector.removeFirstMatching([] (const RegisteredEventListener& listener) {
     165        return listener.listener->wasCreatedFromMarkup();
     166    });
    174167    ASSERT_UNUSED(foundListener, foundListener);
    175168}
     
    181174    for (unsigned i = 0; i < m_entries.size(); ++i) {
    182175        if (m_entries[i].first == eventType) {
    183             removeFirstListenerCreatedFromMarkup(m_entries[i].second.get());
     176            removeFirstListenerCreatedFromMarkup(*m_entries[i].second);
    184177            if (m_entries[i].second->isEmpty())
    185178                m_entries.remove(i);
  • trunk/Source/WebCore/dom/Node.cpp

    r179143 r179791  
    19531953        return;
    19541954
    1955     for (size_t i = 0; i < registry->size(); ++i) {
    1956         if (registry->at(i).get() == registration) {
    1957             registry->remove(i);
    1958             return;
    1959         }
    1960     }
     1955    registry->removeFirstMatching([registration] (const std::unique_ptr<MutationObserverRegistration>& current) {
     1956        return current.get() == registration;
     1957    });
    19611958}
    19621959
  • trunk/Source/WebCore/html/parser/HTMLTreeBuilder.cpp

    r178265 r179791  
    442442{
    443443    Vector<Attribute> attributes = token.attributes();
    444     for (int i = attributes.size() - 1; i >= 0; --i) {
    445         const QualifiedName& name = attributes.at(i).name();
    446         if (name.matches(nameAttr) || name.matches(actionAttr) || name.matches(promptAttr))
    447             attributes.remove(i);
    448     }
     444    attributes.removeAllMatching([] (const Attribute& attribute) {
     445        const QualifiedName& name = attribute.name();
     446        return name.matches(nameAttr) || name.matches(actionAttr) || name.matches(promptAttr);
     447    });
    449448
    450449    attributes.append(Attribute(nameAttr, isindexTag.localName()));
  • trunk/Source/WebCore/platform/graphics/texmap/TextureMapperAnimation.cpp

    r178182 r179791  
    366366void TextureMapperAnimations::remove(const String& name)
    367367{
    368     for (int i = m_animations.size() - 1; i >= 0; --i) {
    369         if (m_animations[i].name() == name)
    370             m_animations.remove(i);
    371     }
     368    m_animations.removeAllMatching([&name] (const TextureMapperAnimation& animation) {
     369        return animation.name() == name;
     370    });
    372371}
    373372
    374373void TextureMapperAnimations::remove(const String& name, AnimatedPropertyID property)
    375374{
    376     for (int i = m_animations.size() - 1; i >= 0; --i) {
    377         if (m_animations[i].name() == name && m_animations[i].property() == property)
    378             m_animations.remove(i);
    379     }
     375    m_animations.removeAllMatching([&name, property] (const TextureMapperAnimation& animation) {
     376        return animation.name() == name && animation.property() == property;
     377    });
    380378}
    381379
  • trunk/Source/WebCore/svg/animation/SVGSMILElement.cpp

    r179695 r179791  
    215215static inline void clearTimesWithDynamicOrigins(Vector<SMILTimeWithOrigin>& timeList)
    216216{
    217     for (int i = timeList.size() - 1; i >= 0; --i) {
    218         if (timeList[i].originIsScript())
    219             timeList.remove(i);
    220     }
     217    timeList.removeAllMatching([] (const SMILTimeWithOrigin& time) {
     218        return time.originIsScript();
     219    });
    221220}
    222221
  • trunk/Source/WebKit/win/ChangeLog

    r179489 r179791  
     12015-02-07  Chris Dumez  <cdumez@apple.com>
     2
     3        Add Vector::removeFirstMatching() / removeAllMatching() methods taking lambda functions
     4        https://bugs.webkit.org/show_bug.cgi?id=141321
     5
     6        Reviewed by Darin Adler.
     7
     8        Use new Vector::removeFirstMatching() / removeAllMatching() methods.
     9
    1102015-02-02  Chris Dumez  <cdumez@apple.com>
    211
  • trunk/Source/WebKit/win/WebNotificationCenter.cpp

    r176892 r179791  
    201201    ObserverListIterator end = observerList.end();
    202202
    203     int i = 0;
    204     for (ObserverListIterator it2 = observerList.begin(); it2 != end; ++it2, ++i) {
    205         IUnknown* observedObject = it2->first.get();
    206         IWebNotificationObserver* observer = it2->second.get();
    207         if (observer == anObserver && (!anObject || anObject == observedObject)) {
    208             observerList.remove(i);
    209             break;
    210         }
    211     }
     203    observerList.removeFirstMatching([anObject, anObserver] (const ObjectObserverPair& pair) {
     204        IUnknown* observedObject = pair.first.get();
     205        IWebNotificationObserver* observer = pair.second.get();
     206        return observer == anObserver && (!anObject || anObject == observedObject);
     207    });
    212208
    213209    if (observerList.isEmpty())
  • trunk/Source/WebKit2/ChangeLog

    r179786 r179791  
     12015-02-07  Chris Dumez  <cdumez@apple.com>
     2
     3        Add Vector::removeFirstMatching() / removeAllMatching() methods taking lambda functions
     4        https://bugs.webkit.org/show_bug.cgi?id=141321
     5
     6        Reviewed by Darin Adler.
     7
     8        Use new Vector::removeFirstMatching() / removeAllMatching() methods.
     9
    1102015-02-07  Tim Horton  <timothy_horton@apple.com>
    211
  • trunk/Source/WebKit2/WebProcess/WebPage/mac/PlatformCALayerRemote.cpp

    r179604 r179791  
    351351{
    352352    if (m_animations.remove(key)) {
    353         for (size_t i = 0; i < m_properties.addedAnimations.size(); ++i) {
    354             if (m_properties.addedAnimations[i].first == key) {
    355                 m_properties.addedAnimations.remove(i);
    356                 break;
    357             }
    358         }
     353        m_properties.addedAnimations.removeFirstMatching([&key] (const std::pair<String, PlatformCAAnimationRemote::Properties>& pair) {
     354            return pair.first == key;
     355        });
    359356    }
    360357    m_properties.keyPathsOfAnimationsToRemove.add(key);
  • trunk/Tools/ChangeLog

    r179788 r179791  
     12015-02-07  Chris Dumez  <cdumez@apple.com>
     2
     3        Add Vector::removeFirstMatching() / removeAllMatching() methods taking lambda functions
     4        https://bugs.webkit.org/show_bug.cgi?id=141321
     5
     6        Reviewed by Darin Adler.
     7
     8        Use new Vector::removeFirstMatching() / removeAllMatching() methods.
     9
    1102015-02-07  David Kilzer  <ddkilzer@apple.com>
    211
  • trunk/Tools/TestWebKitAPI/Tests/WTF/Vector.cpp

    r179599 r179791  
    464464}
    465465
     466TEST(WTF_Vector, RemoveFirstMatching)
     467{
     468    Vector<int> v;
     469    EXPECT_TRUE(v.isEmpty());
     470    EXPECT_FALSE(v.removeFirstMatching([] (int value) { return value > 0; }));
     471    EXPECT_FALSE(v.removeFirstMatching([] (int) { return true; }));
     472    EXPECT_FALSE(v.removeFirstMatching([] (int) { return false; }));
     473
     474    v = {3, 1, 2, 1, 2, 1, 2, 2, 1, 1, 1, 3};
     475    EXPECT_EQ(12U, v.size());
     476    EXPECT_FALSE(v.removeFirstMatching([] (int) { return false; }));
     477    EXPECT_EQ(12U, v.size());
     478    EXPECT_FALSE(v.removeFirstMatching([] (int value) { return value < 0; }));
     479    EXPECT_EQ(12U, v.size());
     480    EXPECT_TRUE(v.removeFirstMatching([] (int value) { return value < 3; }));
     481    EXPECT_EQ(11U, v.size());
     482    EXPECT_TRUE(v == Vector<int>({3, 2, 1, 2, 1, 2, 2, 1, 1, 1, 3}));
     483    EXPECT_TRUE(v.removeFirstMatching([] (int value) { return value > 2; }));
     484    EXPECT_EQ(10U, v.size());
     485    EXPECT_TRUE(v == Vector<int>({2, 1, 2, 1, 2, 2, 1, 1, 1, 3}));
     486    EXPECT_TRUE(v.removeFirstMatching([] (int value) { return value > 2; }));
     487    EXPECT_EQ(9U, v.size());
     488    EXPECT_TRUE(v == Vector<int>({2, 1, 2, 1, 2, 2, 1, 1, 1}));
     489}
     490
     491TEST(WTF_Vector, RemoveAllMatching)
     492{
     493    Vector<int> v;
     494    EXPECT_TRUE(v.isEmpty());
     495    EXPECT_FALSE(v.removeAllMatching([] (int value) { return value > 0; }));
     496    EXPECT_FALSE(v.removeAllMatching([] (int) { return true; }));
     497    EXPECT_FALSE(v.removeAllMatching([] (int) { return false; }));
     498
     499    v = {3, 1, 2, 1, 2, 1, 2, 2, 1, 1, 1, 3};
     500    EXPECT_EQ(12U, v.size());
     501    EXPECT_EQ(0U, v.removeAllMatching([] (int) { return false; }));
     502    EXPECT_EQ(12U, v.size());
     503    EXPECT_EQ(0U, v.removeAllMatching([] (int value) { return value < 0; }));
     504    EXPECT_EQ(12U, v.size());
     505    EXPECT_EQ(12U, v.removeAllMatching([] (int value) { return value > 0; }));
     506    EXPECT_TRUE(v.isEmpty());
     507
     508    v = {3, 1, 2, 1, 2, 1, 3, 2, 2, 1, 1, 1, 3};
     509    EXPECT_EQ(13U, v.size());
     510    EXPECT_EQ(3U, v.removeAllMatching([] (int value) { return value > 2; }));
     511    EXPECT_EQ(10U, v.size());
     512    EXPECT_TRUE(v == Vector<int>({1, 2, 1, 2, 1, 2, 2, 1, 1, 1}));
     513    EXPECT_EQ(6U, v.removeAllMatching([] (int value) { return value != 2; }));
     514    EXPECT_EQ(4U, v.size());
     515    EXPECT_TRUE(v == Vector<int>({2, 2, 2, 2}));
     516    EXPECT_EQ(4U, v.removeAllMatching([] (int value) { return value == 2; }));
     517    EXPECT_TRUE(v.isEmpty());
     518}
     519
    466520} // namespace TestWebKitAPI
  • trunk/Tools/WebKitTestRunner/InjectedBundle/InjectedBundle.cpp

    r179409 r179791  
    110110void InjectedBundle::willDestroyPage(WKBundlePageRef page)
    111111{
    112     size_t size = m_pages.size();
    113     for (size_t i = 0; i < size; ++i) {
    114         if (m_pages[i]->page() == page) {
    115             m_pages.remove(i);
    116             break;
    117         }
    118     }
     112    m_pages.removeFirstMatching([page] (const std::unique_ptr<InjectedBundlePage>& current) {
     113        return current->page() == page;
     114    });
    119115}
    120116
Note: See TracChangeset for help on using the changeset viewer.