1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/layout/xul/tree/nsTreeStyleCache.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,111 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#ifndef nsTreeStyleCache_h__ 1.10 +#define nsTreeStyleCache_h__ 1.11 + 1.12 +#include "mozilla/Attributes.h" 1.13 +#include "nsHashtable.h" 1.14 +#include "nsIAtom.h" 1.15 +#include "nsCOMArray.h" 1.16 +#include "nsICSSPseudoComparator.h" 1.17 +#include "nsStyleContext.h" 1.18 + 1.19 +typedef nsCOMArray<nsIAtom> AtomArray; 1.20 + 1.21 +class nsDFAState : public nsHashKey 1.22 +{ 1.23 +public: 1.24 + uint32_t mStateID; 1.25 + 1.26 + nsDFAState(uint32_t aID) :mStateID(aID) {} 1.27 + 1.28 + uint32_t GetStateID() { return mStateID; } 1.29 + 1.30 + uint32_t HashCode(void) const MOZ_OVERRIDE { 1.31 + return mStateID; 1.32 + } 1.33 + 1.34 + bool Equals(const nsHashKey *aKey) const MOZ_OVERRIDE { 1.35 + nsDFAState* key = (nsDFAState*)aKey; 1.36 + return key->mStateID == mStateID; 1.37 + } 1.38 + 1.39 + nsHashKey *Clone(void) const MOZ_OVERRIDE { 1.40 + return new nsDFAState(mStateID); 1.41 + } 1.42 +}; 1.43 + 1.44 +class nsTransitionKey : public nsHashKey 1.45 +{ 1.46 +public: 1.47 + uint32_t mState; 1.48 + nsCOMPtr<nsIAtom> mInputSymbol; 1.49 + 1.50 + nsTransitionKey(uint32_t aState, nsIAtom* aSymbol) :mState(aState), mInputSymbol(aSymbol) {} 1.51 + 1.52 + uint32_t HashCode(void) const MOZ_OVERRIDE { 1.53 + // Make a 32-bit integer that combines the low-order 16 bits of the state and the input symbol. 1.54 + int32_t hb = mState << 16; 1.55 + int32_t lb = (NS_PTR_TO_INT32(mInputSymbol.get()) << 16) >> 16; 1.56 + return hb+lb; 1.57 + } 1.58 + 1.59 + bool Equals(const nsHashKey *aKey) const MOZ_OVERRIDE { 1.60 + nsTransitionKey* key = (nsTransitionKey*)aKey; 1.61 + return key->mState == mState && key->mInputSymbol == mInputSymbol; 1.62 + } 1.63 + 1.64 + nsHashKey *Clone(void) const MOZ_OVERRIDE { 1.65 + return new nsTransitionKey(mState, mInputSymbol); 1.66 + } 1.67 +}; 1.68 + 1.69 +class nsTreeStyleCache 1.70 +{ 1.71 +public: 1.72 + nsTreeStyleCache() :mTransitionTable(nullptr), mCache(nullptr), mNextState(0) {} 1.73 + ~nsTreeStyleCache() { Clear(); } 1.74 + 1.75 + void Clear() { delete mTransitionTable; mTransitionTable = nullptr; delete mCache; mCache = nullptr; mNextState = 0; } 1.76 + 1.77 + nsStyleContext* GetStyleContext(nsICSSPseudoComparator* aComparator, 1.78 + nsPresContext* aPresContext, 1.79 + nsIContent* aContent, 1.80 + nsStyleContext* aContext, 1.81 + nsIAtom* aPseudoElement, 1.82 + const AtomArray & aInputWord); 1.83 + 1.84 + static bool DeleteDFAState(nsHashKey *aKey, void *aData, void *closure); 1.85 + 1.86 + static bool ReleaseStyleContext(nsHashKey *aKey, void *aData, void *closure); 1.87 + 1.88 +protected: 1.89 + // A transition table for a deterministic finite automaton. The DFA 1.90 + // takes as its input a single pseudoelement and an ordered set of properties. 1.91 + // It transitions on an input word that is the concatenation of the pseudoelement supplied 1.92 + // with the properties in the array. 1.93 + // 1.94 + // It transitions from state to state by looking up entries in the transition table (which is 1.95 + // a mapping from (S,i)->S', where S is the current state, i is the next 1.96 + // property in the input word, and S' is the state to transition to. 1.97 + // 1.98 + // If S' is not found, it is constructed and entered into the hashtable 1.99 + // under the key (S,i). 1.100 + // 1.101 + // Once the entire word has been consumed, the final state is used 1.102 + // to reference the cache table to locate the style context. 1.103 + nsObjectHashtable* mTransitionTable; 1.104 + 1.105 + // The cache of all active style contexts. This is a hash from 1.106 + // a final state in the DFA, Sf, to the resultant style context. 1.107 + nsObjectHashtable* mCache; 1.108 + 1.109 + // An integer counter that is used when we need to make new states in the 1.110 + // DFA. 1.111 + uint32_t mNextState; 1.112 +}; 1.113 + 1.114 +#endif // nsTreeStyleCache_h__