2014-06-30 19:39:45 +04: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 15:12:37 +04: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/. */
|
2005-08-11 23:42:59 +04:00
|
|
|
|
|
|
|
#ifndef nsTHashKeys_h__
|
|
|
|
#define nsTHashKeys_h__
|
|
|
|
|
|
|
|
#include "nsID.h"
|
|
|
|
#include "nsISupports.h"
|
2005-11-18 17:18:49 +03:00
|
|
|
#include "nsIHashable.h"
|
2013-03-17 11:55:16 +04:00
|
|
|
#include "nsAutoPtr.h"
|
2005-08-11 23:42:59 +04:00
|
|
|
#include "nsCOMPtr.h"
|
2015-09-16 06:49:53 +03:00
|
|
|
#include "PLDHashTable.h"
|
2013-07-12 04:16:41 +04:00
|
|
|
#include <new>
|
2005-08-11 23:42:59 +04:00
|
|
|
|
2005-11-02 23:38:21 +03:00
|
|
|
#include "nsStringGlue.h"
|
2006-06-15 17:48:29 +04:00
|
|
|
#include "nsCRTGlue.h"
|
2012-03-13 02:53:18 +04:00
|
|
|
#include "nsUnicharUtils.h"
|
2005-08-15 22:29:55 +04:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2012-03-13 02:53:18 +04:00
|
|
|
#include "mozilla/HashFunctions.h"
|
2013-08-20 20:08:00 +04:00
|
|
|
#include "mozilla/Move.h"
|
2012-03-13 02:53:18 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
// These are defined analogously to the HashString overloads in mfbt.
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
inline uint32_t
|
2012-03-13 02:53:18 +04:00
|
|
|
HashString(const nsAString& aStr)
|
|
|
|
{
|
|
|
|
return HashString(aStr.BeginReading(), aStr.Length());
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
inline uint32_t
|
2012-03-13 02:53:18 +04:00
|
|
|
HashString(const nsACString& aStr)
|
|
|
|
{
|
|
|
|
return HashString(aStr.BeginReading(), aStr.Length());
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2005-08-11 23:42:59 +04:00
|
|
|
/** @file nsHashKeys.h
|
2005-08-11 23:43:06 +04:00
|
|
|
* standard HashKey classes for nsBaseHashtable and relatives. Each of these
|
2005-08-11 23:42:59 +04:00
|
|
|
* classes follows the nsTHashtable::EntryType specification
|
|
|
|
*
|
2005-08-11 23:43:06 +04:00
|
|
|
* Lightweight keytypes provided here:
|
2005-08-11 23:42:59 +04:00
|
|
|
* nsStringHashKey
|
|
|
|
* nsCStringHashKey
|
|
|
|
* nsUint32HashKey
|
2010-10-21 04:24:16 +04:00
|
|
|
* nsUint64HashKey
|
2012-09-06 08:58:46 +04:00
|
|
|
* nsFloatHashKey
|
2015-01-29 22:41:57 +03:00
|
|
|
* nsPtrHashKey
|
2009-01-09 03:12:33 +03:00
|
|
|
* nsClearingPtrHashKey
|
2007-02-09 23:44:10 +03:00
|
|
|
* nsVoidPtrHashKey
|
|
|
|
* nsClearingVoidPtrHashKey
|
2005-08-11 23:42:59 +04:00
|
|
|
* nsISupportsHashKey
|
|
|
|
* nsIDHashKey
|
2005-08-11 23:43:11 +04:00
|
|
|
* nsDepCharHashKey
|
2007-02-09 23:44:10 +03:00
|
|
|
* nsCharPtrHashKey
|
|
|
|
* nsUnicharPtrHashKey
|
|
|
|
* nsHashableHashKey
|
2013-12-04 04:23:32 +04:00
|
|
|
* nsGenericHashKey
|
2005-08-11 23:42:59 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* hashkey wrapper using nsAString KeyType
|
|
|
|
*
|
|
|
|
* @see nsTHashtable::EntryType for specification
|
|
|
|
*/
|
2005-09-22 17:22:52 +04:00
|
|
|
class nsStringHashKey : public PLDHashEntryHdr
|
2005-08-11 23:42:59 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef const nsAString& KeyType;
|
|
|
|
typedef const nsAString* KeyTypePointer;
|
|
|
|
|
2014-07-28 21:19:06 +04:00
|
|
|
explicit nsStringHashKey(KeyTypePointer aStr) : mStr(*aStr) {}
|
2014-06-27 05:35:39 +04:00
|
|
|
nsStringHashKey(const nsStringHashKey& aToCopy) : mStr(aToCopy.mStr) {}
|
|
|
|
~nsStringHashKey() {}
|
2005-08-11 23:42:59 +04:00
|
|
|
|
|
|
|
KeyType GetKey() const { return mStr; }
|
2011-09-29 10:19:26 +04:00
|
|
|
bool KeyEquals(const KeyTypePointer aKey) const
|
2005-08-11 23:42:59 +04:00
|
|
|
{
|
|
|
|
return mStr.Equals(*aKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
static KeyTypePointer KeyToPointer(KeyType aKey) { return &aKey; }
|
|
|
|
static PLDHashNumber HashKey(const KeyTypePointer aKey)
|
|
|
|
{
|
2012-03-13 02:53:18 +04:00
|
|
|
return mozilla::HashString(*aKey);
|
|
|
|
}
|
2014-08-06 00:27:41 +04:00
|
|
|
|
|
|
|
#ifdef MOZILLA_INTERNAL_API
|
|
|
|
// To avoid double-counting, only measure the string if it is unshared.
|
|
|
|
size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const
|
|
|
|
{
|
2015-12-02 02:36:26 +03:00
|
|
|
return GetKey().SizeOfExcludingThisIfUnshared(aMallocSizeOf);
|
2014-08-06 00:27:41 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-03-13 02:53:18 +04:00
|
|
|
enum { ALLOW_MEMMOVE = true };
|
|
|
|
|
|
|
|
private:
|
|
|
|
const nsString mStr;
|
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef MOZILLA_INTERNAL_API
|
|
|
|
|
|
|
|
/**
|
|
|
|
* hashkey wrapper using nsAString KeyType
|
|
|
|
*
|
|
|
|
* This is internal-API only because nsCaseInsensitiveStringComparator is
|
|
|
|
* internal-only.
|
|
|
|
*
|
|
|
|
* @see nsTHashtable::EntryType for specification
|
|
|
|
*/
|
|
|
|
class nsStringCaseInsensitiveHashKey : public PLDHashEntryHdr
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef const nsAString& KeyType;
|
|
|
|
typedef const nsAString* KeyTypePointer;
|
|
|
|
|
2014-07-28 21:19:06 +04:00
|
|
|
explicit nsStringCaseInsensitiveHashKey(KeyTypePointer aStr)
|
2014-06-27 05:35:39 +04:00
|
|
|
: mStr(*aStr)
|
|
|
|
{
|
|
|
|
// take it easy just deal HashKey
|
|
|
|
}
|
|
|
|
nsStringCaseInsensitiveHashKey(const nsStringCaseInsensitiveHashKey& aToCopy)
|
|
|
|
: mStr(aToCopy.mStr)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
~nsStringCaseInsensitiveHashKey() {}
|
2012-03-13 02:53:18 +04:00
|
|
|
|
|
|
|
KeyType GetKey() const { return mStr; }
|
|
|
|
bool KeyEquals(const KeyTypePointer aKey) const
|
|
|
|
{
|
|
|
|
return mStr.Equals(*aKey, nsCaseInsensitiveStringComparator());
|
|
|
|
}
|
|
|
|
|
|
|
|
static KeyTypePointer KeyToPointer(KeyType aKey) { return &aKey; }
|
|
|
|
static PLDHashNumber HashKey(const KeyTypePointer aKey)
|
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
nsAutoString tmKey(*aKey);
|
|
|
|
ToLowerCase(tmKey);
|
|
|
|
return mozilla::HashString(tmKey);
|
2005-08-11 23:42:59 +04:00
|
|
|
}
|
2011-10-17 18:59:28 +04:00
|
|
|
enum { ALLOW_MEMMOVE = true };
|
2005-08-11 23:42:59 +04:00
|
|
|
|
2014-08-06 00:27:41 +04:00
|
|
|
// To avoid double-counting, only measure the string if it is unshared.
|
|
|
|
size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const
|
|
|
|
{
|
2015-12-02 02:36:26 +03:00
|
|
|
return GetKey().SizeOfExcludingThisIfUnshared(aMallocSizeOf);
|
2014-08-06 00:27:41 +04:00
|
|
|
}
|
|
|
|
|
2005-08-11 23:42:59 +04:00
|
|
|
private:
|
|
|
|
const nsString mStr;
|
|
|
|
};
|
|
|
|
|
2012-03-13 02:53:18 +04:00
|
|
|
#endif
|
|
|
|
|
2005-08-11 23:42:59 +04:00
|
|
|
/**
|
|
|
|
* hashkey wrapper using nsACString KeyType
|
|
|
|
*
|
|
|
|
* @see nsTHashtable::EntryType for specification
|
|
|
|
*/
|
2005-09-22 17:22:52 +04:00
|
|
|
class nsCStringHashKey : public PLDHashEntryHdr
|
2005-08-11 23:42:59 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef const nsACString& KeyType;
|
|
|
|
typedef const nsACString* KeyTypePointer;
|
|
|
|
|
2014-07-28 21:19:06 +04:00
|
|
|
explicit nsCStringHashKey(const nsACString* aStr) : mStr(*aStr) {}
|
2014-06-27 05:35:39 +04:00
|
|
|
nsCStringHashKey(const nsCStringHashKey& aToCopy) : mStr(aToCopy.mStr) {}
|
|
|
|
~nsCStringHashKey() {}
|
2005-08-11 23:42:59 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
KeyType GetKey() const { return mStr; }
|
2011-09-29 10:19:26 +04:00
|
|
|
bool KeyEquals(KeyTypePointer aKey) const { return mStr.Equals(*aKey); }
|
2005-08-11 23:42:59 +04:00
|
|
|
|
|
|
|
static KeyTypePointer KeyToPointer(KeyType aKey) { return &aKey; }
|
|
|
|
static PLDHashNumber HashKey(KeyTypePointer aKey)
|
|
|
|
{
|
2012-03-13 02:53:18 +04:00
|
|
|
return mozilla::HashString(*aKey);
|
2005-08-11 23:42:59 +04:00
|
|
|
}
|
2014-08-06 00:27:41 +04:00
|
|
|
|
|
|
|
#ifdef MOZILLA_INTERNAL_API
|
|
|
|
// To avoid double-counting, only measure the string if it is unshared.
|
|
|
|
size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const
|
|
|
|
{
|
2015-12-02 02:36:26 +03:00
|
|
|
return GetKey().SizeOfExcludingThisIfUnshared(aMallocSizeOf);
|
2014-08-06 00:27:41 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
enum { ALLOW_MEMMOVE = true };
|
2005-08-11 23:42:59 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
const nsCString mStr;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2012-08-22 19:56:38 +04:00
|
|
|
* hashkey wrapper using uint32_t KeyType
|
2005-08-11 23:42:59 +04:00
|
|
|
*
|
|
|
|
* @see nsTHashtable::EntryType for specification
|
|
|
|
*/
|
2005-09-22 17:22:52 +04:00
|
|
|
class nsUint32HashKey : public PLDHashEntryHdr
|
2005-08-11 23:42:59 +04:00
|
|
|
{
|
|
|
|
public:
|
2012-08-22 19:56:38 +04:00
|
|
|
typedef const uint32_t& KeyType;
|
|
|
|
typedef const uint32_t* KeyTypePointer;
|
2014-06-27 05:35:39 +04:00
|
|
|
|
2014-07-28 21:19:06 +04:00
|
|
|
explicit nsUint32HashKey(KeyTypePointer aKey) : mValue(*aKey) {}
|
2014-06-27 05:35:39 +04:00
|
|
|
nsUint32HashKey(const nsUint32HashKey& aToCopy) : mValue(aToCopy.mValue) {}
|
|
|
|
~nsUint32HashKey() {}
|
2005-08-11 23:42:59 +04:00
|
|
|
|
|
|
|
KeyType GetKey() const { return mValue; }
|
2011-09-29 10:19:26 +04:00
|
|
|
bool KeyEquals(KeyTypePointer aKey) const { return *aKey == mValue; }
|
2005-08-11 23:42:59 +04:00
|
|
|
|
|
|
|
static KeyTypePointer KeyToPointer(KeyType aKey) { return &aKey; }
|
|
|
|
static PLDHashNumber HashKey(KeyTypePointer aKey) { return *aKey; }
|
2011-10-17 18:59:28 +04:00
|
|
|
enum { ALLOW_MEMMOVE = true };
|
2005-08-11 23:42:59 +04:00
|
|
|
|
|
|
|
private:
|
2012-08-22 19:56:38 +04:00
|
|
|
const uint32_t mValue;
|
2005-08-11 23:42:59 +04:00
|
|
|
};
|
|
|
|
|
2010-10-21 04:24:16 +04:00
|
|
|
/**
|
2012-08-22 19:56:38 +04:00
|
|
|
* hashkey wrapper using uint64_t KeyType
|
2010-10-21 04:24:16 +04:00
|
|
|
*
|
|
|
|
* @see nsTHashtable::EntryType for specification
|
|
|
|
*/
|
|
|
|
class nsUint64HashKey : public PLDHashEntryHdr
|
|
|
|
{
|
|
|
|
public:
|
2012-08-22 19:56:38 +04:00
|
|
|
typedef const uint64_t& KeyType;
|
|
|
|
typedef const uint64_t* KeyTypePointer;
|
2014-06-27 05:35:39 +04:00
|
|
|
|
2014-07-28 21:19:06 +04:00
|
|
|
explicit nsUint64HashKey(KeyTypePointer aKey) : mValue(*aKey) {}
|
2014-06-27 05:35:39 +04:00
|
|
|
nsUint64HashKey(const nsUint64HashKey& aToCopy) : mValue(aToCopy.mValue) {}
|
|
|
|
~nsUint64HashKey() {}
|
2010-10-21 04:24:16 +04:00
|
|
|
|
|
|
|
KeyType GetKey() const { return mValue; }
|
2011-09-29 10:19:26 +04:00
|
|
|
bool KeyEquals(KeyTypePointer aKey) const { return *aKey == mValue; }
|
2010-10-21 04:24:16 +04:00
|
|
|
|
|
|
|
static KeyTypePointer KeyToPointer(KeyType aKey) { return &aKey; }
|
2014-06-27 05:35:39 +04:00
|
|
|
static PLDHashNumber HashKey(KeyTypePointer aKey)
|
|
|
|
{
|
|
|
|
return PLDHashNumber(*aKey);
|
|
|
|
}
|
2011-10-17 18:59:28 +04:00
|
|
|
enum { ALLOW_MEMMOVE = true };
|
2010-10-21 04:24:16 +04:00
|
|
|
|
|
|
|
private:
|
2012-08-22 19:56:38 +04:00
|
|
|
const uint64_t mValue;
|
2010-10-21 04:24:16 +04:00
|
|
|
};
|
|
|
|
|
2012-09-06 08:58:46 +04:00
|
|
|
/**
|
|
|
|
* hashkey wrapper using float KeyType
|
|
|
|
*
|
|
|
|
* @see nsTHashtable::EntryType for specification
|
|
|
|
*/
|
|
|
|
class nsFloatHashKey : public PLDHashEntryHdr
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef const float& KeyType;
|
|
|
|
typedef const float* KeyTypePointer;
|
|
|
|
|
2014-07-28 21:19:06 +04:00
|
|
|
explicit nsFloatHashKey(KeyTypePointer aKey) : mValue(*aKey) {}
|
2014-06-27 05:35:39 +04:00
|
|
|
nsFloatHashKey(const nsFloatHashKey& aToCopy) : mValue(aToCopy.mValue) {}
|
|
|
|
~nsFloatHashKey() {}
|
2012-09-06 08:58:46 +04:00
|
|
|
|
|
|
|
KeyType GetKey() const { return mValue; }
|
|
|
|
bool KeyEquals(KeyTypePointer aKey) const { return *aKey == mValue; }
|
|
|
|
|
|
|
|
static KeyTypePointer KeyToPointer(KeyType aKey) { return &aKey; }
|
2014-06-27 05:35:39 +04:00
|
|
|
static PLDHashNumber HashKey(KeyTypePointer aKey)
|
|
|
|
{
|
|
|
|
return *reinterpret_cast<const uint32_t*>(aKey);
|
|
|
|
}
|
2012-09-06 08:58:46 +04:00
|
|
|
enum { ALLOW_MEMMOVE = true };
|
|
|
|
|
|
|
|
private:
|
|
|
|
const float mValue;
|
|
|
|
};
|
|
|
|
|
2005-08-11 23:42:59 +04:00
|
|
|
/**
|
|
|
|
* hashkey wrapper using nsISupports* KeyType
|
|
|
|
*
|
|
|
|
* @see nsTHashtable::EntryType for specification
|
|
|
|
*/
|
2005-09-22 17:22:52 +04:00
|
|
|
class nsISupportsHashKey : public PLDHashEntryHdr
|
2005-08-11 23:42:59 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef nsISupports* KeyType;
|
|
|
|
typedef const nsISupports* KeyTypePointer;
|
|
|
|
|
2014-07-28 21:19:06 +04:00
|
|
|
explicit nsISupportsHashKey(const nsISupports* aKey)
|
2014-06-27 05:35:39 +04:00
|
|
|
: mSupports(const_cast<nsISupports*>(aKey))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
nsISupportsHashKey(const nsISupportsHashKey& aToCopy)
|
|
|
|
: mSupports(aToCopy.mSupports)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
~nsISupportsHashKey() {}
|
2005-08-11 23:42:59 +04:00
|
|
|
|
|
|
|
KeyType GetKey() const { return mSupports; }
|
2011-09-29 10:19:26 +04:00
|
|
|
bool KeyEquals(KeyTypePointer aKey) const { return aKey == mSupports; }
|
2005-08-11 23:42:59 +04:00
|
|
|
|
|
|
|
static KeyTypePointer KeyToPointer(KeyType aKey) { return aKey; }
|
|
|
|
static PLDHashNumber HashKey(KeyTypePointer aKey)
|
|
|
|
{
|
2014-08-04 22:29:30 +04:00
|
|
|
return NS_PTR_TO_UINT32(aKey) >> 2;
|
2005-08-11 23:42:59 +04:00
|
|
|
}
|
2011-10-17 18:59:28 +04:00
|
|
|
enum { ALLOW_MEMMOVE = true };
|
2005-08-11 23:42:59 +04:00
|
|
|
|
|
|
|
private:
|
2005-08-11 23:43:06 +04:00
|
|
|
nsCOMPtr<nsISupports> mSupports;
|
2005-08-11 23:42:59 +04:00
|
|
|
};
|
|
|
|
|
2011-12-01 01:30:55 +04:00
|
|
|
/**
|
|
|
|
* hashkey wrapper using refcounted * KeyType
|
|
|
|
*
|
|
|
|
* @see nsTHashtable::EntryType for specification
|
|
|
|
*/
|
|
|
|
template<class T>
|
|
|
|
class nsRefPtrHashKey : public PLDHashEntryHdr
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef T* KeyType;
|
|
|
|
typedef const T* KeyTypePointer;
|
|
|
|
|
2014-07-28 21:19:06 +04:00
|
|
|
explicit nsRefPtrHashKey(const T* aKey) : mKey(const_cast<T*>(aKey)) {}
|
2014-06-27 05:35:39 +04:00
|
|
|
nsRefPtrHashKey(const nsRefPtrHashKey& aToCopy) : mKey(aToCopy.mKey) {}
|
|
|
|
~nsRefPtrHashKey() {}
|
2011-12-01 01:30:55 +04:00
|
|
|
|
|
|
|
KeyType GetKey() const { return mKey; }
|
|
|
|
bool KeyEquals(KeyTypePointer aKey) const { return aKey == mKey; }
|
|
|
|
|
|
|
|
static KeyTypePointer KeyToPointer(KeyType aKey) { return aKey; }
|
|
|
|
static PLDHashNumber HashKey(KeyTypePointer aKey)
|
|
|
|
{
|
2014-08-04 22:29:30 +04:00
|
|
|
return NS_PTR_TO_UINT32(aKey) >> 2;
|
2011-12-01 01:30:55 +04:00
|
|
|
}
|
|
|
|
enum { ALLOW_MEMMOVE = true };
|
|
|
|
|
|
|
|
private:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<T> mKey;
|
2011-12-01 01:30:55 +04:00
|
|
|
};
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
template<class T>
|
2013-09-24 05:47:30 +04:00
|
|
|
inline void
|
|
|
|
ImplCycleCollectionTraverse(nsCycleCollectionTraversalCallback& aCallback,
|
|
|
|
nsRefPtrHashKey<T>& aField,
|
|
|
|
const char* aName,
|
|
|
|
uint32_t aFlags = 0)
|
|
|
|
{
|
|
|
|
CycleCollectionNoteChild(aCallback, aField.GetKey(), aName, aFlags);
|
|
|
|
}
|
|
|
|
|
2005-08-11 23:43:14 +04:00
|
|
|
/**
|
2009-01-09 03:12:33 +03:00
|
|
|
* hashkey wrapper using T* KeyType
|
2005-08-11 23:43:14 +04:00
|
|
|
*
|
|
|
|
* @see nsTHashtable::EntryType for specification
|
|
|
|
*/
|
2009-01-09 03:12:33 +03:00
|
|
|
template<class T>
|
|
|
|
class nsPtrHashKey : public PLDHashEntryHdr
|
2005-08-11 23:43:14 +04:00
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
public:
|
|
|
|
typedef T* KeyType;
|
|
|
|
typedef const T* KeyTypePointer;
|
2005-08-11 23:43:14 +04:00
|
|
|
|
2014-07-28 21:19:06 +04:00
|
|
|
explicit nsPtrHashKey(const T* aKey) : mKey(const_cast<T*>(aKey)) {}
|
2014-06-27 05:35:39 +04:00
|
|
|
nsPtrHashKey(const nsPtrHashKey<T>& aToCopy) : mKey(aToCopy.mKey) {}
|
2009-01-09 03:12:33 +03:00
|
|
|
~nsPtrHashKey() {}
|
2005-08-11 23:43:14 +04:00
|
|
|
|
|
|
|
KeyType GetKey() const { return mKey; }
|
2014-06-27 05:35:39 +04:00
|
|
|
bool KeyEquals(KeyTypePointer aKey) const { return aKey == mKey; }
|
2005-08-11 23:43:14 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
static KeyTypePointer KeyToPointer(KeyType aKey) { return aKey; }
|
|
|
|
static PLDHashNumber HashKey(KeyTypePointer aKey)
|
2005-08-11 23:43:14 +04:00
|
|
|
{
|
2014-08-04 22:29:30 +04:00
|
|
|
return NS_PTR_TO_UINT32(aKey) >> 2;
|
2005-08-11 23:43:14 +04:00
|
|
|
}
|
2011-10-17 18:59:28 +04:00
|
|
|
enum { ALLOW_MEMMOVE = true };
|
2007-01-05 01:31:26 +03:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
protected:
|
2014-12-24 05:17:50 +03:00
|
|
|
T* MOZ_NON_OWNING_REF mKey;
|
2007-01-05 01:31:26 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2013-10-11 00:41:39 +04:00
|
|
|
* hashkey wrapper using T* KeyType that sets key to nullptr upon
|
2007-01-05 01:31:26 +03:00
|
|
|
* destruction. Relevant only in cases where a memory pointer-scanner
|
|
|
|
* like valgrind might get confused about stale references.
|
|
|
|
*
|
|
|
|
* @see nsTHashtable::EntryType for specification
|
|
|
|
*/
|
|
|
|
|
2009-01-09 03:12:33 +03:00
|
|
|
template<class T>
|
|
|
|
class nsClearingPtrHashKey : public nsPtrHashKey<T>
|
2007-01-05 01:31:26 +03:00
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
public:
|
2014-07-28 21:19:06 +04:00
|
|
|
explicit nsClearingPtrHashKey(const T* aKey) : nsPtrHashKey<T>(aKey) {}
|
2014-06-27 05:35:39 +04:00
|
|
|
nsClearingPtrHashKey(const nsClearingPtrHashKey<T>& aToCopy)
|
|
|
|
: nsPtrHashKey<T>(aToCopy)
|
|
|
|
{
|
|
|
|
}
|
2012-07-30 18:20:58 +04:00
|
|
|
~nsClearingPtrHashKey() { nsPtrHashKey<T>::mKey = nullptr; }
|
2005-08-11 23:43:14 +04:00
|
|
|
};
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
typedef nsPtrHashKey<const void> nsVoidPtrHashKey;
|
2009-01-09 03:12:33 +03:00
|
|
|
typedef nsClearingPtrHashKey<const void> nsClearingVoidPtrHashKey;
|
|
|
|
|
2013-07-09 18:28:15 +04:00
|
|
|
/**
|
|
|
|
* hashkey wrapper using a function pointer KeyType
|
|
|
|
*
|
|
|
|
* @see nsTHashtable::EntryType for specification
|
|
|
|
*/
|
|
|
|
template<class T>
|
|
|
|
class nsFuncPtrHashKey : public PLDHashEntryHdr
|
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
public:
|
|
|
|
typedef T& KeyType;
|
|
|
|
typedef const T* KeyTypePointer;
|
2013-07-09 18:28:15 +04:00
|
|
|
|
2014-07-28 21:19:06 +04:00
|
|
|
explicit nsFuncPtrHashKey(const T* aKey) : mKey(*const_cast<T*>(aKey)) {}
|
2014-06-27 05:35:39 +04:00
|
|
|
nsFuncPtrHashKey(const nsFuncPtrHashKey<T>& aToCopy) : mKey(aToCopy.mKey) {}
|
2013-07-09 18:28:15 +04:00
|
|
|
~nsFuncPtrHashKey() {}
|
|
|
|
|
|
|
|
KeyType GetKey() const { return const_cast<T&>(mKey); }
|
2014-06-27 05:35:39 +04:00
|
|
|
bool KeyEquals(KeyTypePointer aKey) const { return *aKey == mKey; }
|
2013-07-09 18:28:15 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
static KeyTypePointer KeyToPointer(KeyType aKey) { return &aKey; }
|
|
|
|
static PLDHashNumber HashKey(KeyTypePointer aKey)
|
2013-07-09 18:28:15 +04:00
|
|
|
{
|
2014-08-04 22:29:30 +04:00
|
|
|
return NS_PTR_TO_UINT32(*aKey) >> 2;
|
2013-07-09 18:28:15 +04:00
|
|
|
}
|
|
|
|
enum { ALLOW_MEMMOVE = true };
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
protected:
|
2013-07-09 18:28:15 +04:00
|
|
|
T mKey;
|
|
|
|
};
|
|
|
|
|
2005-08-11 23:42:59 +04:00
|
|
|
/**
|
|
|
|
* hashkey wrapper using nsID KeyType
|
|
|
|
*
|
|
|
|
* @see nsTHashtable::EntryType for specification
|
|
|
|
*/
|
2005-09-22 17:22:52 +04:00
|
|
|
class nsIDHashKey : public PLDHashEntryHdr
|
2005-08-11 23:42:59 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef const nsID& KeyType;
|
|
|
|
typedef const nsID* KeyTypePointer;
|
|
|
|
|
2014-07-28 21:19:06 +04:00
|
|
|
explicit nsIDHashKey(const nsID* aInID) : mID(*aInID) {}
|
2014-06-27 05:35:39 +04:00
|
|
|
nsIDHashKey(const nsIDHashKey& aToCopy) : mID(aToCopy.mID) {}
|
|
|
|
~nsIDHashKey() {}
|
2005-08-11 23:43:15 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
KeyType GetKey() const { return mID; }
|
2011-09-29 10:19:26 +04:00
|
|
|
bool KeyEquals(KeyTypePointer aKey) const { return aKey->Equals(mID); }
|
2005-08-11 23:42:59 +04:00
|
|
|
|
|
|
|
static KeyTypePointer KeyToPointer(KeyType aKey) { return &aKey; }
|
2012-03-13 02:53:18 +04:00
|
|
|
static PLDHashNumber HashKey(KeyTypePointer aKey)
|
|
|
|
{
|
|
|
|
// Hash the nsID object's raw bytes.
|
|
|
|
return mozilla::HashBytes(aKey, sizeof(KeyType));
|
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
enum { ALLOW_MEMMOVE = true };
|
2005-08-11 23:42:59 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
const nsID mID;
|
|
|
|
};
|
|
|
|
|
2005-08-11 23:43:11 +04:00
|
|
|
/**
|
|
|
|
* hashkey wrapper for "dependent" const char*; this class does not "own"
|
|
|
|
* its string pointer.
|
|
|
|
*
|
|
|
|
* This class must only be used if the strings have a lifetime longer than
|
|
|
|
* the hashtable they occupy. This normally occurs only for static
|
|
|
|
* strings or strings that have been arena-allocated.
|
|
|
|
*
|
|
|
|
* @see nsTHashtable::EntryType for specification
|
|
|
|
*/
|
2005-09-22 17:22:52 +04:00
|
|
|
class nsDepCharHashKey : public PLDHashEntryHdr
|
2005-08-11 23:43:11 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef const char* KeyType;
|
|
|
|
typedef const char* KeyTypePointer;
|
|
|
|
|
2014-07-28 21:19:06 +04:00
|
|
|
explicit nsDepCharHashKey(const char* aKey) : mKey(aKey) {}
|
2014-06-27 05:35:39 +04:00
|
|
|
nsDepCharHashKey(const nsDepCharHashKey& aToCopy) : mKey(aToCopy.mKey) {}
|
|
|
|
~nsDepCharHashKey() {}
|
2005-08-11 23:43:11 +04:00
|
|
|
|
|
|
|
const char* GetKey() const { return mKey; }
|
2014-06-27 05:35:39 +04:00
|
|
|
bool KeyEquals(const char* aKey) const { return !strcmp(mKey, aKey); }
|
2005-08-11 23:43:11 +04:00
|
|
|
|
|
|
|
static const char* KeyToPointer(const char* aKey) { return aKey; }
|
2014-06-27 05:35:39 +04:00
|
|
|
static PLDHashNumber HashKey(const char* aKey)
|
|
|
|
{
|
|
|
|
return mozilla::HashString(aKey);
|
|
|
|
}
|
2011-10-17 18:59:28 +04:00
|
|
|
enum { ALLOW_MEMMOVE = true };
|
2005-08-11 23:43:11 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
const char* mKey;
|
|
|
|
};
|
|
|
|
|
2005-08-11 23:43:13 +04:00
|
|
|
/**
|
|
|
|
* hashkey wrapper for const char*; at construction, this class duplicates
|
|
|
|
* a string pointed to by the pointer so that it doesn't matter whether or not
|
|
|
|
* the string lives longer than the hash table.
|
|
|
|
*/
|
2005-09-22 17:22:52 +04:00
|
|
|
class nsCharPtrHashKey : public PLDHashEntryHdr
|
2005-08-11 23:43:13 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef const char* KeyType;
|
|
|
|
typedef const char* KeyTypePointer;
|
|
|
|
|
2014-07-28 21:19:06 +04:00
|
|
|
explicit nsCharPtrHashKey(const char* aKey) : mKey(strdup(aKey)) {}
|
2014-06-27 05:35:39 +04:00
|
|
|
nsCharPtrHashKey(const nsCharPtrHashKey& aToCopy)
|
|
|
|
: mKey(strdup(aToCopy.mKey))
|
2013-08-20 20:08:00 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
nsCharPtrHashKey(nsCharPtrHashKey&& aOther)
|
|
|
|
: mKey(aOther.mKey)
|
|
|
|
{
|
|
|
|
aOther.mKey = nullptr;
|
|
|
|
}
|
2005-08-11 23:43:13 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
~nsCharPtrHashKey()
|
2005-08-11 23:43:13 +04:00
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
if (mKey) {
|
|
|
|
free(const_cast<char*>(mKey));
|
|
|
|
}
|
2005-08-11 23:43:13 +04:00
|
|
|
}
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
const char* GetKey() const { return mKey; }
|
|
|
|
bool KeyEquals(KeyTypePointer aKey) const { return !strcmp(mKey, aKey); }
|
|
|
|
|
2005-08-11 23:43:13 +04:00
|
|
|
static KeyTypePointer KeyToPointer(KeyType aKey) { return aKey; }
|
2014-06-27 05:35:39 +04:00
|
|
|
static PLDHashNumber HashKey(KeyTypePointer aKey)
|
|
|
|
{
|
|
|
|
return mozilla::HashString(aKey);
|
|
|
|
}
|
2005-08-11 23:43:13 +04:00
|
|
|
|
2014-08-06 00:27:41 +04:00
|
|
|
size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const
|
2014-07-31 00:00:29 +04:00
|
|
|
{
|
|
|
|
return aMallocSizeOf(mKey);
|
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
enum { ALLOW_MEMMOVE = true };
|
2005-08-11 23:43:13 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
const char* mKey;
|
|
|
|
};
|
|
|
|
|
2006-06-15 17:48:29 +04:00
|
|
|
/**
|
2014-01-04 19:02:17 +04:00
|
|
|
* hashkey wrapper for const char16_t*; at construction, this class duplicates
|
2006-06-15 17:48:29 +04:00
|
|
|
* a string pointed to by the pointer so that it doesn't matter whether or not
|
|
|
|
* the string lives longer than the hash table.
|
|
|
|
*/
|
|
|
|
class nsUnicharPtrHashKey : public PLDHashEntryHdr
|
|
|
|
{
|
|
|
|
public:
|
2014-01-04 19:02:17 +04:00
|
|
|
typedef const char16_t* KeyType;
|
|
|
|
typedef const char16_t* KeyTypePointer;
|
2006-06-15 17:48:29 +04:00
|
|
|
|
2014-07-28 21:19:06 +04:00
|
|
|
explicit nsUnicharPtrHashKey(const char16_t* aKey) : mKey(NS_strdup(aKey)) {}
|
2014-06-27 05:35:39 +04:00
|
|
|
nsUnicharPtrHashKey(const nsUnicharPtrHashKey& aToCopy)
|
|
|
|
: mKey(NS_strdup(aToCopy.mKey))
|
2013-08-20 20:08:00 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
nsUnicharPtrHashKey(nsUnicharPtrHashKey&& aOther)
|
|
|
|
: mKey(aOther.mKey)
|
|
|
|
{
|
|
|
|
aOther.mKey = nullptr;
|
|
|
|
}
|
2006-06-15 17:48:29 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
~nsUnicharPtrHashKey()
|
2006-06-15 17:48:29 +04:00
|
|
|
{
|
2014-06-27 05:35:39 +04:00
|
|
|
if (mKey) {
|
|
|
|
NS_Free(const_cast<char16_t*>(mKey));
|
|
|
|
}
|
2006-06-15 17:48:29 +04:00
|
|
|
}
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
const char16_t* GetKey() const { return mKey; }
|
|
|
|
bool KeyEquals(KeyTypePointer aKey) const { return !NS_strcmp(mKey, aKey); }
|
|
|
|
|
2006-06-15 17:48:29 +04:00
|
|
|
static KeyTypePointer KeyToPointer(KeyType aKey) { return aKey; }
|
2014-06-27 05:35:39 +04:00
|
|
|
static PLDHashNumber HashKey(KeyTypePointer aKey)
|
|
|
|
{
|
|
|
|
return mozilla::HashString(aKey);
|
|
|
|
}
|
2006-06-15 17:48:29 +04:00
|
|
|
|
2014-08-06 00:27:41 +04:00
|
|
|
size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const
|
|
|
|
{
|
|
|
|
return aMallocSizeOf(mKey);
|
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
enum { ALLOW_MEMMOVE = true };
|
2006-06-15 17:48:29 +04:00
|
|
|
|
|
|
|
private:
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t* mKey;
|
2006-06-15 17:48:29 +04:00
|
|
|
};
|
|
|
|
|
2005-11-18 17:18:49 +03:00
|
|
|
/**
|
|
|
|
* Hashtable key class to use with objects that support nsIHashable
|
|
|
|
*/
|
|
|
|
class nsHashableHashKey : public PLDHashEntryHdr
|
|
|
|
{
|
|
|
|
public:
|
2014-06-27 05:35:39 +04:00
|
|
|
typedef nsIHashable* KeyType;
|
|
|
|
typedef const nsIHashable* KeyTypePointer;
|
|
|
|
|
2014-07-28 21:19:06 +04:00
|
|
|
explicit nsHashableHashKey(const nsIHashable* aKey)
|
2014-06-27 05:35:39 +04:00
|
|
|
: mKey(const_cast<nsIHashable*>(aKey))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
nsHashableHashKey(const nsHashableHashKey& aToCopy) : mKey(aToCopy.mKey) {}
|
|
|
|
~nsHashableHashKey() {}
|
|
|
|
|
|
|
|
nsIHashable* GetKey() const { return mKey; }
|
|
|
|
|
|
|
|
bool KeyEquals(const nsIHashable* aKey) const
|
|
|
|
{
|
|
|
|
bool eq;
|
|
|
|
if (NS_SUCCEEDED(mKey->Equals(const_cast<nsIHashable*>(aKey), &eq))) {
|
|
|
|
return eq;
|
2005-11-18 17:18:49 +03:00
|
|
|
}
|
2014-06-27 05:35:39 +04:00
|
|
|
return false;
|
|
|
|
}
|
2005-11-18 17:18:49 +03:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
static const nsIHashable* KeyToPointer(nsIHashable* aKey) { return aKey; }
|
|
|
|
static PLDHashNumber HashKey(const nsIHashable* aKey)
|
|
|
|
{
|
|
|
|
uint32_t code = 8888; // magic number if GetHashCode fails :-(
|
2012-06-25 23:59:42 +04:00
|
|
|
#ifdef DEBUG
|
2014-06-27 05:35:39 +04:00
|
|
|
nsresult rv =
|
2005-12-01 22:35:27 +03:00
|
|
|
#endif
|
2014-06-27 05:35:39 +04:00
|
|
|
const_cast<nsIHashable*>(aKey)->GetHashCode(&code);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "GetHashCode should not throw!");
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum { ALLOW_MEMMOVE = true };
|
2005-11-18 17:18:49 +03:00
|
|
|
|
|
|
|
private:
|
2014-06-27 05:35:39 +04:00
|
|
|
nsCOMPtr<nsIHashable> mKey;
|
2005-11-18 17:18:49 +03:00
|
|
|
};
|
|
|
|
|
2015-08-27 04:19:31 +03:00
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
PLDHashNumber
|
|
|
|
Hash(const T& aValue)
|
|
|
|
{
|
|
|
|
return aValue.Hash();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2013-12-04 04:23:32 +04:00
|
|
|
/**
|
|
|
|
* Hashtable key class to use with objects for which Hash() and operator==()
|
|
|
|
* are defined.
|
|
|
|
*/
|
2014-06-27 05:35:39 +04:00
|
|
|
template<typename T>
|
2013-12-04 04:23:32 +04:00
|
|
|
class nsGenericHashKey : public PLDHashEntryHdr
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef const T& KeyType;
|
|
|
|
typedef const T* KeyTypePointer;
|
|
|
|
|
2014-07-28 21:19:06 +04:00
|
|
|
explicit nsGenericHashKey(KeyTypePointer aKey) : mKey(*aKey) {}
|
2014-06-27 05:35:39 +04:00
|
|
|
nsGenericHashKey(const nsGenericHashKey<T>& aOther) : mKey(aOther.mKey) {}
|
2013-12-04 04:23:32 +04:00
|
|
|
|
|
|
|
KeyType GetKey() const { return mKey; }
|
|
|
|
bool KeyEquals(KeyTypePointer aKey) const { return *aKey == mKey; }
|
|
|
|
|
|
|
|
static KeyTypePointer KeyToPointer(KeyType aKey) { return &aKey; }
|
2015-08-27 04:19:31 +03:00
|
|
|
static PLDHashNumber HashKey(KeyTypePointer aKey) { return ::mozilla::Hash(*aKey); }
|
2013-12-04 04:23:32 +04:00
|
|
|
enum { ALLOW_MEMMOVE = true };
|
|
|
|
|
|
|
|
private:
|
|
|
|
T mKey;
|
|
|
|
};
|
|
|
|
|
2005-08-11 23:42:59 +04:00
|
|
|
#endif // nsTHashKeys_h__
|