Back to index

lightning-sunbird  0.9+nobinonly
Classes | Public Member Functions | Protected Types | Protected Member Functions | Static Protected Member Functions | Protected Attributes | Static Protected Attributes | Friends
nsTemplateMatchRefSet Class Reference

A set of references nsTemplateMatch objects. More...

#include <nsTemplateMatchSet.h>

Collaboration diagram for nsTemplateMatchRefSet:
Collaboration graph
[legend]

List of all members.

Classes

union  _stor_elements
 The set is implemented as a dual datastructure. More...
class  ConstIterator
 An iterator that can be used to enumerate the contents of the set. More...
struct  Entry
 Hashtable entry; holds weak reference to a match object. More...
struct  InlineMatches
 If the set is currently. More...

Public Member Functions

 nsTemplateMatchRefSet ()
 nsTemplateMatchRefSet (const nsTemplateMatchRefSet &aMatchSet)
nsTemplateMatchRefSetoperator= (const nsTemplateMatchRefSet &aMatchSet)
 ~nsTemplateMatchRefSet ()
ConstIterator First () const
 Retrieve an iterator that refers to the first element of the set.
ConstIterator Last () const
 Retrieve an iterator that refers to ``one past'' the last element of the set.
PRBool Empty () const
 Return PR_TRUE if the set is empty.
PRBool Contains (const nsTemplateMatch *aMatch) const
 Return PR_TRUE if the set contains aMatch.
PRBool Add (const nsTemplateMatch *aMatch)
 Add a match to the set.
PRBool Remove (const nsTemplateMatch *aMatch)
 Remove a match from the set.

Protected Types

enum  { kMaxInlineMatches = (sizeof(PLDHashTable) / sizeof(void*)) - 1 }

Protected Member Functions

void Init ()
 Initialize the set.
void Finish ()
 Finish the set, releasing all matches.
void CopyFrom (const nsTemplateMatchRefSet &aMatchSet)
 Copy the set's contents from another match set.
PRBool AddToTable (nsTemplateMatch *aMatch)
 Helper routine that adds the match to the hashtable.

Static Protected Member Functions

static PLDHashNumber PR_CALLBACK HashEntry (PLDHashTable *aTable, const void *aKey)
static PRBool PR_CALLBACK MatchEntry (PLDHashTable *aTable, const PLDHashEntryHdr *aHdr, const void *aKey)

Protected Attributes

union
nsTemplateMatchRefSet::_stor_elements 
mStorageElements

Static Protected Attributes

static PLDHashTableOps gOps

Friends

class ConstIterator
struct InlineMatches
union _stor_elements

Detailed Description

A set of references nsTemplateMatch objects.

Definition at line 135 of file nsTemplateMatchSet.h.


Class Documentation

union nsTemplateMatchRefSet::_stor_elements

The set is implemented as a dual datastructure.

It is initially a simple array that holds storage for kMaxInlineMatches elements. Once that capacity is exceeded, the storage is re-used for a PLDHashTable header. The hashtable allocates its entries from the normal malloc() heap.

