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
|
/************************************************************************
**
** Copyright (C) 2024 Kevin B. Hendricks, Stratford Ontario Canada
**
** This file is part of PageEdit.
**
** PageEdit 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 3 of the License, or
** (at your option) any later version.
**
** PageEdit 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 PageEdit. If not, see <http://www.gnu.org/licenses/>.
**
*************************************************************************/
#include <QDebug>
#include "TagAtts.h"
TagAtts::TagAtts()
: m_n(0), m_mapping(QHash<QString, TagAtts::TAttribute*>()), m_anchor(new TagAtts::TAttribute("",""))
{
m_anchor->prev = m_anchor;
m_anchor->next = m_anchor;
}
// copy constructor linear time
TagAtts::TagAtts(const TagAtts &other)
: m_n(0), m_mapping(QHash<QString, TagAtts::TAttribute*>()), m_anchor(new TagAtts::TAttribute("",""))
{
m_anchor->prev = m_anchor;
m_anchor->next = m_anchor;
QList<std::pair<QString, QString> > pairlist = other.pairs();
for(unsigned int i=0; i < other.size(); i++) {
insert(pairlist.at(i).first, pairlist.at(i).second);
}
}
// assignment linear time
TagAtts& TagAtts::operator=(const TagAtts &other)
{
if (m_n > 0) {
m_mapping.clear();
TagAtts::TAttribute * patt = m_anchor->next;
while(patt && (patt != m_anchor)) {
TagAtts::TAttribute * natt = patt->next;
delete patt;
patt = natt;
m_n--;
}
m_anchor->next = m_anchor;
m_anchor->prev = m_anchor;
m_n = 0;
}
QList<std::pair<QString, QString> > pairlist = other.pairs();
for(unsigned int i=0; i < other.size(); i++) {
insert(pairlist.at(i).first, pairlist.at(i).second);
}
return *this;
}
// comparison
bool TagAtts::operator==(const TagAtts &other)
{
if (m_n != other.size()) return false;
if (other.keys() != keys()) return false;
if (other.values() != values()) return false;
return true;
}
bool TagAtts::operator!=(const TagAtts &other)
{
if (m_n != other.size()) return true;
if (other.keys() != keys()) return true;
if (other.values() != values()) return true;
return false;
}
QString& TagAtts::operator[](const QString & key)
{
if (!m_mapping.contains(key)) insert(key, "");
return m_mapping[key]->value;
}
const QString TagAtts::operator[](const QString & key) const noexcept
{
return value(key);
}
TagAtts::~TagAtts()
{
m_n = 0;
m_mapping.clear();
TagAtts::TAttribute * patt = m_anchor->next;
while(patt && (patt != m_anchor)) {
TagAtts::TAttribute * natt = patt->next;
delete patt;
patt = natt;
}
delete m_anchor;
m_anchor = nullptr;
}
void TagAtts::insert(const QString &key, const QString &value)
{
if (m_mapping.contains(key)) {
TagAtts::TAttribute* patt = m_mapping[key];
patt->value = value;
} else {
TagAtts::TAttribute* patt = new TAttribute(key, value);
// circular all prev and next values should exist
// insert at end of list (just before anchor)
TagAtts::TAttribute* last = m_anchor->prev;
last->next = patt;
patt->prev = last;
patt->next = m_anchor;
m_anchor->prev = patt;
m_mapping[key] = patt;
m_n++;
}
}
void TagAtts::remove(const QString &key)
{
if (!m_mapping.contains(key)) return;
TagAtts::TAttribute* patt = m_mapping[key];
// remove it from doubly-linked list
TagAtts::TAttribute* pnext = patt->next;
TagAtts::TAttribute* pprev = patt->prev;
pprev->next = pnext;
pnext->prev = pprev;
m_mapping.remove(key);
delete patt;
m_n--;
}
QString TagAtts::value(const QString &key, const QString &altvalue) const
{
if (!m_mapping.contains(key)) return altvalue;
TagAtts::TAttribute* patt = m_mapping[key];
return patt->value;
}
QStringList TagAtts::keys() const
{
QStringList keylist;
if (m_n == 0) return keylist;
TagAtts::TAttribute* patt = m_anchor->next;
while (patt && (patt != m_anchor)) {
keylist << patt->key;
patt = patt->next;
}
return keylist;
}
QStringList TagAtts::values() const
{
QStringList vallist;
if (m_n == 0) return vallist;
TagAtts::TAttribute* patt = m_anchor->next;
while (patt && (patt != m_anchor)) {
vallist << patt->value;
patt = patt->next;
}
return vallist;
}
QList<std::pair<QString, QString> > TagAtts::pairs() const
{
QList<std::pair<QString, QString> > plist;
if (m_n == 0) return plist;
TagAtts::TAttribute* patt = m_anchor->next;
while (patt && (patt != m_anchor)) {
std::pair<QString, QString> apair;
apair.first = patt->key;
apair.second = patt->value;
plist << apair;
patt = patt->next;
}
return plist;
}
|