VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/xpcom/ds/nsCheapSets.h@ 44770

最後變更 在這個檔案從44770是 21113,由 vboxsync 提交於 15 年 前

xpcom: first go at making xpcom bitness-safe

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 5.2 KB
 
1/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2/* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 *
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
14 *
15 * The Original Code is Mozilla Communicator client code.
16 *
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
21 *
22 * Contributor(s):
23 *
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
35 *
36 * ***** END LICENSE BLOCK ***** */
37
38#ifndef __nsCheapSets_h__
39#define __nsCheapSets_h__
40
41#include "nsHashSets.h"
42
43/**
44 * A string set that takes up minimal size when there are 0 or 1 strings in the
45 * set. Use for cases where sizes of 0 and 1 are even slightly common.
46 */
47class NS_COM nsCheapStringSet {
48public:
49 nsCheapStringSet() : mValOrHash(nsnull)
50 {
51 }
52 ~nsCheapStringSet();
53
54 /**
55 * Put a string into the set
56 * @param aVal the value to put in
57 */
58 nsresult Put(const nsAString& aVal);
59
60 /**
61 * Remove a string from the set
62 * @param aVal the string to remove
63 */
64 void Remove(const nsAString& aVal);
65
66 /**
67 * Check if the set contains a particular string
68 * @param aVal the string to check for
69 * @return whether the string is in the set
70 */
71 PRBool Contains(const nsAString& aVal)
72 {
73 nsStringHashSet* set = GetHash();
74 // Check the value from the hash if the hash is there
75 if (set) {
76 return set->Contains(aVal);
77 }
78
79 // Check whether the value is equal to the string if the string is there
80 nsAString* str = GetStr();
81 return str && str->Equals(aVal);
82 }
83
84private:
85 typedef PRUint64 PtrBits;
86
87 /** Get the hash pointer (or null if we're not a hash) */
88 nsStringHashSet* GetHash()
89 {
90 return (PtrBits(mValOrHash) & 0x1) ? nsnull : (nsStringHashSet*)mValOrHash;
91 }
92 /** Find out whether it is a string */
93 nsAString* GetStr()
94 {
95 return (PtrBits(mValOrHash) & 0x1)
96 ? (nsAString*)(PtrBits(mValOrHash) & ~0x1)
97 : nsnull;
98 }
99 /** Set the single string */
100 nsresult SetStr(const nsAString& aVal)
101 {
102 nsString* str = new nsString(aVal);
103 if (!str) {
104 return NS_ERROR_OUT_OF_MEMORY;
105 }
106 mValOrHash = (nsAString*)(PtrBits(str) | 0x1);
107 return NS_OK;
108 }
109 /** Initialize the hash */
110 nsresult InitHash(nsStringHashSet** aSet);
111
112private:
113 /** A hash or string ptr, depending on the lower bit (0=hash, 1=string) */
114 void* mValOrHash;
115};
116
117
118/**
119 * An integer set that takes up only 4 bytes when there are 0 or 1 integers
120 * in the set. Use for cases where sizes of 0 and 1 are even slightly common.
121 */
122class NS_COM nsCheapInt32Set {
123public:
124 nsCheapInt32Set() : mValOrHash(nsnull)
125 {
126 }
127 ~nsCheapInt32Set();
128
129 /**
130 * Put an int into the set
131 */
132 nsresult Put(PRInt32 aVal);
133
134 /**
135 * Remove a int from the set
136 * @param aVal the int to remove
137 */
138 void Remove(PRInt32 aVal);
139
140 /**
141 * Check if the set contains a particular int
142 * @param aVal the int to check for
143 * @return whether the int is in the set
144 */
145 PRBool Contains(PRInt32 aVal)
146 {
147 nsInt32HashSet* set = GetHash();
148 if (set) {
149 return set->Contains(aVal);
150 }
151 if (IsInt()) {
152 return GetInt() == aVal;
153 }
154 return PR_FALSE;
155 }
156
157private:
158 typedef PRUint64 PtrBits;
159
160 /** Get the hash pointer (or null if we're not a hash) */
161 nsInt32HashSet* GetHash()
162 {
163 return PtrBits(mValOrHash) & 0x1 ? nsnull : (nsInt32HashSet*)mValOrHash;
164 }
165 /** Find out whether it is an integer */
166 PRBool IsInt()
167 {
168 return !!(PtrBits(mValOrHash) & 0x1);
169 }
170 /** Get the single integer */
171 PRInt32 GetInt()
172 {
173 return PtrBits(mValOrHash) >> 1;
174 }
175 /** Set the single integer */
176 void SetInt(PRInt32 aInt)
177 {
178 mValOrHash = (void*)((aInt << 1) | 0x1);
179 }
180 /** Create the hash and initialize */
181 nsresult InitHash(nsInt32HashSet** aSet);
182
183private:
184 /** A hash or int, depending on the lower bit (0=hash, 1=int) */
185 void* mValOrHash;
186};
187
188
189/**
190 * XXX We may want an nsCheapVoidSet and nsCheapCStringSet at some point
191 */
192
193
194#endif
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette