diff options
author | Sebastian Geerken <devnull@localhost> | 2014-10-27 12:15:51 +0100 |
---|---|---|
committer | Sebastian Geerken <devnull@localhost> | 2014-10-27 12:15:51 +0100 |
commit | 596b929b188de4ee64e1a54a86cb4fc3ff00577f (patch) | |
tree | dc978309dff1da73d9e501a8475002b022345de2 | |
parent | d417ed0233c7c152b4b884c40aba50e8daf15a17 (diff) |
Some smaller changes in lout (backport from RTFL).
-rw-r--r-- | lout/container.cc | 72 | ||||
-rw-r--r-- | lout/container.hh | 42 | ||||
-rw-r--r-- | lout/unicode.cc | 20 |
3 files changed, 120 insertions, 14 deletions
diff --git a/lout/container.cc b/lout/container.cc index 2dd56255..366a58fa 100644 --- a/lout/container.cc +++ b/lout/container.cc @@ -120,6 +120,11 @@ Vector::~Vector() DBG_OBJ_DELETE (); } +int Vector::size () +{ + return numElements; +} + void Vector::put(Object *newElement, int newPos) { if (newPos == -1) @@ -296,6 +301,32 @@ List::~List() clear(); } +int List::size () +{ + return numElements; +} + +bool List::equals(Object *other) +{ + List *otherList = (List*)other; + Node *node1 = first, *node2 = otherList->first; + while (node1 != NULL && node2 != NULL ) { + if (!node1->object->equals (node2->object)) + return false; + node1 = node1->next; + node2 = node2->next; + } + return node1 == NULL && node2 == NULL; +} + +int List::hashValue() +{ + int h = 0; + for (Node *node = first; node; node = node->next) + h = h ^ node->object->hashValue (); + return h; +} + void List::clear() { while (first) { @@ -325,6 +356,28 @@ void List::append(Object *element) numElements++; } +bool List::insertBefore(object::Object *beforeThis, object::Object *neew) +{ + Node *beforeCur, *cur; + + for (beforeCur = NULL, cur = first; cur; beforeCur = cur, cur = cur->next) { + if (cur->object == beforeThis) { + Node *newNode = new Node; + newNode->next = cur; + newNode->object = neew; + + if (beforeCur) + beforeCur->next = newNode; + else + first = newNode; + + numElements++; + return true; + } + } + + return false; +} bool List::remove0(Object *element, bool compare, bool doNotDeleteAtAll) { @@ -392,6 +445,8 @@ HashSet::HashSet(bool ownerOfObjects, int tableSize) table = new Node*[tableSize]; for (int i = 0; i < tableSize; i++) table[i] = NULL; + + numElements = 0; } HashSet::~HashSet() @@ -419,6 +474,11 @@ HashSet::~HashSet() delete[] table; } +int HashSet::size () +{ + return numElements; +} + HashSet::Node *HashSet::createNode() { return new Node; @@ -447,13 +507,15 @@ HashSet::Node *HashSet::insertNode(Object *object) { // Look whether object is already contained. Node *node = findNode(object); - if (node) + if (node) { clearNode(node); - else { + numElements--; + } else { int h = calcHashValue(object); node = createNode (); node->next = table[h]; table[h] = node; + numElements++; } node->object = object; @@ -491,6 +553,7 @@ bool HashSet::remove(Object *object) clearNode (cur); delete cur; + numElements--; return true; } @@ -662,6 +725,11 @@ Stack::~Stack() pop (); } +int Stack::size () +{ + return numElements; +} + void Stack::push (object::Object *object) { Node *newTop = new Node (); diff --git a/lout/container.hh b/lout/container.hh index a7f01732..f8c22439 100644 --- a/lout/container.hh +++ b/lout/container.hh @@ -93,6 +93,7 @@ class Collection: public Collection0 public: void intoStringBuffer(misc::StringBuffer *sb); inline Iterator iterator() { Iterator it(createIterator()); return it; } + virtual int size() = 0; }; @@ -128,6 +129,8 @@ public: Vector(int initSize, bool ownerOfObjects); ~Vector(); + int size(); + void put(object::Object *newElement, int newPos = -1); void insert(object::Object *newElement, int pos); @@ -137,15 +140,15 @@ public: * Notice that insertion is not very efficient, unless the position * is rather at the end. */ - inline void insertSorted(object::Object *newElement, - object::Comparator *comparator = - &object::standardComparator) - { insert (newElement, bsearch (newElement, false, comparator)); } + inline int insertSorted(object::Object *newElement, + object::Comparator *comparator = + &object::standardComparator) + { int pos = bsearch (newElement, false, comparator); + insert (newElement, pos); return pos; } void remove(int pos); inline object::Object *get(int pos) const { return (pos >= 0 && pos < numElements) ? array[pos] : NULL; } - inline int size() { return numElements; } void clear(); void sort(object::Comparator *comparator = &object::standardComparator); int bsearch(Object *key, bool mustExist, int start, int end, @@ -195,8 +198,14 @@ public: List(bool ownerOfObjects); ~List(); + bool equals(Object *other); + int hashValue(); + + int size (); + void clear(); void append(object::Object *element); + bool insertBefore(object::Object *beforeThis, object::Object *neew); inline bool removeRef(object::Object *element) { return remove0(element, false, false); } inline bool remove(object::Object *element) @@ -225,7 +234,7 @@ protected: }; Node **table; - int tableSize; + int tableSize, numElements; bool ownerOfObjects; inline int calcHashValue(object::Object *object) const @@ -261,6 +270,8 @@ public: HashSet(bool ownerOfObjects, int tableSize = 251); ~HashSet(); + int size (); + void put (object::Object *object); bool contains (object::Object *key) const; bool remove (object::Object *key); @@ -334,6 +345,8 @@ public: Stack (bool ownerOfObjects); ~Stack(); + int size (); + void push (object::Object *object); void pushUnder (object::Object *object); inline object::Object *getTop () const { return top ? top->object : NULL; } @@ -393,11 +406,16 @@ public: Collection () { this->base = NULL; } ~Collection () { if (this->base) delete this->base; } + bool equals(Object *other) + { return this->base->equals (((Collection<T>*)other)->base); } + + int hashValue() { return this->base->hashValue (); } + void intoStringBuffer(misc::StringBuffer *sb) { this->base->intoStringBuffer(sb); } - inline Iterator<T> iterator() { Iterator<T> it; it.base = this->base->iterator(); return it; } + inline int size() { return this->base->size (); } }; @@ -414,14 +432,14 @@ public: { ((untyped::Vector*)this->base)->put(newElement, newPos); } inline void insert(T *newElement, int pos) { ((untyped::Vector*)this->base)->insert(newElement, pos); } - inline void insertSorted(T *newElement, + inline bool insertSorted(T *newElement, object::Comparator *comparator = &object::standardComparator) - { ((untyped::Vector*)this->base)->insertSorted(newElement, comparator); } + { return ((untyped::Vector*)this->base)->insertSorted(newElement, + comparator); } inline void remove(int pos) { ((untyped::Vector*)this->base)->remove(pos); } inline T *get(int pos) const { return (T*)((untyped::Vector*)this->base)->get(pos); } - inline int size() const { return ((untyped::Vector*)this->base)->size(); } inline void clear() { ((untyped::Vector*)this->base)->clear(); } inline void sort(object::Comparator *comparator = &object::standardComparator) @@ -451,6 +469,8 @@ public: inline void clear() { ((untyped::List*)this->base)->clear(); } inline void append(T *element) { ((untyped::List*)this->base)->append(element); } + inline bool insertBefore(object::Object *beforeThis, object::Object *neew) + { return ((untyped::List*)this->base)->insertBefore(beforeThis, neew); } inline bool removeRef(T *element) { return ((untyped::List*)this->base)->removeRef(element); } inline bool remove(T *element) { @@ -458,7 +478,6 @@ public: inline bool detachRef(T *element) { return ((untyped::List*)this->base)->detachRef(element); } - inline int size() const { return ((untyped::List*)this->base)->size(); } inline bool isEmpty() const { return ((untyped::List*)this->base)->isEmpty(); } inline T *getFirst() const @@ -521,7 +540,6 @@ public: inline T *getTop () const { return (T*)((untyped::Stack*)this->base)->getTop (); } inline void pop () { ((untyped::Stack*)this->base)->pop (); } - inline int size() const { return ((untyped::Stack*)this->base)->size(); } }; } // namespace untyped diff --git a/lout/unicode.cc b/lout/unicode.cc index 4f0f0b3b..9fc2f3d3 100644 --- a/lout/unicode.cc +++ b/lout/unicode.cc @@ -1,3 +1,23 @@ +/* + * Dillo Widget + * + * Copyright 2012, 2013 Sebastian Geerken <sgeerken@dillo.org> + * + * This program 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. + * + * This program 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 this program. If not, see <http://www.gnu.org/licenses/>. + */ + + #include "unicode.hh" #include "misc.hh" |