Back to index

lightning-sunbird  0.9+nobinonly
nsValueArray.h
Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is nsValueArray.h/nsValueArray.cpp code, released
00017  * Dec 28, 2001.
00018  *
00019  * The Initial Developer of the Original Code is
00020  * Netscape Communications Corporation.
00021  * Portions created by the Initial Developer are Copyright (C) 2001
00022  * the Initial Developer. All Rights Reserved.
00023  *
00024  * Contributor(s):
00025  *   Garrett Arch Blythe, 20-December-2001
00026  *
00027  * Alternatively, the contents of this file may be used under the terms of
00028  * either of the GNU General Public License Version 2 or later (the "GPL"),
00029  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00030  * in which case the provisions of the GPL or the LGPL are applicable instead
00031  * of those above. If you wish to allow use of your version of this file only
00032  * under the terms of either the GPL or the LGPL, and not to allow others to
00033  * use your version of this file under the terms of the MPL, indicate your
00034  * decision by deleting the provisions above and replace them with the notice
00035  * and other provisions required by the GPL or the LGPL. If you do not delete
00036  * the provisions above, a recipient may use your version of this file under
00037  * the terms of any one of the MPL, the GPL or the LGPL.
00038  *
00039  * ***** END LICENSE BLOCK ***** */
00040 #ifndef nsValueArray_h___
00041 #define nsValueArray_h___
00042 
00043 //
00044 // nsValueArray.h
00045 //
00046 // Implement an array class to store unsigned integer values.
00047 // The maximum value must be known up front.  Once known, the
00048 //  smallest memory representation will be attempted; i.e. if the
00049 //  maximum value was 1275, then 2 bytes (uint16) would represent each value
00050 //  in the array instead of 4 bytes (uint32).
00051 //
00052 #include "nscore.h"
00053 
00054 typedef PRUint32 nsValueArrayCount;
00055 typedef PRUint32 nsValueArrayIndex;
00056 typedef PRUint32 nsValueArrayValue;
00057 #define NSVALUEARRAY_INVALID ((nsValueArrayValue)-1)
00058 
00059 class NS_COM nsValueArray {
00060   public:
00061     nsValueArray(nsValueArrayValue aMaxValue,
00062                  nsValueArrayCount aInitialCapacity = 0);
00063     ~nsValueArray();
00064 
00065     //
00066     // Assignment.
00067     //
00068   public:
00069     nsValueArray& operator=(const nsValueArray& other);
00070 
00071     //
00072     // Array size information.
00073     // Ability to add more values without growing is Capacity - Count.
00074     //
00075   public:
00076     inline nsValueArrayCount Count() const {
00077         return mCount;
00078     }
00079 
00080     inline nsValueArrayCount Capacity() const {
00081         return mCapacity;
00082     }
00083 
00084     void Compact();
00085 
00086     // Removes all elements from this array
00087     inline void Clear() {
00088         mCount = 0;
00089     }
00090 
00091     //
00092     // Array access.
00093     //
00094   public:
00095     nsValueArrayValue ValueAt(nsValueArrayIndex aIndex) const;
00096 
00097     inline nsValueArrayValue operator[](nsValueArrayIndex aIndex) const {
00098         return ValueAt(aIndex);
00099     }
00100 
00101     nsValueArrayIndex IndexOf(nsValueArrayValue aPossibleValue) const;
00102 
00103     inline PRBool AppendValue(nsValueArrayValue aValue) {
00104         return InsertValueAt(aValue, Count());
00105     }
00106 
00107     inline PRBool RemoveValue(nsValueArrayValue aValue) {
00108         return  RemoveValueAt(IndexOf(aValue));
00109     }
00110 
00111     PRBool InsertValueAt(nsValueArrayValue aValue, nsValueArrayIndex aIndex);
00112 
00113     PRBool RemoveValueAt(nsValueArrayIndex aIndex);
00114 
00115     //
00116     // Data members.
00117     //
00118   private:
00119     nsValueArrayCount mCount;
00120     nsValueArrayCount mCapacity;
00121     PRUint8* mValueArray;
00122     PRUint8 mBytesPerValue;
00123 };
00124 
00125 #endif /* nsValueArray_h___ */