| OLD | NEW |
| 1 /* | 1 /* |
| 2 ********************************************************************** | 2 ********************************************************************** |
| 3 * Copyright (C) 2013, International Business Machines | 3 * Copyright (C) 2014, International Business Machines |
| 4 * Corporation and others. All Rights Reserved. | 4 * Corporation and others. All Rights Reserved. |
| 5 ********************************************************************** | 5 ********************************************************************** |
| 6 * | 6 * |
| 7 * scriptset.cpp | 7 * scriptset.cpp |
| 8 * | 8 * |
| 9 * created on: 2013 Jan 7 | 9 * created on: 2013 Jan 7 |
| 10 * created by: Andy Heninger | 10 * created by: Andy Heninger |
| 11 */ | 11 */ |
| 12 | 12 |
| 13 #include "unicode/utypes.h" | 13 #include "unicode/utypes.h" |
| 14 | 14 |
| 15 #include "unicode/uchar.h" | 15 #include "unicode/uchar.h" |
| 16 #include "unicode/unistr.h" | 16 #include "unicode/unistr.h" |
| 17 | 17 |
| 18 #include "scriptset.h" | 18 #include "scriptset.h" |
| 19 #include "uassert.h" | 19 #include "uassert.h" |
| 20 #include "cmemory.h" |
| 20 | 21 |
| 21 U_NAMESPACE_BEGIN | 22 U_NAMESPACE_BEGIN |
| 22 | 23 |
| 23 #define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0])) | |
| 24 | |
| 25 //---------------------------------------------------------------------------- | 24 //---------------------------------------------------------------------------- |
| 26 // | 25 // |
| 27 // ScriptSet implementation | 26 // ScriptSet implementation |
| 28 // | 27 // |
| 29 //---------------------------------------------------------------------------- | 28 //---------------------------------------------------------------------------- |
| 30 ScriptSet::ScriptSet() { | 29 ScriptSet::ScriptSet() { |
| 31 for (uint32_t i=0; i<LENGTHOF(bits); i++) { | 30 for (uint32_t i=0; i<UPRV_LENGTHOF(bits); i++) { |
| 32 bits[i] = 0; | 31 bits[i] = 0; |
| 33 } | 32 } |
| 34 } | 33 } |
| 35 | 34 |
| 36 ScriptSet::~ScriptSet() { | 35 ScriptSet::~ScriptSet() { |
| 37 } | 36 } |
| 38 | 37 |
| 39 ScriptSet::ScriptSet(const ScriptSet &other) { | 38 ScriptSet::ScriptSet(const ScriptSet &other) { |
| 40 *this = other; | 39 *this = other; |
| 41 } | 40 } |
| 42 | 41 |
| 43 | 42 |
| 44 ScriptSet & ScriptSet::operator =(const ScriptSet &other) { | 43 ScriptSet & ScriptSet::operator =(const ScriptSet &other) { |
| 45 for (uint32_t i=0; i<LENGTHOF(bits); i++) { | 44 for (uint32_t i=0; i<UPRV_LENGTHOF(bits); i++) { |
| 46 bits[i] = other.bits[i]; | 45 bits[i] = other.bits[i]; |
| 47 } | 46 } |
| 48 return *this; | 47 return *this; |
| 49 } | 48 } |
| 50 | 49 |
| 51 | 50 |
| 52 UBool ScriptSet::operator == (const ScriptSet &other) const { | 51 UBool ScriptSet::operator == (const ScriptSet &other) const { |
| 53 for (uint32_t i=0; i<LENGTHOF(bits); i++) { | 52 for (uint32_t i=0; i<UPRV_LENGTHOF(bits); i++) { |
| 54 if (bits[i] != other.bits[i]) { | 53 if (bits[i] != other.bits[i]) { |
| 55 return FALSE; | 54 return FALSE; |
| 56 } | 55 } |
| 57 } | 56 } |
| 58 return TRUE; | 57 return TRUE; |
| 59 } | 58 } |
| 60 | 59 |
| 61 UBool ScriptSet::test(UScriptCode script, UErrorCode &status) const { | 60 UBool ScriptSet::test(UScriptCode script, UErrorCode &status) const { |
| 62 if (U_FAILURE(status)) { | 61 if (U_FAILURE(status)) { |
| 63 return FALSE; | 62 return FALSE; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 } | 95 } |
| 97 uint32_t index = script / 32; | 96 uint32_t index = script / 32; |
| 98 uint32_t bit = 1 << (script & 31); | 97 uint32_t bit = 1 << (script & 31); |
| 99 bits[index] &= ~bit; | 98 bits[index] &= ~bit; |
| 100 return *this; | 99 return *this; |
| 101 } | 100 } |
| 102 | 101 |
| 103 | 102 |
| 104 | 103 |
| 105 ScriptSet &ScriptSet::Union(const ScriptSet &other) { | 104 ScriptSet &ScriptSet::Union(const ScriptSet &other) { |
| 106 for (uint32_t i=0; i<LENGTHOF(bits); i++) { | 105 for (uint32_t i=0; i<UPRV_LENGTHOF(bits); i++) { |
| 107 bits[i] |= other.bits[i]; | 106 bits[i] |= other.bits[i]; |
| 108 } | 107 } |
| 109 return *this; | 108 return *this; |
| 110 } | 109 } |
| 111 | 110 |
| 112 ScriptSet &ScriptSet::intersect(const ScriptSet &other) { | 111 ScriptSet &ScriptSet::intersect(const ScriptSet &other) { |
| 113 for (uint32_t i=0; i<LENGTHOF(bits); i++) { | 112 for (uint32_t i=0; i<UPRV_LENGTHOF(bits); i++) { |
| 114 bits[i] &= other.bits[i]; | 113 bits[i] &= other.bits[i]; |
| 115 } | 114 } |
| 116 return *this; | 115 return *this; |
| 117 } | 116 } |
| 118 | 117 |
| 119 ScriptSet &ScriptSet::intersect(UScriptCode script, UErrorCode &status) { | 118 ScriptSet &ScriptSet::intersect(UScriptCode script, UErrorCode &status) { |
| 120 ScriptSet t; | 119 ScriptSet t; |
| 121 t.set(script, status); | 120 t.set(script, status); |
| 122 if (U_SUCCESS(status)) { | 121 if (U_SUCCESS(status)) { |
| 123 this->intersect(t); | 122 this->intersect(t); |
| 124 } | 123 } |
| 125 return *this; | 124 return *this; |
| 126 } | 125 } |
| 127 | 126 |
| 128 UBool ScriptSet::intersects(const ScriptSet &other) const { | 127 UBool ScriptSet::intersects(const ScriptSet &other) const { |
| 129 for (uint32_t i=0; i<LENGTHOF(bits); i++) { | 128 for (uint32_t i=0; i<UPRV_LENGTHOF(bits); i++) { |
| 130 if ((bits[i] & other.bits[i]) != 0) { | 129 if ((bits[i] & other.bits[i]) != 0) { |
| 131 return true; | 130 return true; |
| 132 } | 131 } |
| 133 } | 132 } |
| 134 return false; | 133 return false; |
| 135 } | 134 } |
| 136 | 135 |
| 137 UBool ScriptSet::contains(const ScriptSet &other) const { | 136 UBool ScriptSet::contains(const ScriptSet &other) const { |
| 138 ScriptSet t(*this); | 137 ScriptSet t(*this); |
| 139 t.intersect(other); | 138 t.intersect(other); |
| 140 return (t == other); | 139 return (t == other); |
| 141 } | 140 } |
| 142 | 141 |
| 143 | 142 |
| 144 ScriptSet &ScriptSet::setAll() { | 143 ScriptSet &ScriptSet::setAll() { |
| 145 for (uint32_t i=0; i<LENGTHOF(bits); i++) { | 144 for (uint32_t i=0; i<UPRV_LENGTHOF(bits); i++) { |
| 146 bits[i] = 0xffffffffu; | 145 bits[i] = 0xffffffffu; |
| 147 } | 146 } |
| 148 return *this; | 147 return *this; |
| 149 } | 148 } |
| 150 | 149 |
| 151 | 150 |
| 152 ScriptSet &ScriptSet::resetAll() { | 151 ScriptSet &ScriptSet::resetAll() { |
| 153 for (uint32_t i=0; i<LENGTHOF(bits); i++) { | 152 for (uint32_t i=0; i<UPRV_LENGTHOF(bits); i++) { |
| 154 bits[i] = 0; | 153 bits[i] = 0; |
| 155 } | 154 } |
| 156 return *this; | 155 return *this; |
| 157 } | 156 } |
| 158 | 157 |
| 159 int32_t ScriptSet::countMembers() const { | 158 int32_t ScriptSet::countMembers() const { |
| 160 // This bit counter is good for sparse numbers of '1's, which is | 159 // This bit counter is good for sparse numbers of '1's, which is |
| 161 // very much the case that we will usually have. | 160 // very much the case that we will usually have. |
| 162 int32_t count = 0; | 161 int32_t count = 0; |
| 163 for (uint32_t i=0; i<LENGTHOF(bits); i++) { | 162 for (uint32_t i=0; i<UPRV_LENGTHOF(bits); i++) { |
| 164 uint32_t x = bits[i]; | 163 uint32_t x = bits[i]; |
| 165 while (x > 0) { | 164 while (x > 0) { |
| 166 count++; | 165 count++; |
| 167 x &= (x - 1); // and off the least significant one bit. | 166 x &= (x - 1); // and off the least significant one bit. |
| 168 } | 167 } |
| 169 } | 168 } |
| 170 return count; | 169 return count; |
| 171 } | 170 } |
| 172 | 171 |
| 173 int32_t ScriptSet::hashCode() const { | 172 int32_t ScriptSet::hashCode() const { |
| 174 int32_t hash = 0; | 173 int32_t hash = 0; |
| 175 for (int32_t i=0; i<LENGTHOF(bits); i++) { | 174 for (int32_t i=0; i<UPRV_LENGTHOF(bits); i++) { |
| 176 hash ^= bits[i]; | 175 hash ^= bits[i]; |
| 177 } | 176 } |
| 178 return hash; | 177 return hash; |
| 179 } | 178 } |
| 180 | 179 |
| 181 int32_t ScriptSet::nextSetBit(int32_t fromIndex) const { | 180 int32_t ScriptSet::nextSetBit(int32_t fromIndex) const { |
| 182 // TODO: Wants a better implementation. | 181 // TODO: Wants a better implementation. |
| 183 if (fromIndex < 0) { | 182 if (fromIndex < 0) { |
| 184 return -1; | 183 return -1; |
| 185 } | 184 } |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 267 uhash_hashScriptSet(const UElement key) { | 266 uhash_hashScriptSet(const UElement key) { |
| 268 icu::ScriptSet *s = static_cast<icu::ScriptSet *>(key.pointer); | 267 icu::ScriptSet *s = static_cast<icu::ScriptSet *>(key.pointer); |
| 269 return s->hashCode(); | 268 return s->hashCode(); |
| 270 } | 269 } |
| 271 | 270 |
| 272 U_CAPI void U_EXPORT2 | 271 U_CAPI void U_EXPORT2 |
| 273 uhash_deleteScriptSet(void *obj) { | 272 uhash_deleteScriptSet(void *obj) { |
| 274 icu::ScriptSet *s = static_cast<icu::ScriptSet *>(obj); | 273 icu::ScriptSet *s = static_cast<icu::ScriptSet *>(obj); |
| 275 delete s; | 274 delete s; |
| 276 } | 275 } |
| OLD | NEW |