| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). | 
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com) | 
|---|
| 5 | ** | 
|---|
| 6 | ** This file is part of the QtScript module of the Qt Toolkit. | 
|---|
| 7 | ** | 
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ | 
|---|
| 9 | ** Commercial Usage | 
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in | 
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the | 
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in | 
|---|
| 13 | ** a written agreement between you and Nokia. | 
|---|
| 14 | ** | 
|---|
| 15 | ** GNU Lesser General Public License Usage | 
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software | 
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the | 
|---|
| 19 | ** packaging of this file.  Please review the following information to | 
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements | 
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | 
|---|
| 22 | ** | 
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain | 
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL | 
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this | 
|---|
| 26 | ** package. | 
|---|
| 27 | ** | 
|---|
| 28 | ** GNU General Public License Usage | 
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU | 
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software | 
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the | 
|---|
| 32 | ** packaging of this file.  Please review the following information to | 
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be | 
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html. | 
|---|
| 35 | ** | 
|---|
| 36 | ** If you are unsure which license is appropriate for your use, please | 
|---|
| 37 | ** contact the sales department at qt-sales@nokia.com. | 
|---|
| 38 | ** $QT_END_LICENSE$ | 
|---|
| 39 | ** | 
|---|
| 40 | ****************************************************************************/ | 
|---|
| 41 |  | 
|---|
| 42 | #ifndef QSCRIPTARRAY_P_H | 
|---|
| 43 | #define QSCRIPTARRAY_P_H | 
|---|
| 44 |  | 
|---|
| 45 | // | 
|---|
| 46 | //  W A R N I N G | 
|---|
| 47 | //  ------------- | 
|---|
| 48 | // | 
|---|
| 49 | // This file is not part of the Qt API.  It exists purely as an | 
|---|
| 50 | // implementation detail.  This header file may change from version to | 
|---|
| 51 | // version without notice, or even be removed. | 
|---|
| 52 | // | 
|---|
| 53 | // We mean it. | 
|---|
| 54 | // | 
|---|
| 55 |  | 
|---|
| 56 | #include <QtCore/QMap> | 
|---|
| 57 |  | 
|---|
| 58 | #ifndef QT_NO_SCRIPT | 
|---|
| 59 |  | 
|---|
| 60 | #include <QtCore/QVector> | 
|---|
| 61 |  | 
|---|
| 62 | #include "qscriptvalueimplfwd_p.h" | 
|---|
| 63 | #include "qscriptenginefwd_p.h" | 
|---|
| 64 |  | 
|---|
| 65 | QT_BEGIN_NAMESPACE | 
|---|
| 66 |  | 
|---|
| 67 | namespace QScript { | 
|---|
| 68 |  | 
|---|
| 69 | class Array | 
|---|
| 70 | { | 
|---|
| 71 | public: | 
|---|
| 72 | inline Array(QScriptEnginePrivate *engine); | 
|---|
| 73 | inline Array(const Array &other); | 
|---|
| 74 | inline ~Array(); | 
|---|
| 75 |  | 
|---|
| 76 | inline Array &operator = (const Array &other); | 
|---|
| 77 |  | 
|---|
| 78 | inline bool isEmpty() const; | 
|---|
| 79 | inline uint size() const; | 
|---|
| 80 | inline uint count() const; | 
|---|
| 81 | inline QScriptValueImpl at(uint index) const; | 
|---|
| 82 | inline void assign(uint index, const QScriptValueImpl &v); | 
|---|
| 83 | inline void clear(); | 
|---|
| 84 | inline void mark(int generation); | 
|---|
| 85 | inline void resize(uint size); | 
|---|
| 86 | inline void concat(const Array &other); | 
|---|
| 87 | inline QScriptValueImpl pop(); | 
|---|
| 88 | inline void sort(const QScriptValueImpl &comparefn); | 
|---|
| 89 | inline void splice(qsreal start, qsreal deleteCount, | 
|---|
| 90 | const QVector<QScriptValueImpl> &items, | 
|---|
| 91 | Array &other); | 
|---|
| 92 | inline QList<uint> keys() const; | 
|---|
| 93 |  | 
|---|
| 94 | private: | 
|---|
| 95 | enum Mode { | 
|---|
| 96 | VectorMode, | 
|---|
| 97 | MapMode | 
|---|
| 98 | }; | 
|---|
| 99 |  | 
|---|
| 100 | QScriptEnginePrivate *m_engine; | 
|---|
| 101 | Mode m_mode; | 
|---|
| 102 | int m_instances; | 
|---|
| 103 |  | 
|---|
| 104 | union { | 
|---|
| 105 | QMap<uint, QScriptValueImpl> *to_map; | 
|---|
| 106 | QVector<QScriptValueImpl> *to_vector; | 
|---|
| 107 | }; | 
|---|
| 108 | }; | 
|---|
| 109 |  | 
|---|
| 110 | class ArrayElementLessThan | 
|---|
| 111 | { | 
|---|
| 112 | public: | 
|---|
| 113 | inline ArrayElementLessThan(const QScriptValueImpl &comparefn) | 
|---|
| 114 | : m_comparefn(comparefn) {} | 
|---|
| 115 |  | 
|---|
| 116 | inline bool operator()(const QScriptValueImpl &v1, const QScriptValueImpl &v2) const | 
|---|
| 117 | { | 
|---|
| 118 | if (!v1.isValid() || v1.isUndefined()) | 
|---|
| 119 | return false; | 
|---|
| 120 | if (!v2.isValid() || v2.isUndefined()) | 
|---|
| 121 | return true; | 
|---|
| 122 | if (!m_comparefn.isUndefined()) { | 
|---|
| 123 | ArrayElementLessThan *that = const_cast<ArrayElementLessThan*>(this); | 
|---|
| 124 | QScriptValueImpl result = that->m_comparefn.call(QScriptValueImpl(), | 
|---|
| 125 | QScriptValueImplList() << v1 << v2); | 
|---|
| 126 | return result.toNumber() <= 0; | 
|---|
| 127 | } | 
|---|
| 128 | return v1.toString() < v2.toString(); | 
|---|
| 129 | } | 
|---|
| 130 |  | 
|---|
| 131 | private: | 
|---|
| 132 | QScriptValueImpl m_comparefn; | 
|---|
| 133 | }; | 
|---|
| 134 |  | 
|---|
| 135 | } // namespace QScript | 
|---|
| 136 |  | 
|---|
| 137 | inline QScript::Array::Array(QScriptEnginePrivate *engine): | 
|---|
| 138 | m_engine(engine), | 
|---|
| 139 | m_mode(VectorMode), | 
|---|
| 140 | m_instances(0) | 
|---|
| 141 | { | 
|---|
| 142 | to_vector = new QVector<QScriptValueImpl>(); | 
|---|
| 143 | } | 
|---|
| 144 |  | 
|---|
| 145 | inline QScript::Array::Array(const Array &other): | 
|---|
| 146 | m_engine(other.m_engine), | 
|---|
| 147 | m_mode(other.m_mode), | 
|---|
| 148 | m_instances(other.m_instances) | 
|---|
| 149 | { | 
|---|
| 150 | if (m_mode == VectorMode) | 
|---|
| 151 | to_vector = new QVector<QScriptValueImpl> (*other.to_vector); | 
|---|
| 152 | else | 
|---|
| 153 | to_map = new QMap<uint, QScriptValueImpl> (*other.to_map); | 
|---|
| 154 | } | 
|---|
| 155 |  | 
|---|
| 156 | inline QScript::Array::~Array() | 
|---|
| 157 | { | 
|---|
| 158 | if (m_mode == VectorMode) | 
|---|
| 159 | delete to_vector; | 
|---|
| 160 | else | 
|---|
| 161 | delete to_map; | 
|---|
| 162 | } | 
|---|
| 163 |  | 
|---|
| 164 | inline QScript::Array &QScript::Array::operator = (const Array &other) | 
|---|
| 165 | { | 
|---|
| 166 | m_engine = other.m_engine; | 
|---|
| 167 | m_instances = other.m_instances; | 
|---|
| 168 | if (m_mode != other.m_mode) { | 
|---|
| 169 | if (m_mode == VectorMode) | 
|---|
| 170 | delete to_vector; | 
|---|
| 171 | else | 
|---|
| 172 | delete to_map; | 
|---|
| 173 | m_mode = other.m_mode; | 
|---|
| 174 |  | 
|---|
| 175 | if (m_mode == VectorMode) | 
|---|
| 176 | to_vector = new QVector<QScriptValueImpl> (*other.to_vector); | 
|---|
| 177 | else | 
|---|
| 178 | to_map = new QMap<uint, QScriptValueImpl> (*other.to_map); | 
|---|
| 179 | } | 
|---|
| 180 |  | 
|---|
| 181 | if (m_mode == VectorMode) | 
|---|
| 182 | *to_vector = *other.to_vector; | 
|---|
| 183 | else | 
|---|
| 184 | *to_map = *other.to_map; | 
|---|
| 185 |  | 
|---|
| 186 | return *this; | 
|---|
| 187 | } | 
|---|
| 188 |  | 
|---|
| 189 | inline bool QScript::Array::isEmpty() const | 
|---|
| 190 | { | 
|---|
| 191 | if (m_mode == VectorMode) | 
|---|
| 192 | return to_vector->isEmpty(); | 
|---|
| 193 |  | 
|---|
| 194 | return to_map->isEmpty(); | 
|---|
| 195 | } | 
|---|
| 196 |  | 
|---|
| 197 | inline uint QScript::Array::size() const | 
|---|
| 198 | { | 
|---|
| 199 | if (m_mode == VectorMode) | 
|---|
| 200 | return to_vector->size(); | 
|---|
| 201 |  | 
|---|
| 202 | if (to_map->isEmpty()) | 
|---|
| 203 | return 0; | 
|---|
| 204 |  | 
|---|
| 205 | return (--to_map->constEnd()).key(); | 
|---|
| 206 | } | 
|---|
| 207 |  | 
|---|
| 208 | inline uint QScript::Array::count() const | 
|---|
| 209 | { | 
|---|
| 210 | return size(); | 
|---|
| 211 | } | 
|---|
| 212 |  | 
|---|
| 213 | inline QScriptValueImpl QScript::Array::at(uint index) const | 
|---|
| 214 | { | 
|---|
| 215 | if (m_mode == VectorMode) { | 
|---|
| 216 | if (index < uint(to_vector->size())) | 
|---|
| 217 | return to_vector->at(index); | 
|---|
| 218 | return QScriptValueImpl(); | 
|---|
| 219 | } else { | 
|---|
| 220 | return to_map->value(index, QScriptValueImpl()); | 
|---|
| 221 | } | 
|---|
| 222 | } | 
|---|
| 223 |  | 
|---|
| 224 | inline void QScript::Array::assign(uint index, const QScriptValueImpl &v) | 
|---|
| 225 | { | 
|---|
| 226 | if (index >= size()) { | 
|---|
| 227 | resize(index + 1); | 
|---|
| 228 | if (v.isValid() && m_engine) | 
|---|
| 229 | m_engine->adjustBytesAllocated(sizeof(QScriptValueImpl) * (size() - index)); | 
|---|
| 230 | } | 
|---|
| 231 |  | 
|---|
| 232 | const QScriptValueImpl &oldv = at(index); | 
|---|
| 233 | if (oldv.isValid() && (oldv.isObject() || oldv.isString())) | 
|---|
| 234 | --m_instances; | 
|---|
| 235 |  | 
|---|
| 236 | if (v.isValid() && (v.isObject() || v.isString())) | 
|---|
| 237 | ++m_instances; | 
|---|
| 238 |  | 
|---|
| 239 | if (m_mode == VectorMode) { | 
|---|
| 240 | to_vector->replace(index, v); | 
|---|
| 241 | } else { | 
|---|
| 242 | if (!v.isValid()) | 
|---|
| 243 | to_map->remove(index); | 
|---|
| 244 | else | 
|---|
| 245 | to_map->insert(index, v); | 
|---|
| 246 | } | 
|---|
| 247 | } | 
|---|
| 248 |  | 
|---|
| 249 | inline void QScript::Array::clear() | 
|---|
| 250 | { | 
|---|
| 251 | m_instances = 0; | 
|---|
| 252 |  | 
|---|
| 253 | if (m_mode == VectorMode) | 
|---|
| 254 | to_vector->clear(); | 
|---|
| 255 |  | 
|---|
| 256 | else | 
|---|
| 257 | to_map->clear(); | 
|---|
| 258 | } | 
|---|
| 259 |  | 
|---|
| 260 | inline void QScript::Array::mark(int generation) | 
|---|
| 261 | { | 
|---|
| 262 | if (! m_instances) | 
|---|
| 263 | return; | 
|---|
| 264 |  | 
|---|
| 265 | if (m_mode == VectorMode) { | 
|---|
| 266 | for (int i = 0; i < to_vector->size(); ++i) | 
|---|
| 267 | to_vector->at(i).mark(generation); | 
|---|
| 268 | } else { | 
|---|
| 269 | QMap<uint, QScriptValueImpl>::const_iterator it = to_map->constBegin(); | 
|---|
| 270 | for (; it != to_map->constEnd(); ++it) | 
|---|
| 271 | it.value().mark(generation); | 
|---|
| 272 | } | 
|---|
| 273 | } | 
|---|
| 274 |  | 
|---|
| 275 | inline void QScript::Array::resize(uint s) | 
|---|
| 276 | { | 
|---|
| 277 | const uint oldSize = size(); | 
|---|
| 278 | if (oldSize == s) | 
|---|
| 279 | return; | 
|---|
| 280 |  | 
|---|
| 281 | const uint N = 10 * 1024; | 
|---|
| 282 |  | 
|---|
| 283 | if (m_mode == VectorMode) { | 
|---|
| 284 | if (s < N) { | 
|---|
| 285 | to_vector->resize (s); | 
|---|
| 286 | } else { | 
|---|
| 287 | // switch to MapMode | 
|---|
| 288 | QMap<uint, QScriptValueImpl> *m = new QMap<uint, QScriptValueImpl>(); | 
|---|
| 289 | for (uint i = 0; i < oldSize; ++i) { | 
|---|
| 290 | if (to_vector->at(i).isValid()) | 
|---|
| 291 | m->insert(i, to_vector->at(i)); | 
|---|
| 292 | } | 
|---|
| 293 | m->insert(s, QScriptValueImpl()); | 
|---|
| 294 | delete to_vector; | 
|---|
| 295 | to_map = m; | 
|---|
| 296 | m_mode = MapMode; | 
|---|
| 297 | } | 
|---|
| 298 | } | 
|---|
| 299 |  | 
|---|
| 300 | else { | 
|---|
| 301 | if (s < N) { | 
|---|
| 302 | // switch to VectorMode | 
|---|
| 303 | QVector<QScriptValueImpl> *v = new QVector<QScriptValueImpl> (s); | 
|---|
| 304 | QMap<uint, QScriptValueImpl>::const_iterator it = to_map->constBegin(); | 
|---|
| 305 | for ( ; (it != to_map->constEnd()) && (it.key() < s); ++it) | 
|---|
| 306 | (*v) [it.key()] = it.value(); | 
|---|
| 307 | delete to_map; | 
|---|
| 308 | to_vector = v; | 
|---|
| 309 | m_mode = VectorMode; | 
|---|
| 310 | } else { | 
|---|
| 311 | if (!to_map->isEmpty()) { | 
|---|
| 312 | QMap<uint, QScriptValueImpl>::iterator it = --to_map->end(); | 
|---|
| 313 | if (oldSize > s) { | 
|---|
| 314 | // shrink | 
|---|
| 315 | while ((it != to_map->end()) && (it.key() >= s)) { | 
|---|
| 316 | it = to_map->erase(it); | 
|---|
| 317 | --it; | 
|---|
| 318 | } | 
|---|
| 319 | } else { | 
|---|
| 320 | if ((it.key() == oldSize) && !it.value().isValid()) | 
|---|
| 321 | to_map->erase(it); | 
|---|
| 322 | } | 
|---|
| 323 | } | 
|---|
| 324 | to_map->insert(s, QScriptValueImpl()); | 
|---|
| 325 | } | 
|---|
| 326 | } | 
|---|
| 327 | } | 
|---|
| 328 |  | 
|---|
| 329 | inline void QScript::Array::concat(const QScript::Array &other) | 
|---|
| 330 | { | 
|---|
| 331 | uint k = size(); | 
|---|
| 332 | resize (k + other.size()); | 
|---|
| 333 | for (uint i = 0; i < other.size(); ++i) { | 
|---|
| 334 | QScriptValueImpl v = other.at(i); | 
|---|
| 335 | if (! v.isValid()) | 
|---|
| 336 | continue; | 
|---|
| 337 |  | 
|---|
| 338 | assign(k + i, v); | 
|---|
| 339 | } | 
|---|
| 340 | } | 
|---|
| 341 |  | 
|---|
| 342 | inline QScriptValueImpl QScript::Array::pop() | 
|---|
| 343 | { | 
|---|
| 344 | if (isEmpty()) | 
|---|
| 345 | return QScriptValueImpl(); | 
|---|
| 346 |  | 
|---|
| 347 | QScriptValueImpl v; | 
|---|
| 348 |  | 
|---|
| 349 | if (m_mode == VectorMode) | 
|---|
| 350 | v = to_vector->last(); | 
|---|
| 351 | else | 
|---|
| 352 | v = *--to_map->end(); | 
|---|
| 353 |  | 
|---|
| 354 | resize(size() - 1); | 
|---|
| 355 |  | 
|---|
| 356 | return v; | 
|---|
| 357 | } | 
|---|
| 358 |  | 
|---|
| 359 | inline void QScript::Array::sort(const QScriptValueImpl &comparefn) | 
|---|
| 360 | { | 
|---|
| 361 | ArrayElementLessThan lessThan(comparefn); | 
|---|
| 362 | if (m_mode == VectorMode) { | 
|---|
| 363 | qSort(to_vector->begin(), to_vector->end(), lessThan); | 
|---|
| 364 | } else { | 
|---|
| 365 | QList<uint> keys = to_map->keys(); | 
|---|
| 366 | QList<QScriptValueImpl> values = to_map->values(); | 
|---|
| 367 | qStableSort(values.begin(), values.end(), lessThan); | 
|---|
| 368 | const uint len = keys.size(); | 
|---|
| 369 | for (uint i = 0; i < len; ++i) | 
|---|
| 370 | to_map->insert(keys.at(i), values.at(i)); | 
|---|
| 371 | } | 
|---|
| 372 | } | 
|---|
| 373 |  | 
|---|
| 374 | inline void QScript::Array::splice(qsreal start, qsreal deleteCount, | 
|---|
| 375 | const QVector<QScriptValueImpl> &items, | 
|---|
| 376 | Array &other) | 
|---|
| 377 | { | 
|---|
| 378 | const qsreal len = size(); | 
|---|
| 379 | if (start < 0) | 
|---|
| 380 | start = qMax(len + start, qsreal(0)); | 
|---|
| 381 | else if (start > len) | 
|---|
| 382 | start = len; | 
|---|
| 383 | deleteCount = qMax(qMin(deleteCount, len - start), qsreal(0)); | 
|---|
| 384 |  | 
|---|
| 385 | const uint st = uint(start); | 
|---|
| 386 | const uint dc = uint(deleteCount); | 
|---|
| 387 | other.resize(dc); | 
|---|
| 388 |  | 
|---|
| 389 | const uint itemsSize = uint(items.size()); | 
|---|
| 390 |  | 
|---|
| 391 | if (m_mode == VectorMode) { | 
|---|
| 392 | for (uint i = 0; i < dc; ++i) | 
|---|
| 393 | other.assign(i, to_vector->at(st + i)); | 
|---|
| 394 | if (itemsSize > dc) | 
|---|
| 395 | to_vector->insert(st, itemsSize - dc, QScriptValueImpl()); | 
|---|
| 396 | else if (itemsSize < dc) | 
|---|
| 397 | to_vector->remove(st, dc - itemsSize); | 
|---|
| 398 | for (uint i = 0; i < itemsSize; ++i) | 
|---|
| 399 | to_vector->replace(st + i, items.at(i)); | 
|---|
| 400 | } else { | 
|---|
| 401 | for (uint i = 0; i < dc; ++i) | 
|---|
| 402 | other.assign(i, to_map->take(st + i)); | 
|---|
| 403 | uint del = itemsSize - dc; | 
|---|
| 404 | if (del != 0) { | 
|---|
| 405 | for (uint i = st; i < uint(len); ++i) { | 
|---|
| 406 | if (to_map->contains(i)) | 
|---|
| 407 | to_map->insert(i + del, to_map->take(i)); | 
|---|
| 408 | } | 
|---|
| 409 | resize(uint(len) + del); | 
|---|
| 410 | } | 
|---|
| 411 | for (uint i = 0; i < itemsSize; ++i) | 
|---|
| 412 | to_map->insert(st + i, items.at(i)); | 
|---|
| 413 | } | 
|---|
| 414 | } | 
|---|
| 415 |  | 
|---|
| 416 | inline QList<uint> QScript::Array::keys() const | 
|---|
| 417 | { | 
|---|
| 418 | if (m_mode == VectorMode) | 
|---|
| 419 | return QList<uint>(); | 
|---|
| 420 | else | 
|---|
| 421 | return to_map->keys(); | 
|---|
| 422 | } | 
|---|
| 423 |  | 
|---|
| 424 | QT_END_NAMESPACE | 
|---|
| 425 |  | 
|---|
| 426 | #endif // QT_NO_SCRIPT | 
|---|
| 427 |  | 
|---|
| 428 | #endif // QSCRIPTARRAY_P_H | 
|---|