the InlineMatches structure is implemented such that its mCount variable overlaps with the PLDHashTable's `ops' member (which is a pointer to the hashtable's callback table). On a 32-bit architecture, we're safe assuming that the value for `ops' will be larger than kMaxInlineMatches when treated as an unsigned integer. And we'd have to get pretty unlucky on a 64-bit system for us to get screwed, I think.

Instrumentation (define NSTEMPLATEMATCHSET_METER) shows that almost all of the match sets contain fewer than seven elements.

Definition at line 232 of file nsTemplateMatchSet.h.

Collaboration diagram for nsTemplateMatchRefSet::_stor_elements:
Class Members
InlineMatches mInlineMatches
PLDHashTable mTable
struct nsTemplateMatchRefSet::InlineMatches

If the set is currently.

Definition at line 209 of file nsTemplateMatchSet.h.

Collaboration diagram for nsTemplateMatchRefSet::InlineMatches:
Class Members
PRUint32 mCount
nsTemplateMatch * mEntries

Member Enumeration Documentation

anonymous enum [protected]
Enumerator:
kMaxInlineMatches 

Definition at line 199 of file nsTemplateMatchSet.h.

{ kMaxInlineMatches = (sizeof(PLDHashTable) / sizeof(void*)) - 1 };

Constructor & Destructor Documentation

Definition at line 141 of file nsTemplateMatchSet.h.

Here is the call graph for this function:

Definition at line 145 of file nsTemplateMatchSet.h.

Here is the call graph for this function:

Definition at line 157 of file nsTemplateMatchSet.h.

Here is the call graph for this function:


Member Function Documentation

Add a match to the set.

The set does not assume ownership of the match object: it only holds a weak reference. Duplicate matches are not added.

Returns:
PR_TRUE if the match was added to the set; PR_FALSE if it already existed (or could not be added for some other reason)

Definition at line 182 of file nsTemplateMatchSet.cpp.

{
    // Cast away const, we assume shared ownership.
    nsTemplateMatch* match = NS_CONST_CAST(nsTemplateMatch*, aMatch);

    PRUint32 count = mStorageElements.mInlineMatches.mCount;
    if (count < kMaxInlineMatches) {
        // There's still room in the inline matches.

        // Check for duplicates
        for (PRInt32 i = PRInt32(count) - 1; i >= 0; --i) {
            if (*(mStorageElements.mInlineMatches.mEntries[i]) == *aMatch)
                return PR_FALSE;
        }

        // Nope. Add it!
        mStorageElements.mInlineMatches.mEntries[count] = match;

        ++mStorageElements.mInlineMatches.mCount;
        return PR_TRUE;
    }

    if (count == kMaxInlineMatches) {
        // No room left in inline matches. Fault, and convert to
        // hashtable.
        nsTemplateMatch* temp[kMaxInlineMatches];
        PRInt32 i;

        // Copy pointers to a safe place
        for (i = count - 1; i >= 0; --i)
            temp[i] = mStorageElements.mInlineMatches.mEntries[i];

        // Clobber the union; we'll treat it as a hashtable now.
        if (!PL_DHashTableInit(&mStorageElements.mTable, &gOps, nsnull,
                               sizeof(Entry), PL_DHASH_MIN_SIZE)) {
            for (i = count - 1; i >= 0; --i)
                mStorageElements.mInlineMatches.mEntries[i] = temp[i];
            return PR_FALSE;
        }

        // Now that we've table-ized this thing, mCount better be a
        // big freaking number, since it's sharing space with a
        // pointer to the PLDHashTable's ops.
        NS_ASSERTION(mStorageElements.mInlineMatches.mCount > kMaxInlineMatches,
                     "wow, I thought it'd be bigger than _that_");

        // Copy the pointers into the hashtable.
        for (i = count - 1; i >= 0; --i)
            AddToTable(temp[i]);
    }

    return AddToTable(match);
}

Here is the call graph for this function:

Here is the caller graph for this function:

Helper routine that adds the match to the hashtable.

Definition at line 237 of file nsTemplateMatchSet.cpp.

{
    PLDHashEntryHdr* hdr =
        PL_DHashTableOperate(&mStorageElements.mTable, aMatch, PL_DHASH_ADD);

    if (hdr) {
        Entry* entry = NS_REINTERPRET_CAST(Entry*, hdr);

        if (! entry->mMatch) {
            entry->mMatch = aMatch;
            return PR_TRUE;
        }
    }

    return PR_FALSE;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Return PR_TRUE if the set contains aMatch.

Definition at line 162 of file nsTemplateMatchSet.cpp.

Here is the call graph for this function:

Copy the set's contents from another match set.

Definition at line 144 of file nsTemplateMatchSet.cpp.

{
    ConstIterator last = aSet.Last();
    for (ConstIterator iter = aSet.First(); iter != last; ++iter)
        Add(iter.operator->());
}

Here is the call graph for this function:

Here is the caller graph for this function:

Return PR_TRUE if the set is empty.

Definition at line 152 of file nsTemplateMatchSet.cpp.

Here is the caller graph for this function:

Finish the set, releasing all matches.

This must be called to properly release matches in the set. Yeah, yeah, this sucks.

Definition at line 113 of file nsTemplateMatchSet.cpp.

{
#ifdef NSTEMPLATEMATCHREFSET_METER
    {
        PRInt32 entries = mStorageElements.mInlineMatches.mCount <= kMaxInlineMatches
            ? PRInt32(mStorageElements.mInlineMatches.mCount)
            : mStorageElements.mTable.entryCount;

        PRInt32 count = (entries < gCountDistribution.Count())
            ? PRInt32(gCountDistribution[entries])
            : 0;

        gCountDistribution.ReplaceElementAt((void*) ++count, entries);

        --gCount;
        if (gCount == 0) {
            printf("nsTemplateMatchRefSet distribution\n");
            for (PRInt32 i = gCountDistribution.Count() - 1; i >= 0; --i)
                printf("%d: %d\n", i, PRInt32(gCountDistribution[i]));
        }
    }
#endif

    if (mStorageElements.mInlineMatches.mCount > kMaxInlineMatches)
        // It's a hashtable, so finish the table properly
        PL_DHashTableFinish(&mStorageElements.mTable);

    mStorageElements.mInlineMatches.mCount = 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Retrieve an iterator that refers to the first element of the set.

Definition at line 301 of file nsTemplateMatchSet.cpp.

{
    if (mStorageElements.mInlineMatches.mCount <= kMaxInlineMatches)
        // XXX C-style cast to avoid gcc-2.7.2.3. bustage
        return ConstIterator(this, (nsTemplateMatch**) mStorageElements.mInlineMatches.mEntries);

    Entry* entry = NS_REINTERPRET_CAST(Entry*, mStorageElements.mTable.entryStore);
    Entry* limit = entry + PL_DHASH_TABLE_SIZE(&mStorageElements.mTable);
    for ( ; entry < limit; ++entry) {
        if (ENTRY_IS_LIVE(entry))
            break;
    }

    return ConstIterator(this, entry);
}

Here is the caller graph for this function:

PLDHashNumber PR_CALLBACK nsTemplateMatchRefSet::HashEntry ( PLDHashTable aTable,
const void aKey 
) [static, protected]

Definition at line 81 of file nsTemplateMatchSet.cpp.

Here is the call graph for this function:

Initialize the set.

Must be called before the set is used, or after Finish().

Definition at line 104 of file nsTemplateMatchSet.cpp.

{
#ifdef NSTEMPLATEMATCHREFSET_METER
    ++gCount;
#endif
    mStorageElements.mInlineMatches.mCount = 0;
}

Here is the caller graph for this function:

Retrieve an iterator that refers to ``one past'' the last element of the set.

Definition at line 318 of file nsTemplateMatchSet.cpp.

{
    PRUint32 count = mStorageElements.mInlineMatches.mCount;
    if (count <= kMaxInlineMatches) {
        // XXX C-style cast to avoid gcc-2.7.2.3 bustage
        nsTemplateMatch** first = (nsTemplateMatch**) mStorageElements.mInlineMatches.mEntries;
        nsTemplateMatch** limit = first + count;
        return ConstIterator(this, limit);
    }

    Entry* limit = NS_REINTERPRET_CAST(Entry*, mStorageElements.mTable.entryStore);
    limit += PL_DHASH_TABLE_SIZE(&mStorageElements.mTable);
    return ConstIterator(this, limit);
}

Here is the caller graph for this function:

PRBool PR_CALLBACK nsTemplateMatchRefSet::MatchEntry ( PLDHashTable aTable,
const PLDHashEntryHdr aHdr,
const void aKey 
) [static, protected]

Definition at line 89 of file nsTemplateMatchSet.cpp.

{
    const Entry* entry = NS_REINTERPRET_CAST(const Entry*, aHdr);
    const nsTemplateMatch* left  = entry->mMatch;
    const nsTemplateMatch* right = NS_STATIC_CAST(const nsTemplateMatch*, aKey);
    return *left == *right;
}
nsTemplateMatchRefSet& nsTemplateMatchRefSet::operator= ( const nsTemplateMatchRefSet aMatchSet) [inline]

Definition at line 151 of file nsTemplateMatchSet.h.

                                                      {
        Finish();
        Init();
        CopyFrom(aMatchSet);
        return *this; }

Here is the call graph for this function:

Remove a match from the set.

Returns:
PR_TRUE if the match was removed from the set; PR_FALSE if the match was not present in the set.

Definition at line 255 of file nsTemplateMatchSet.cpp.

{
    PRBool found = PR_FALSE;

    PRUint32 count = mStorageElements.mInlineMatches.mCount;
    if (count <= kMaxInlineMatches) {
        nsTemplateMatch** last;

        for (PRUint32 i = 0; i < count; ++i) {
            nsTemplateMatch** entry = &mStorageElements.mInlineMatches.mEntries[i];
            nsTemplateMatch* match = *entry;

            if (*match == *aMatch) {
                found = PR_TRUE;
            }
            else if (found)
                *last = match;

            last = entry;
        }

        if (found)
            --mStorageElements.mInlineMatches.mCount;
    }
    else {
        PLDHashEntryHdr* hdr =
            PL_DHashTableOperate(&mStorageElements.mTable, aMatch, PL_DHASH_LOOKUP);

        found = PL_DHASH_ENTRY_IS_BUSY(hdr);

        if (found)
            // Remove the match. N.B., we never demote back to a list.
            PL_DHashTableOperate(&mStorageElements.mTable, aMatch, PL_DHASH_REMOVE);
    }

    return found;
}

Here is the call graph for this function:


Friends And Related Function Documentation

friend union _stor_elements [friend]

Definition at line 203 of file nsTemplateMatchSet.h.

friend class ConstIterator [friend]

Definition at line 138 of file nsTemplateMatchSet.h.

friend struct InlineMatches [friend]

Definition at line 201 of file nsTemplateMatchSet.h.


Member Data Documentation


The documentation for this class was generated from the following files: