[Scummvm-git-logs] scummvm master -> 83ffb047f710be0e18bdb2a2bcb07d6735febace

grisenti noreply at scummvm.org
Wed Jan 4 17:07:59 UTC 2023


This automated email contains information about 4 new commits which have been
pushed to the 'scummvm' repo located at https://github.com/scummvm/scummvm .

Summary:
1c2c3ed7e6 HPL1: fix use of invalid iterators
f74a7659c7 HPL1: fix save description format
898b1aeee7 HPL1: add tree container to std folder
83ffb047f7 HPL1: replace Std::map/multimap internal container


Commit: 1c2c3ed7e6fa3b4997947bba4899658a3501c0df
    https://github.com/scummvm/scummvm/commit/1c2c3ed7e6fa3b4997947bba4899658a3501c0df
Author: grisenti (emanuele at grisenti.net)
Date: 2023-01-04T18:06:27+01:00

Commit Message:
HPL1: fix use of invalid iterators

Changed paths:
    engines/hpl1/penumbra-overture/Inventory.cpp


diff --git a/engines/hpl1/penumbra-overture/Inventory.cpp b/engines/hpl1/penumbra-overture/Inventory.cpp
index 0195c0c9ca6..081d9be27a3 100644
--- a/engines/hpl1/penumbra-overture/Inventory.cpp
+++ b/engines/hpl1/penumbra-overture/Inventory.cpp
@@ -39,6 +39,7 @@
 #include "hpl1/penumbra-overture/SaveHandler.h"
 
 #include "hpl1/penumbra-overture/GlobalInit.h"
+#include "hpl1/algorithms.h"
 
 //////////////////////////////////////////////////////////////////////////
 // CONSTRUCTORS
