Sat, 03 Jan 2015 20:18:00 +0100
Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.
michael@0 | 1 | /* |
michael@0 | 2 | ********************************************************************** |
michael@0 | 3 | * Copyright (C) 1999-2010, International Business Machines |
michael@0 | 4 | * Corporation and others. All Rights Reserved. |
michael@0 | 5 | ********************************************************************** |
michael@0 | 6 | */ |
michael@0 | 7 | |
michael@0 | 8 | // |
michael@0 | 9 | // UVector64 is a class implementing a vector of 64 bit integers. |
michael@0 | 10 | // It is similar to UVector32, but holds int64_t values rather than int32_t. |
michael@0 | 11 | // Most of the code is unchanged from UVector. |
michael@0 | 12 | // |
michael@0 | 13 | |
michael@0 | 14 | #ifndef UVECTOR64_H |
michael@0 | 15 | #define UVECTOR64_H |
michael@0 | 16 | |
michael@0 | 17 | #include "unicode/utypes.h" |
michael@0 | 18 | #include "unicode/uobject.h" |
michael@0 | 19 | #include "uhash.h" |
michael@0 | 20 | #include "uassert.h" |
michael@0 | 21 | |
michael@0 | 22 | U_NAMESPACE_BEGIN |
michael@0 | 23 | |
michael@0 | 24 | |
michael@0 | 25 | |
michael@0 | 26 | /** |
michael@0 | 27 | * <p>Ultralightweight C++ implementation of an <tt>int64_t</tt> vector |
michael@0 | 28 | * that has a subset of methods from UVector32 |
michael@0 | 29 | * |
michael@0 | 30 | * <p>This is a very simple implementation, written to satisfy an |
michael@0 | 31 | * immediate porting need. As such, it is not completely fleshed out, |
michael@0 | 32 | * and it aims for simplicity and conformity. Nonetheless, it serves |
michael@0 | 33 | * its purpose (porting code from java that uses java.util.Vector) |
michael@0 | 34 | * well, and it could be easily made into a more robust vector class. |
michael@0 | 35 | * |
michael@0 | 36 | * <p><b>Design notes</b> |
michael@0 | 37 | * |
michael@0 | 38 | * <p>There is index bounds checking, but little is done about it. If |
michael@0 | 39 | * indices are out of bounds, either nothing happens, or zero is |
michael@0 | 40 | * returned. We <em>do</em> avoid indexing off into the weeds. |
michael@0 | 41 | * |
michael@0 | 42 | * <p>There is detection of out of memory, but the handling is very |
michael@0 | 43 | * coarse-grained -- similar to UnicodeString's protocol, but even |
michael@0 | 44 | * coarser. The class contains <em>one static flag</em> that is set |
michael@0 | 45 | * when any call to <tt>new</tt> returns zero. This allows the caller |
michael@0 | 46 | * to use several vectors and make just one check at the end to see if |
michael@0 | 47 | * a memory failure occurred. This is more efficient than making a |
michael@0 | 48 | * check after each call on each vector when doing many operations on |
michael@0 | 49 | * multiple vectors. The single static flag works best when memory |
michael@0 | 50 | * failures are infrequent, and when recovery options are limited or |
michael@0 | 51 | * nonexistent. |
michael@0 | 52 | * |
michael@0 | 53 | * <p><b>To do</b> |
michael@0 | 54 | * |
michael@0 | 55 | * <p>Improve the handling of index out of bounds errors. |
michael@0 | 56 | * |
michael@0 | 57 | */ |
michael@0 | 58 | class U_COMMON_API UVector64 : public UObject { |
michael@0 | 59 | private: |
michael@0 | 60 | int32_t count; |
michael@0 | 61 | |
michael@0 | 62 | int32_t capacity; |
michael@0 | 63 | |
michael@0 | 64 | int32_t maxCapacity; // Limit beyond which capacity is not permitted to grow. |
michael@0 | 65 | |
michael@0 | 66 | int64_t* elements; |
michael@0 | 67 | |
michael@0 | 68 | public: |
michael@0 | 69 | UVector64(UErrorCode &status); |
michael@0 | 70 | |
michael@0 | 71 | UVector64(int32_t initialCapacity, UErrorCode &status); |
michael@0 | 72 | |
michael@0 | 73 | virtual ~UVector64(); |
michael@0 | 74 | |
michael@0 | 75 | /** |
michael@0 | 76 | * Assign this object to another (make this a copy of 'other'). |
michael@0 | 77 | * Use the 'assign' function to assign each element. |
michael@0 | 78 | */ |
michael@0 | 79 | void assign(const UVector64& other, UErrorCode &ec); |
michael@0 | 80 | |
michael@0 | 81 | /** |
michael@0 | 82 | * Compare this vector with another. They will be considered |
michael@0 | 83 | * equal if they are of the same size and all elements are equal, |
michael@0 | 84 | * as compared using this object's comparer. |
michael@0 | 85 | */ |
michael@0 | 86 | UBool operator==(const UVector64& other); |
michael@0 | 87 | |
michael@0 | 88 | /** |
michael@0 | 89 | * Equivalent to !operator==() |
michael@0 | 90 | */ |
michael@0 | 91 | inline UBool operator!=(const UVector64& other); |
michael@0 | 92 | |
michael@0 | 93 | //------------------------------------------------------------ |
michael@0 | 94 | // subset of java.util.Vector API |
michael@0 | 95 | //------------------------------------------------------------ |
michael@0 | 96 | |
michael@0 | 97 | void addElement(int64_t elem, UErrorCode &status); |
michael@0 | 98 | |
michael@0 | 99 | void setElementAt(int64_t elem, int32_t index); |
michael@0 | 100 | |
michael@0 | 101 | void insertElementAt(int64_t elem, int32_t index, UErrorCode &status); |
michael@0 | 102 | |
michael@0 | 103 | int64_t elementAti(int32_t index) const; |
michael@0 | 104 | |
michael@0 | 105 | //UBool equals(const UVector64 &other) const; |
michael@0 | 106 | |
michael@0 | 107 | int64_t lastElementi(void) const; |
michael@0 | 108 | |
michael@0 | 109 | //int32_t indexOf(int64_t elem, int32_t startIndex = 0) const; |
michael@0 | 110 | |
michael@0 | 111 | //UBool contains(int64_t elem) const; |
michael@0 | 112 | |
michael@0 | 113 | //UBool containsAll(const UVector64& other) const; |
michael@0 | 114 | |
michael@0 | 115 | //UBool removeAll(const UVector64& other); |
michael@0 | 116 | |
michael@0 | 117 | //UBool retainAll(const UVector64& other); |
michael@0 | 118 | |
michael@0 | 119 | //void removeElementAt(int32_t index); |
michael@0 | 120 | |
michael@0 | 121 | void removeAllElements(); |
michael@0 | 122 | |
michael@0 | 123 | int32_t size(void) const; |
michael@0 | 124 | |
michael@0 | 125 | //UBool isEmpty(void) const; |
michael@0 | 126 | |
michael@0 | 127 | // Inline. Use this one for speedy size check. |
michael@0 | 128 | inline UBool ensureCapacity(int32_t minimumCapacity, UErrorCode &status); |
michael@0 | 129 | |
michael@0 | 130 | // Out-of-line, handles actual growth. Called by ensureCapacity() when necessary. |
michael@0 | 131 | UBool expandCapacity(int32_t minimumCapacity, UErrorCode &status); |
michael@0 | 132 | |
michael@0 | 133 | /** |
michael@0 | 134 | * Change the size of this vector as follows: If newSize is |
michael@0 | 135 | * smaller, then truncate the array, possibly deleting held |
michael@0 | 136 | * elements for i >= newSize. If newSize is larger, grow the |
michael@0 | 137 | * array, filling in new slows with zero. |
michael@0 | 138 | */ |
michael@0 | 139 | void setSize(int32_t newSize); |
michael@0 | 140 | |
michael@0 | 141 | //------------------------------------------------------------ |
michael@0 | 142 | // New API |
michael@0 | 143 | //------------------------------------------------------------ |
michael@0 | 144 | |
michael@0 | 145 | //UBool containsNone(const UVector64& other) const; |
michael@0 | 146 | |
michael@0 | 147 | |
michael@0 | 148 | //void sortedInsert(int64_t elem, UErrorCode& ec); |
michael@0 | 149 | |
michael@0 | 150 | /** |
michael@0 | 151 | * Returns a pointer to the internal array holding the vector. |
michael@0 | 152 | */ |
michael@0 | 153 | int64_t *getBuffer() const; |
michael@0 | 154 | |
michael@0 | 155 | /** |
michael@0 | 156 | * Set the maximum allowed buffer capacity for this vector/stack. |
michael@0 | 157 | * Default with no limit set is unlimited, go until malloc() fails. |
michael@0 | 158 | * A Limit of zero means unlimited capacity. |
michael@0 | 159 | * Units are vector elements (64 bits each), not bytes. |
michael@0 | 160 | */ |
michael@0 | 161 | void setMaxCapacity(int32_t limit); |
michael@0 | 162 | |
michael@0 | 163 | /** |
michael@0 | 164 | * ICU "poor man's RTTI", returns a UClassID for this class. |
michael@0 | 165 | */ |
michael@0 | 166 | static UClassID U_EXPORT2 getStaticClassID(); |
michael@0 | 167 | |
michael@0 | 168 | /** |
michael@0 | 169 | * ICU "poor man's RTTI", returns a UClassID for the actual class. |
michael@0 | 170 | */ |
michael@0 | 171 | virtual UClassID getDynamicClassID() const; |
michael@0 | 172 | |
michael@0 | 173 | private: |
michael@0 | 174 | void _init(int32_t initialCapacity, UErrorCode &status); |
michael@0 | 175 | |
michael@0 | 176 | // Disallow |
michael@0 | 177 | UVector64(const UVector64&); |
michael@0 | 178 | |
michael@0 | 179 | // Disallow |
michael@0 | 180 | UVector64& operator=(const UVector64&); |
michael@0 | 181 | |
michael@0 | 182 | |
michael@0 | 183 | // API Functions for Stack operations. |
michael@0 | 184 | // In the original UVector, these were in a separate derived class, UStack. |
michael@0 | 185 | // Here in UVector64, they are all together. |
michael@0 | 186 | public: |
michael@0 | 187 | //UBool empty(void) const; // TODO: redundant, same as empty(). Remove it? |
michael@0 | 188 | |
michael@0 | 189 | //int64_t peeki(void) const; |
michael@0 | 190 | |
michael@0 | 191 | int64_t popi(void); |
michael@0 | 192 | |
michael@0 | 193 | int64_t push(int64_t i, UErrorCode &status); |
michael@0 | 194 | |
michael@0 | 195 | int64_t *reserveBlock(int32_t size, UErrorCode &status); |
michael@0 | 196 | int64_t *popFrame(int32_t size); |
michael@0 | 197 | }; |
michael@0 | 198 | |
michael@0 | 199 | |
michael@0 | 200 | // UVector64 inlines |
michael@0 | 201 | |
michael@0 | 202 | inline UBool UVector64::ensureCapacity(int32_t minimumCapacity, UErrorCode &status) { |
michael@0 | 203 | if ((minimumCapacity >= 0) && (capacity >= minimumCapacity)) { |
michael@0 | 204 | return TRUE; |
michael@0 | 205 | } else { |
michael@0 | 206 | return expandCapacity(minimumCapacity, status); |
michael@0 | 207 | } |
michael@0 | 208 | } |
michael@0 | 209 | |
michael@0 | 210 | inline int64_t UVector64::elementAti(int32_t index) const { |
michael@0 | 211 | return (0 <= index && index < count) ? elements[index] : 0; |
michael@0 | 212 | } |
michael@0 | 213 | |
michael@0 | 214 | |
michael@0 | 215 | inline void UVector64::addElement(int64_t elem, UErrorCode &status) { |
michael@0 | 216 | if (ensureCapacity(count + 1, status)) { |
michael@0 | 217 | elements[count] = elem; |
michael@0 | 218 | count++; |
michael@0 | 219 | } |
michael@0 | 220 | } |
michael@0 | 221 | |
michael@0 | 222 | inline int64_t *UVector64::reserveBlock(int32_t size, UErrorCode &status) { |
michael@0 | 223 | if (ensureCapacity(count+size, status) == FALSE) { |
michael@0 | 224 | return NULL; |
michael@0 | 225 | } |
michael@0 | 226 | int64_t *rp = elements+count; |
michael@0 | 227 | count += size; |
michael@0 | 228 | return rp; |
michael@0 | 229 | } |
michael@0 | 230 | |
michael@0 | 231 | inline int64_t *UVector64::popFrame(int32_t size) { |
michael@0 | 232 | U_ASSERT(count >= size); |
michael@0 | 233 | count -= size; |
michael@0 | 234 | if (count < 0) { |
michael@0 | 235 | count = 0; |
michael@0 | 236 | } |
michael@0 | 237 | return elements+count-size; |
michael@0 | 238 | } |
michael@0 | 239 | |
michael@0 | 240 | |
michael@0 | 241 | |
michael@0 | 242 | inline int32_t UVector64::size(void) const { |
michael@0 | 243 | return count; |
michael@0 | 244 | } |
michael@0 | 245 | |
michael@0 | 246 | inline int64_t UVector64::lastElementi(void) const { |
michael@0 | 247 | return elementAti(count-1); |
michael@0 | 248 | } |
michael@0 | 249 | |
michael@0 | 250 | inline UBool UVector64::operator!=(const UVector64& other) { |
michael@0 | 251 | return !operator==(other); |
michael@0 | 252 | } |
michael@0 | 253 | |
michael@0 | 254 | inline int64_t *UVector64::getBuffer() const { |
michael@0 | 255 | return elements; |
michael@0 | 256 | } |
michael@0 | 257 | |
michael@0 | 258 | |
michael@0 | 259 | // UStack inlines |
michael@0 | 260 | |
michael@0 | 261 | inline int64_t UVector64::push(int64_t i, UErrorCode &status) { |
michael@0 | 262 | addElement(i, status); |
michael@0 | 263 | return i; |
michael@0 | 264 | } |
michael@0 | 265 | |
michael@0 | 266 | inline int64_t UVector64::popi(void) { |
michael@0 | 267 | int64_t result = 0; |
michael@0 | 268 | if (count > 0) { |
michael@0 | 269 | count--; |
michael@0 | 270 | result = elements[count]; |
michael@0 | 271 | } |
michael@0 | 272 | return result; |
michael@0 | 273 | } |
michael@0 | 274 | |
michael@0 | 275 | U_NAMESPACE_END |
michael@0 | 276 | |
michael@0 | 277 | #endif |