Changeset 35144 in webkit for trunk/JavaScriptCore/wtf


Ignore:
Timestamp:
Jul 11, 2008, 7:24:25 PM (17 years ago)
Author:
[email protected]
Message:

2008-07-11 David Hyatt <[email protected]>

Add an insertBefore method to ListHashSet to allow for insertions in the middle of the list (rather than just
at the end).

Reviewed by Anders

  • wtf/ListHashSet.h: (WTF::::insertBefore): (WTF::::insertNodeBefore):
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/wtf/ListHashSet.h

    r30356 r35144  
    3535    // in which they are added.
    3636
    37     // In theory it would be possible to add prepend, insertAfter, insertBefore,
     37    // In theory it would be possible to add prepend, insertAfter
    3838    // and an append that moves the element to the end even if already present,
    3939    // but unclear yet if these are needed.
     
    9292        bool contains(const ValueType&) const;
    9393
    94         // the return value is a pair of an interator to the new value's location,
     94        // the return value is a pair of an iterator to the new value's location,
    9595        // and a bool that is true if an new entry was added
    9696        pair<iterator, bool> add(const ValueType&);
     97
     98        pair<iterator, bool> insertBefore(const ValueType& beforeValue, const ValueType& newValue);
     99        pair<iterator, bool> insertBefore(iterator it, const ValueType&);
    97100
    98101        void remove(const ValueType&);
     
    103106        void unlinkAndDelete(Node*);
    104107        void appendNode(Node*);
     108        void insertNodeBefore(Node* beforeNode, Node* newNode);
    105109        void deleteAllNodes();
    106110        iterator makeIterator(Node*);
     
    470474
    471475    template<typename T, typename U>
     476    pair<typename ListHashSet<T, U>::iterator, bool> ListHashSet<T, U>::insertBefore(iterator it, const ValueType& newValue)
     477    {
     478        typedef ListHashSetTranslator<ValueType, HashFunctions> Translator;
     479        pair<typename ImplType::iterator, bool> result = m_impl.template add<ValueType, NodeAllocator*, Translator>(newValue, m_allocator.get());
     480        if (result.second)
     481            insertNodeBefore(it.node(), *result.first);
     482        return std::make_pair(makeIterator(*result.first), result.second);
     483
     484    }
     485
     486    template<typename T, typename U>
     487    pair<typename ListHashSet<T, U>::iterator, bool> ListHashSet<T, U>::insertBefore(const ValueType& beforeValue, const ValueType& newValue)
     488    {
     489        insertBefore(find(beforeValue), newValue);
     490    }
     491
     492    template<typename T, typename U>
    472493    inline void ListHashSet<T, U>::remove(iterator it)
    473494    {
     
    533554
    534555    template<typename T, typename U>
     556    void ListHashSet<T, U>::insertNodeBefore(Node* beforeNode, Node* newNode)
     557    {
     558        if (!beforeNode)
     559            return appendNode(newNode);
     560       
     561        newNode->m_next = beforeNode;
     562        newNode->m_prev = beforeNode->m_prev;
     563        if (beforeNode->m_prev)
     564            beforeNode->m_prev->m_next = newNode;
     565        beforeNode->m_prev = newNode;
     566
     567        if (!newNode->m_prev)
     568            m_head = newNode;
     569    }
     570
     571    template<typename T, typename U>
    535572    void ListHashSet<T, U>::deleteAllNodes()
    536573    {
Note: See TracChangeset for help on using the changeset viewer.