2015-04-09 17:25:05 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 11:12:37 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2002-10-03 03:43:11 +00:00
|
|
|
|
|
|
|
#ifndef __nsCheapSets_h__
|
|
|
|
#define __nsCheapSets_h__
|
|
|
|
|
2012-02-10 19:30:40 +00:00
|
|
|
#include "nsTHashtable.h"
|
2013-07-30 14:25:31 +00:00
|
|
|
#include <stdint.h>
|
2002-10-03 03:43:11 +00:00
|
|
|
|
2015-07-23 09:35:27 +00:00
|
|
|
enum nsCheapSetOperator
|
|
|
|
{
|
|
|
|
OpNext = 0, // enumerator says continue
|
|
|
|
OpRemove = 1, // enumerator says remove and continue
|
|
|
|
};
|
|
|
|
|
2002-10-03 03:43:11 +00:00
|
|
|
/**
|
2012-02-10 19:30:40 +00:00
|
|
|
* A set that takes up minimal size when there are 0 or 1 entries in the set.
|
|
|
|
* Use for cases where sizes of 0 and 1 are even slightly common.
|
2002-10-03 03:43:11 +00:00
|
|
|
*/
|
2012-02-10 19:30:40 +00:00
|
|
|
template<typename EntryType>
|
|
|
|
class nsCheapSet
|
|
|
|
{
|
2002-10-03 03:43:11 +00:00
|
|
|
public:
|
2012-02-10 19:30:40 +00:00
|
|
|
typedef typename EntryType::KeyType KeyType;
|
2015-07-23 09:35:27 +00:00
|
|
|
typedef nsCheapSetOperator (*Enumerator)(EntryType* aEntry, void* userArg);
|
2012-02-10 19:30:40 +00:00
|
|
|
|
2014-07-09 15:15:21 +00:00
|
|
|
nsCheapSet() : mState(ZERO) {}
|
|
|
|
~nsCheapSet() { Clear(); }
|
2014-05-17 23:10:53 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove all entries.
|
|
|
|
*/
|
|
|
|
void Clear()
|
2002-10-03 03:43:11 +00:00
|
|
|
{
|
2012-02-10 19:30:40 +00:00
|
|
|
switch (mState) {
|
2014-07-09 15:15:21 +00:00
|
|
|
case ZERO:
|
|
|
|
break;
|
|
|
|
case ONE:
|
|
|
|
GetSingleEntry()->~EntryType();
|
|
|
|
break;
|
|
|
|
case MANY:
|
|
|
|
delete mUnion.table;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NS_NOTREACHED("bogus state");
|
|
|
|
break;
|
2002-10-03 03:43:11 +00:00
|
|
|
}
|
2014-05-17 23:10:53 +00:00
|
|
|
mState = ZERO;
|
2002-10-03 03:43:11 +00:00
|
|
|
}
|
|
|
|
|
2012-02-10 19:30:40 +00:00
|
|
|
nsresult Put(const KeyType aVal);
|
2002-10-03 03:43:11 +00:00
|
|
|
|
2012-02-10 19:30:40 +00:00
|
|
|
void Remove(const KeyType aVal);
|
|
|
|
|
|
|
|
bool Contains(const KeyType aVal)
|
2002-10-03 03:43:11 +00:00
|
|
|
{
|
2012-02-10 19:30:40 +00:00
|
|
|
switch (mState) {
|
2014-07-09 15:15:21 +00:00
|
|
|
case ZERO:
|
|
|
|
return false;
|
|
|
|
case ONE:
|
|
|
|
return GetSingleEntry()->KeyEquals(EntryType::KeyToPointer(aVal));
|
|
|
|
case MANY:
|
|
|
|
return !!mUnion.table->GetEntry(aVal);
|
|
|
|
default:
|
|
|
|
NS_NOTREACHED("bogus state");
|
|
|
|
return false;
|
2002-10-03 03:43:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-09 15:15:21 +00:00
|
|
|
uint32_t EnumerateEntries(Enumerator aEnumFunc, void* aUserArg)
|
2012-11-20 14:21:13 +00:00
|
|
|
{
|
|
|
|
switch (mState) {
|
2014-07-09 15:15:21 +00:00
|
|
|
case ZERO:
|
|
|
|
return 0;
|
|
|
|
case ONE:
|
2015-07-23 09:35:27 +00:00
|
|
|
if (aEnumFunc(GetSingleEntry(), aUserArg) == OpRemove) {
|
2014-07-09 15:15:21 +00:00
|
|
|
GetSingleEntry()->~EntryType();
|
|
|
|
mState = ZERO;
|
|
|
|
}
|
|
|
|
return 1;
|
2015-07-23 09:35:27 +00:00
|
|
|
case MANY: {
|
|
|
|
uint32_t n = mUnion.table->Count();
|
|
|
|
for (auto iter = mUnion.table->Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
auto entry = static_cast<EntryType*>(iter.Get());
|
|
|
|
if (aEnumFunc(entry, aUserArg) == OpRemove) {
|
|
|
|
iter.Remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
2014-07-09 15:15:21 +00:00
|
|
|
default:
|
|
|
|
NS_NOTREACHED("bogus state");
|
|
|
|
return 0;
|
2012-11-20 14:21:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-10-03 03:43:11 +00:00
|
|
|
private:
|
2012-02-10 19:30:40 +00:00
|
|
|
EntryType* GetSingleEntry()
|
2002-10-03 03:43:11 +00:00
|
|
|
{
|
2012-02-10 19:30:40 +00:00
|
|
|
return reinterpret_cast<EntryType*>(&mUnion.singleEntry[0]);
|
2002-10-03 03:43:11 +00:00
|
|
|
}
|
|
|
|
|
2014-07-09 15:15:21 +00:00
|
|
|
enum SetState
|
|
|
|
{
|
2012-02-10 19:30:40 +00:00
|
|
|
ZERO,
|
|
|
|
ONE,
|
|
|
|
MANY
|
|
|
|
};
|
|
|
|
|
2014-07-09 15:15:21 +00:00
|
|
|
union
|
|
|
|
{
|
|
|
|
nsTHashtable<EntryType>* table;
|
2012-02-10 19:30:40 +00:00
|
|
|
char singleEntry[sizeof(EntryType)];
|
|
|
|
} mUnion;
|
|
|
|
enum SetState mState;
|
|
|
|
};
|
2002-10-03 03:43:11 +00:00
|
|
|
|
2012-02-10 19:30:40 +00:00
|
|
|
template<typename EntryType>
|
|
|
|
nsresult
|
|
|
|
nsCheapSet<EntryType>::Put(const KeyType aVal)
|
|
|
|
{
|
|
|
|
switch (mState) {
|
2014-07-09 15:15:21 +00:00
|
|
|
case ZERO:
|
|
|
|
new (GetSingleEntry()) EntryType(EntryType::KeyToPointer(aVal));
|
|
|
|
mState = ONE;
|
|
|
|
return NS_OK;
|
|
|
|
case ONE: {
|
|
|
|
nsTHashtable<EntryType>* table = new nsTHashtable<EntryType>();
|
|
|
|
EntryType* entry = GetSingleEntry();
|
2012-05-18 17:30:49 +00:00
|
|
|
table->PutEntry(entry->GetKey());
|
2012-02-10 19:30:40 +00:00
|
|
|
entry->~EntryType();
|
|
|
|
mUnion.table = table;
|
|
|
|
mState = MANY;
|
2002-10-03 03:43:11 +00:00
|
|
|
}
|
2015-10-30 04:31:32 +00:00
|
|
|
MOZ_FALLTHROUGH;
|
|
|
|
|
2014-07-09 15:15:21 +00:00
|
|
|
case MANY:
|
|
|
|
mUnion.table->PutEntry(aVal);
|
|
|
|
return NS_OK;
|
|
|
|
default:
|
|
|
|
NS_NOTREACHED("bogus state");
|
|
|
|
return NS_OK;
|
2002-10-03 03:43:11 +00:00
|
|
|
}
|
2012-02-10 19:30:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename EntryType>
|
|
|
|
void
|
|
|
|
nsCheapSet<EntryType>::Remove(const KeyType aVal)
|
|
|
|
{
|
|
|
|
switch (mState) {
|
2014-07-09 15:15:21 +00:00
|
|
|
case ZERO:
|
|
|
|
break;
|
|
|
|
case ONE:
|
|
|
|
if (Contains(aVal)) {
|
|
|
|
GetSingleEntry()->~EntryType();
|
|
|
|
mState = ZERO;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case MANY:
|
|
|
|
mUnion.table->RemoveEntry(aVal);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NS_NOTREACHED("bogus state");
|
|
|
|
break;
|
2002-10-03 03:43:11 +00:00
|
|
|
}
|
2012-02-10 19:30:40 +00:00
|
|
|
}
|
2002-10-03 03:43:11 +00:00
|
|
|
|
|
|
|
#endif
|