@@ -1442,31 +1443,26 @@ void cInventory::AddCombineCallback(const tString &asItem1, const tString &asIte
 
 //-----------------------------------------------------------------------
 
-void cInventory::RemovePickupCallback(const tString &asFunction) {
-	tInventoryPickupCallbackMapIt it = m_mapPickupCallbacks.begin();
-	for (; it != m_mapPickupCallbacks.end();) {
-		cInventoryPickupCallback *pCallback = it->second;
-		if (pCallback->msFunction == asFunction) {
-			m_mapPickupCallbacks.erase(it++);
-			hplDelete(pCallback);
-		} else {
-			it++;
+template<typename Map>
+void removeCallbacks(Map &callbackMap, const tString &fn) {
+	auto newEnd = Hpl1::removeIf(callbackMap.begin(), callbackMap.end(), [&](typename Map::value_type& p) -> bool {
+		if (p.second->msFunction == fn) {
+			hplDelete(p.second);
+			return true;
 		}
-	}
+		return false;
+	});
+	callbackMap.erase(newEnd, callbackMap.end());
+}
+
+void cInventory::RemovePickupCallback(const tString &asFunction) {
+	removeCallbacks(m_mapPickupCallbacks, asFunction);
 }
 
 void cInventory::RemoveUseCallback(const tString &asFunction) {
-	tInventoryUseCallbackMapIt it = m_mapUseCallbacks.begin();
-	for (; it != m_mapUseCallbacks.end();) {
-		cInventoryUseCallback *pCallback = it->second;
-		if (pCallback->msFunction == asFunction) {
-			m_mapUseCallbacks.erase(it++);
-			hplDelete(pCallback);
-		} else {
-			it++;
-		}
-	}
+	removeCallbacks(m_mapUseCallbacks, asFunction);
 }
+
 void cInventory::RemoveCombineCallback(const tString &asFunction) {
 	tInventoryCombineCallbackListIt it = mlstCombineCallbacks.begin();
 	for (; it != mlstCombineCallbacks.end(); ++it) {


Commit: f74a7659c73e2ee670f34d7edd5d56a84103f992
    https://github.com/scummvm/scummvm/commit/f74a7659c73e2ee670f34d7edd5d56a84103f992
Author: grisenti (emanuele at grisenti.net)
Date: 2023-01-04T18:06:39+01:00

Commit Message:
HPL1: fix save description format

Changed paths:
    engines/hpl1/penumbra-overture/SaveHandler.cpp


diff --git a/engines/hpl1/penumbra-overture/SaveHandler.cpp b/engines/hpl1/penumbra-overture/SaveHandler.cpp
index f07fb909b7a..c04900fa5ea 100644
--- a/engines/hpl1/penumbra-overture/SaveHandler.cpp
+++ b/engines/hpl1/penumbra-overture/SaveHandler.cpp
@@ -564,7 +564,7 @@ void cSaveHandler::AutoSave(const tWString &asDir, int alMaxSaves) {
 	sMapName = cString::ReplaceCharToW(sMapName, _W("\n"), _W(" "));
 	sMapName = cString::ReplaceCharToW(sMapName, _W(":"), _W(" "));
 	cDate date = mpInit->mpGame->GetSystem()->GetLowLevel()->getDate();
-	tWString sFile = Common::U32String::format("%S: %S %d-%02d-%02d %02d:%02d:%02d",
+	tWString sFile = Common::U32String::format("%S: %S %d-%d-%d %d:%d:%d",
 			 asDir.c_str(),
 			 sMapName.c_str(),
 			 date.year,


Commit: 898b1aeee7ea9ed05f89b48f25217e94ea7691fe
    https://github.com/scummvm/scummvm/commit/898b1aeee7ea9ed05f89b48f25217e94ea7691fe
Author: grisenti (emanuele at grisenti.net)
Date: 2023-01-04T18:07:03+01:00

Commit Message:
HPL1: add tree container to std folder

Changed paths:
  A engines/hpl1/std/tree.h


diff --git a/engines/hpl1/std/tree.h b/engines/hpl1/std/tree.h
new file mode 100644
index 00000000000..6df7495750e
--- /dev/null
+++ b/engines/hpl1/std/tree.h
@@ -0,0 +1,250 @@
+/* ScummVM - Graphic Adventure Engine
+ *
+ * ScummVM is the legal property of its developers, whose names
+ * are too numerous to list here. Please refer to the COPYRIGHT
+ * file distributed with this source distribution.
+ *
+ * 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/>.
+ *
+ */
+
+#ifndef HPL1_TREE_H
+#define HPL1_TREE_H
+
+#include "common/func.h"
+#include "hpl1/std/pair.h"
+
+namespace Hpl1 {
+
+namespace Std {
+
+template<class Key, class Val, class KeyComp = Common::Less<Key> >
+class Tree {
+public:
+	using ValueType = pair<Key, Val>;
+
+	struct Node {
+		Node *parent;
+		Node *left;
+		Node *right;
+		ValueType value;
+	};
+
+	template<typename Ref, typename Ptr>
+	class Iterator {
+	public:
+		friend Tree;
+
+		Iterator() = default;
+		Iterator(const Iterator &) = default;
+		Iterator &operator=(const Iterator &) = default;
+
+		Ref operator*() const { return _current->value; }
+
+		Ptr operator->() const { return &_current->value; }
+
+		Iterator &operator++() {
+			if (_current->right) {
+				_current = leftmost(_current->right);
+			} else {
+				auto p = _current->parent;
+				while (p && p->right == _current) {
+					_current = p;
+					p = p->parent;
+				}
+				_current = p;
+			}
+			return *this;
+		}
+
+		Iterator operator++(int) {
+			auto temp = *this;
+			++(*this);
+			return temp;
+		}
+
+		bool operator==(const Iterator &rhs) const {
+			return _current == rhs._current;
+		}
+
+		bool operator!=(const Iterator &rhs) const {
+			return _current != rhs._current;
+		}
+
+	private:
+		Iterator(Node *n) : _current(n) {}
+
+		Node *_current = nullptr;
+	};
+
+	using BasicIterator = Iterator<ValueType &, ValueType *>;
+	using ConstIterator = Iterator<const ValueType &, const ValueType *>;
+
+	void clear() {
+		erase(begin(), end());
+		_size = 0;
+		_root = nullptr;
+		_leftmost = nullptr;
+	}
+
+	BasicIterator begin() { return BasicIterator{_leftmost}; }
+
+	ConstIterator begin() const { return ConstIterator{_leftmost}; }
+
+	BasicIterator end() { return BasicIterator{nullptr}; }
+
+	ConstIterator end() const { return ConstIterator{nullptr}; }
+
+	BasicIterator lowerBound(const Key &key) {
+		Node *it = _root;
+		Node *res = nullptr;
+		while (it) {
+			if (!_comp(it->value.first, key)) {
+				res = it;
+				it = it->left;
+			} else {
+				it = it->right;
+			}
+		}
+		return BasicIterator{res};
+	}
+
+	ConstIterator lowerBound(const Key &key) const {
+		Node *it = _root;
+		Node *res = nullptr;
+		while (it) {
+			if (!_comp(it->value.first, key)) {
+				res = it;
+				it = it->left;
+			} else {
+				it = it->right;
+			}
+		}
+		return ConstIterator{res};
+	}
+
+	BasicIterator upperBound(const Key &key) {
+		Node *it = _root;
+		Node *res = nullptr;
+		while (it) {
+			if (!_comp(key, it->value.first)) {
+				it = it->right;
+			} else {
+				res = it;
+				it = it->left;
+			}
+		}
+		return BasicIterator{res};
+	}
+
+	ConstIterator upperBound(const Key &key) const {
+		Node *it = _root;
+		Node *res = nullptr;
+		while (it) {
+			if (!_comp(key, it->value.first)) {
+				it = it->right;
+			} else {
+				res = it;
+				it = it->left;
+			}
+		}
+		return ConstIterator{res};
+	}
+
+	BasicIterator erase(BasicIterator it) {
+		auto const u = it._current;
+		if (!u)
+			return {nullptr};
+		auto v = merge(u->left, u->right);
+		if (u->parent) {
+			auto const p = u->parent;
+			if (p->left == u)
+				p->left = v;
+			else
+				p->right = v;
+		} else {
+			_root = v;
+		}
+		if (v)
+			v->parent = u->parent;
+		if (u == _leftmost)
+			_leftmost = v ? v : u->parent;
+		--_size;
+		auto const ret = ++it;
+		delete u;
+		return ret;
+	}
+
+	BasicIterator erase(BasicIterator first, BasicIterator last) {
+		while (first != last)
+			erase(first++);
+		return last;
+	}
+
+	BasicIterator insert(const ValueType &val) {
+		auto it = &_root;
+		Node *parent = nullptr;
+		bool wentRight = false;
+		while (*it) {
+			parent = *it;
+			if (_comp((*it)->value.first, val.first)) {
+				it = &(*it)->right;
+				wentRight = true;
+			} else {
+				it = &(*it)->left;
+			}
+		}
+		*it = new Node{parent, nullptr, nullptr, val};
+		if (!wentRight)
+			_leftmost = *it;
+		++_size;
+		return BasicIterator{*it};
+	}
+
+	std::size_t size() const { return _size; }
+
+	bool isEmpty() const { return _size == 0; }
+
+	~Tree() { clear(); }
+
+private:
+	KeyComp _comp;
+	Node *_root = nullptr;
+	Node *_leftmost = nullptr;
+	std::size_t _size = 0;
+
+	Node *merge(Node *left, Node *right) {
+		if (!right)
+			return left;
+		else if (!left)
+			return right;
+		else {
+			auto lm = leftmost(right);
+			lm->left = left;
+			left->parent = lm;
+			return right;
+		}
+	}
+
+	static Node *leftmost(Node *n) {
+		while (n->left)
+			n = n->left;
+		return n;
+	}
+};
+
+} // namespace Std
+} // namespace Hpl1
+
+#endif
\ No newline at end of file


Commit: 83ffb047f710be0e18bdb2a2bcb07d6735febace
    https://github.com/scummvm/scummvm/commit/83ffb047f710be0e18bdb2a2bcb07d6735febace
Author: grisenti (emanuele at grisenti.net)
Date: 2023-01-04T18:07:46+01:00

Commit Message:
HPL1: replace Std::map/multimap internal container

Changed paths:
    engines/hpl1/std/map.h
    engines/hpl1/std/multimap.h


diff --git a/engines/hpl1/std/map.h b/engines/hpl1/std/map.h
index 477cf8254a2..14db26f9bda 100644
--- a/engines/hpl1/std/map.h
+++ b/engines/hpl1/std/map.h
@@ -22,8 +22,8 @@
 #ifndef HPL1_STD_MAP_H
 #define HPL1_STD_MAP_H
 
-#include "hpl1/algorithms.h"
 #include "hpl1/std/pair.h"
+#include "hpl1/std/tree.h"
 
 namespace Hpl1 {
 namespace Std {
@@ -31,9 +31,9 @@ namespace Std {
 template<class Key, class Val, class CompFunc = Common::Less<Key> >
 class map {
 public:
-	using value_type = pair<Key, Val>;
-	using iterator = typename Common::Array<value_type>::iterator;
-	using const_iterator = typename Common::Array<value_type>::const_iterator;
+	using value_type = typename Tree<Key, Val, CompFunc>::ValueType;
+	using iterator = typename Tree<Key, Val, CompFunc>::BasicIterator;
+	using const_iterator = typename Tree<Key, Val, CompFunc>::ConstIterator;
 
 	/**
 	 * Clears the map
@@ -75,31 +75,29 @@ public:
 	 * not less than the given key
 	 */
 	const_iterator lower_bound(const Key &key) const {
-		return lowerBound(this->begin(), this->end(), key, [&](value_type const &p, Key const &k) { return _comp(p.first, k); });
+		return _items.lowerBound(key);
 	}
 
 	iterator lower_bound(const Key &key) {
-		return lowerBound(this->begin(), this->end(), key, [&](value_type const &p, Key const &k) { return _comp(p.first, k); });
+		return _items.lowerBound(key);
 	}
 
 	iterator upper_bound(const Key &key) {
-		return upperBound(this->begin(), this->end(), key, [&](Key const &k, value_type const &p) { return _comp(k, p.first); });
+		return _items.upperBound(key);
 	}
 
 	/**
 	 * Find the entry with the given key
 	 */
 	iterator find(const Key &theKey) {
-		iterator it = this->lower_bound(theKey);
-
+		iterator it = _items.lowerBound(theKey);
 		if (it != this->end() && compareEqual(it->first, theKey))
 			return it;
 		return this->end();
 	}
 
 	const_iterator find(const Key &theKey) const {
-		const_iterator it = this->lower_bound(theKey);
-
+		const_iterator it = _items.lowerBound(theKey);
 		if (it != this->end() && compareEqual(it->first, theKey))
 			return it;
 		return this->end();
@@ -109,15 +107,11 @@ public:
 	 * Square brackets operator accesses items by key, creating if necessary
 	 */
 	Val &operator[](const Key &theKey) {
-		iterator it = this->lower_bound(theKey);
+		iterator it = _items.lowerBound(theKey);
 		if (it == this->end() || !compareEqual(it->first, theKey)) {
-			size_t idx = it - this->begin();
-			_items.insert_at(idx, {});
-			_items[idx].first = theKey;
-			return _items[idx].second;
-		} else {
-			return _items[it - this->begin()].second;
+			return _items.insert(theKey).second;
 		}
+		return *it->second;
 	}
 
 	/**
@@ -139,16 +133,9 @@ public:
 	}
 
 	pair<iterator, bool> insert(const value_type &val) {
-		if (_items.begin() == nullptr) {
-			_items.push_back(val);
-			return {_items.begin(), true};
-		}
-		iterator it = this->lower_bound(val.first);
-		if (it == this->end() || !compareEqual(it->first, val.first)) {
-			size_t idx = it - this->begin();
-			_items.insert_at(idx, val);
-			return {this->begin() + idx, true};
-		}
+		iterator it = _items.lowerBound(val.first);
+		if (it == this->end() || !compareEqual(it->first, val.first))
+			return {_items.insert(val), true};
 		return {it, false};
 	}
 
@@ -160,7 +147,7 @@ public:
 	}
 
 	bool empty() const {
-		return _items.empty();
+		return _items.isEmpty();
 	}
 
 	/**
@@ -172,7 +159,6 @@ public:
 			if (compareEqual(it->first, theKey))
 				++count_;
 		}
-
 		return count_;
 	}
 
@@ -181,7 +167,7 @@ private:
 		return !_comp(a, b) && !_comp(b, a);
 	}
 
-	Common::Array<value_type> _items;
+	Tree<Key, Val, CompFunc> _items;
 	CompFunc _comp;
 };
 
diff --git a/engines/hpl1/std/multimap.h b/engines/hpl1/std/multimap.h
index e0ca492b9f3..a20106c9132 100644
--- a/engines/hpl1/std/multimap.h
+++ b/engines/hpl1/std/multimap.h
@@ -22,7 +22,7 @@
 #ifndef HPL1_STD_MULTIMAP_H
 #define HPL1_STD_MULTIMAP_H
 
-#include "common/array.h"
+#include "hpl1/std/tree.h"
 #include "hpl1/std/pair.h"
 
 namespace Hpl1 {
@@ -32,9 +32,9 @@ namespace Std {
 template<class Key, class Val, class CompFunc = Common::Less<Key> >
 class multimap {
 public:
-	using value_type = pair<Key, Val>;
-	using iterator = typename Common::Array<value_type>::iterator;
-	using const_iterator = typename Common::Array<value_type>::const_iterator;
+	using value_type = typename Tree<Key, Val, CompFunc>::ValueType;
+	using iterator = typename Tree<Key, Val, CompFunc>::BasicIterator;
+	using const_iterator = typename Tree<Key, Val, CompFunc>::ConstIterator;
 
 	/**
 	 * Clears the map
@@ -76,31 +76,29 @@ public:
 	 * not less than the given key
 	 */
 	const_iterator lower_bound(const Key &key) const {
-		return lowerBound(this->begin(), this->end(), key, [&](value_type const &p, Key const &k) { return _comp(p.first, k); });
+		return _items.lowerBound(key);
 	}
 
 	iterator lower_bound(const Key &key) {
-		return lowerBound(this->begin(), this->end(), key, [&](value_type const &p, Key const &k) { return _comp(p.first, k); });
+		return _items.lowerBound(key);
 	}
 
 	iterator upper_bound(const Key &key) {
-		return upperBound(this->begin(), this->end(), key, [&](Key const &k, value_type const &p) { return _comp(k, p.first); });
+		return _items.upperBound(key);
 	}
 
 	/**
 	 * Find the entry with the given key
 	 */
 	iterator find(const Key &theKey) {
-		iterator it = this->lower_bound(theKey);
-
+		iterator it = _items.lowerBound(theKey);
 		if (it != this->end() && compareEqual(it->first, theKey))
 			return it;
 		return this->end();
 	}
 
 	const_iterator find(const Key &theKey) const {
-		const_iterator it = this->lower_bound(theKey);
-
+		const_iterator it = _items.lowerBound(theKey);
 		if (it != this->end() && compareEqual(it->first, theKey))
 			return it;
 		return this->end();
@@ -126,10 +124,7 @@ public:
 	}
 
 	iterator insert(const value_type &val) {
-		iterator it = this->lower_bound(val.first);
-		size_t idx = it - this->begin();
-		_items.insert_at(idx, val);
-		return it;
+		return _items.insert(val);
 	}
 
 	/**
@@ -140,7 +135,7 @@ public:
 	}
 
 	bool empty() const {
-		return _items.empty();
+		return _items.isEmpty();
 	}
 
 	/**
@@ -152,7 +147,6 @@ public:
 			if (compareEqual(it->first, theKey))
 				++count_;
 		}
-
 		return count_;
 	}
 
@@ -161,7 +155,7 @@ private:
 		return !_comp(a, b) && !_comp(b, a);
 	}
 
-	Common::Array<value_type> _items;
+	Tree<Key, Val, CompFunc> _items;
 	CompFunc _comp;
 };
 




More information about the Scummvm-git-logs mailing list