Back to index

lightning-sunbird  0.9+nobinonly
nsCheapSets.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Mozilla Communicator client code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "nsCheapSets.h"
00039 
00040 nsCheapStringSet::~nsCheapStringSet()
00041 {
00042   nsStringHashSet* set = GetHash();
00043   if (set) {
00044     delete set;
00045   } else {
00046     delete GetStr();
00047   }
00048 }
00049 
00053 nsresult
00054 nsCheapStringSet::Put(const nsAString& aVal)
00055 {
00056   // Add the value to the hash if it is there
00057   nsStringHashSet* set = GetHash();
00058   if (set) {
00059     return set->Put(aVal);
00060   }
00061 
00062   // If a string is already there, create a hashtable and both of these to it
00063   if (GetStr()) {
00064     nsAString* oldStr = GetStr();
00065     nsresult rv = InitHash(&set);
00066     NS_ENSURE_SUCCESS(rv, rv);
00067 
00068     rv = set->Put(*oldStr);
00069     delete oldStr;
00070     NS_ENSURE_SUCCESS(rv, rv);
00071 
00072     return set->Put(aVal);
00073   }
00074 
00075   // Nothing exists in the hash right now, so just set the single string
00076   return SetStr(aVal);
00077 }
00078 
00079 void
00080 nsCheapStringSet::Remove(const nsAString& aVal)
00081 {
00082   // Remove from the hash if the hash is there
00083   nsStringHashSet* set = GetHash();
00084   if (set) {
00085     set->Remove(aVal);
00086     return;
00087   }
00088 
00089   // Remove the string if there is just a string
00090   nsAString* str = GetStr();
00091   if (str && str->Equals(aVal)) {
00092     delete str;
00093     mValOrHash = nsnull;
00094   }
00095 }
00096 
00097 nsresult
00098 nsCheapStringSet::InitHash(nsStringHashSet** aSet)
00099 {
00100   nsStringHashSet* newSet = new nsStringHashSet();
00101   if (!newSet) {
00102     return NS_ERROR_OUT_OF_MEMORY;
00103   }
00104 
00105   nsresult rv = newSet->Init(10);
00106   NS_ENSURE_SUCCESS(rv, rv);
00107 
00108   mValOrHash = newSet;
00109   *aSet = newSet;
00110   return NS_OK;
00111 }
00112 
00113 
00114 nsCheapInt32Set::~nsCheapInt32Set()
00115 {
00116   delete GetHash();
00117 }
00118 
00119 nsresult
00120 nsCheapInt32Set::Put(PRInt32 aVal)
00121 {
00122   // Add the value to the hash or set the pointer as an int
00123   nsInt32HashSet* set = GetHash();
00124   if (set) {
00125     return set->Put(aVal);
00126   }
00127 
00128   // Create the hash and add the value to it if there is an int already
00129   if (IsInt()) {
00130     PRInt32 oldInt = GetInt();
00131 
00132     nsresult rv = InitHash(&set);
00133     NS_ENSURE_SUCCESS(rv, rv);
00134 
00135     rv = set->Put(oldInt);
00136     NS_ENSURE_SUCCESS(rv, rv);
00137 
00138     return set->Put(aVal);
00139   }
00140 
00141   // Create the hash anyway if the int is negative (negative numbers cannot
00142   // fit into our PtrBits abstraction)
00143   if (aVal < 0) {
00144     nsresult rv = InitHash(&set);
00145     NS_ENSURE_SUCCESS(rv, rv);
00146 
00147     return set->Put(aVal);
00148   }
00149 
00150   // Finally, just set the int if we can't do anything with hashes
00151   SetInt(aVal);
00152   return NS_OK;
00153 }
00154  
00155 void
00156 nsCheapInt32Set::Remove(PRInt32 aVal)
00157 {
00158   nsInt32HashSet* set = GetHash();
00159   if (set) {
00160     set->Remove(aVal);
00161   } else if (IsInt() && GetInt() == aVal) {
00162     mValOrHash = nsnull;
00163   }
00164 }
00165 
00166 nsresult
00167 nsCheapInt32Set::InitHash(nsInt32HashSet** aSet)
00168 {
00169   nsInt32HashSet* newSet = new nsInt32HashSet();
00170   if (!newSet) {
00171     return NS_ERROR_OUT_OF_MEMORY;
00172   }
00173 
00174   nsresult rv = newSet->Init(10);
00175   NS_ENSURE_SUCCESS(rv, rv);
00176 
00177   mValOrHash = newSet;
00178   *aSet = newSet;
00179   return NS_OK;
00180 }