1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
|
//===-- qlogo/datum_array.cpp - Array class implementation -------*-
// C++ -*-===//
//
// This file is part of QLogo.
//
// QLogo is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// QLogo is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with QLogo. If not, see <http://www.gnu.org/licenses/>.
//
//===----------------------------------------------------------------------===//
///
/// \file
/// This file contains the implementation of the Array class.
/// An array may contain words, lists or arrays.
/// Is is implemented using QVector.
///
//===----------------------------------------------------------------------===//
#include "datum.h"
#include <qdebug.h>
QList<void *> aryVisited;
QList<void *> otherAryVisited;
Array::Array(int aOrigin, int aSize) {
origin = aOrigin;
array.reserve(aSize);
for (int i = 0; i < aSize; ++i) {
array.append(DatumP(new List));
}
}
Array::Array(int aOrigin, List *source) {
origin = aOrigin;
array.reserve(source->size());
DatumP ptr = source->head;
while (ptr != nothing) {
array.push_back(ptr.listNodeValue()->item);
ptr = ptr.listNodeValue()->next;
}
}
Array::~Array() {}
Datum::DatumType Array::isa() { return Datum::arrayType; }
QString Array::name() {
static const QString retval("Array");
return retval;
}
QString Array::printValue(bool fullPrintp, int printDepthLimit,
int printWidthLimit) {
QString retval = "";
auto iter = array.begin();
if (iter == array.end()) {
return retval;
}
if ((printDepthLimit == 0) || (printWidthLimit == 0)) {
return "...";
}
int printWidth = printWidthLimit - 1;
retval = iter->showValue(fullPrintp, printDepthLimit - 1, printWidthLimit);
while (++iter != array.end()) {
retval.append(QString(" "));
if (printWidth == 0) {
retval.append("...");
break;
}
retval.append(
iter->showValue(fullPrintp, printDepthLimit - 1, printWidthLimit));
--printWidth;
}
return retval;
}
QString Array::showValue(bool fullPrintp, int printDepthLimit,
int printWidthLimit) {
if (!aryVisited.contains(this)) {
aryVisited.push_back(this);
QString retval = "{";
retval.append(printValue(fullPrintp, printDepthLimit, printWidthLimit));
retval.append("}");
aryVisited.removeOne(this);
return retval;
}
return "...";
}
bool Array::isEqual(DatumP other, bool) {
Array *o = other.arrayValue();
return this == o;
}
int Array::size() { return array.size(); }
void Array::append(DatumP value) { array.append(value); }
bool Array::isIndexInRange(int anIndex) {
int index = anIndex - origin;
return ((index >= 0) && (index < array.size()));
}
void Array::setItem(int anIndex, DatumP aValue) {
int index = anIndex - origin;
array[index] = aValue;
}
void Array::setButfirstItem(DatumP aValue) {
Q_ASSERT(array.size() > 0);
auto estart = array.begin();
++estart;
array.erase(estart, array.end());
array.reserve(aValue.arrayValue()->size() + 1);
array.append(aValue.arrayValue()->array);
}
void Array::setFirstItem(DatumP aValue) { array[0] = aValue; }
bool Array::containsDatum(DatumP aDatum, bool ignoreCase) {
for (int i = 0; i < array.size(); ++i) {
DatumP e = array[i];
if (e == aDatum)
return true;
if (e.datumValue()->containsDatum(aDatum, ignoreCase))
return true;
}
return false;
}
bool Array::isMember(DatumP aDatum, bool ignoreCase) {
for (int i = 0; i < array.size(); ++i) {
if (array[i].isEqual(aDatum, ignoreCase))
return true;
}
return false;
}
DatumP Array::fromMember(DatumP aDatum, bool ignoreCase) {
for (int i = 0; i < array.size(); ++i) {
if (array[i].isEqual(aDatum, ignoreCase)) {
Array *retval = new Array(origin, 0);
retval->array.reserve(array.size() - i);
while (i < array.size()) {
retval->append(array[i]);
++i;
}
return DatumP(retval);
}
}
return DatumP(new Array(origin, 0));
}
DatumP Array::datumAtIndex(int anIndex) {
int index = anIndex - origin;
Q_ASSERT((index >= 0) && (index < array.size()));
return array[index];
}
DatumP Array::first() { return DatumP(new Word(origin)); }
DatumP Array::last() {
Q_ASSERT(array.size() > 0);
return array[array.size() - 1];
}
DatumP Array::butfirst() {
Array *retval = new Array(origin, 0);
retval->array = array.mid(1, array.size() - 1);
return DatumP(retval);
}
DatumP Array::butlast() {
Array *retval = new Array(origin, 0);
retval->array = array.mid(0, array.size() - 1);
return DatumP(retval);
}
ArrayIterator Array::newIterator() { return ArrayIterator(&array); }
|