From e22984efe9b82ab006494aea93814a592cd44ece Mon Sep 17 00:00:00 2001 From: David Robillard Date: Thu, 25 Feb 2010 03:40:39 +0000 Subject: Store patch connections in a map using a lexicographic key for fast (logarathmic) connection searching. Replaces all O(num_connections) searches with O(lg(num_connections)) searches. git-svn-id: http://svn.drobilla.net/lad/trunk/ingen@2491 a436a847-0d15-0410-975c-d299462d15a1 --- src/client/ClientStore.cpp | 2 +- src/client/PatchModel.cpp | 42 +++++++++++++++++++----------------------- src/client/PatchModel.hpp | 9 ++++++--- 3 files changed, 26 insertions(+), 27 deletions(-) (limited to 'src/client') diff --git a/src/client/ClientStore.cpp b/src/client/ClientStore.cpp index 8a6e008b..2b7bb7f7 100644 --- a/src/client/ClientStore.cpp +++ b/src/client/ClientStore.cpp @@ -478,7 +478,7 @@ ClientStore::disconnect(const Path& src_port_path, const Path& dst_port_path) SharedPtr patch = connection_patch(src_port_path, dst_port_path); if (patch) - patch->remove_connection(src_port_path, dst_port_path); + patch->remove_connection(src_port.get(), dst_port.get()); else LOG(error) << "Disconnection in non-existent patch: " << src_port_path << " -> " << dst_port_path << endl; diff --git a/src/client/PatchModel.cpp b/src/client/PatchModel.cpp index bf4168d6..94557bd0 100644 --- a/src/client/PatchModel.cpp +++ b/src/client/PatchModel.cpp @@ -59,7 +59,7 @@ PatchModel::remove_child(SharedPtr o) Connections::iterator next = j; ++next; - SharedPtr cm = PtrCast(*j); + SharedPtr cm = PtrCast(j->second); assert(cm); if (cm->src_port_path().parent() == o->path() @@ -68,7 +68,6 @@ PatchModel::remove_child(SharedPtr o) || cm->dst_port_path() == o->path()) { signal_removed_connection.emit(cm); _connections->erase(j); // cuts our reference - assert(!get_connection(cm->src_port_path(), cm->dst_port_path())); // no duplicates } j = next; } @@ -98,13 +97,13 @@ PatchModel::clear() SharedPtr -PatchModel::get_connection(const Path& src_port_path, const Path& dst_port_path) const +PatchModel::get_connection(const Shared::Port* src_port, const Shared::Port* dst_port) { - for (Connections::const_iterator i = _connections->begin(); i != _connections->end(); ++i) - if ((*i)->src_port_path() == src_port_path && (*i)->dst_port_path() == dst_port_path) - return PtrCast(*i); - - return SharedPtr(); + Connections::iterator i = _connections->find(make_pair(src_port, dst_port)); + if (i != _connections->end()) + return PtrCast(i->second); + else + return SharedPtr(); } @@ -130,34 +129,31 @@ PatchModel::add_connection(SharedPtr cm) assert(cm->dst_port()->parent().get() == this || cm->dst_port()->parent()->parent().get() == this); - SharedPtr existing = get_connection(cm->src_port_path(), cm->dst_port_path()); + SharedPtr existing = get_connection( + cm->src_port().get(), cm->dst_port().get()); if (existing) { assert(cm->src_port() == existing->src_port()); assert(cm->dst_port() == existing->dst_port()); } else { - _connections->push_back(new Connections::Node(cm)); + _connections->insert(make_pair(make_pair(cm->src_port().get(), cm->dst_port().get()), cm)); signal_new_connection.emit(cm); } } void -PatchModel::remove_connection(const Path& src_port_path, const Path& dst_port_path) +PatchModel::remove_connection(const Shared::Port* src_port, const Shared::Port* dst_port) { - for (Connections::iterator i = _connections->begin(); i != _connections->end(); ++i) { - SharedPtr cm = PtrCast(*i); - assert(cm); - if (cm->src_port_path() == src_port_path && cm->dst_port_path() == dst_port_path) { - signal_removed_connection.emit(cm); - delete _connections->erase(i); // cuts our reference - assert(!get_connection(src_port_path, dst_port_path)); // no duplicates - return; - } + Connections::iterator i = _connections->find(make_pair(src_port, dst_port)); + if (i != _connections->end()) { + SharedPtr c = PtrCast(i->second); + signal_removed_connection.emit(c); + _connections->erase(i); + } else { + warn << "[PatchModel::remove_connection] Failed to find connection " << + src_port->path() << " -> " << dst_port->path() << endl; } - - warn << "[PatchModel::remove_connection] Failed to find connection " << - src_port_path << " -> " << dst_port_path << endl; } diff --git a/src/client/PatchModel.hpp b/src/client/PatchModel.hpp index 1590aa10..e3baa3eb 100644 --- a/src/client/PatchModel.hpp +++ b/src/client/PatchModel.hpp @@ -27,6 +27,9 @@ #include "ConnectionModel.hpp" namespace Ingen { + +namespace Shared { class Port; } + namespace Client { class ClientStore; @@ -43,8 +46,8 @@ public: const Connections& connections() const { return *_connections.get(); } - SharedPtr get_connection(const Raul::Path& src_port_path, - const Raul::Path& dst_port_path) const; + SharedPtr get_connection(const Shared::Port* src_port, + const Shared::Port* dst_port); //uint32_t poly() const { return _poly; } bool enabled() const; @@ -85,7 +88,7 @@ private: bool remove_child(SharedPtr c); void add_connection(SharedPtr cm); - void remove_connection(const Raul::Path& src_port_path, const Raul::Path& dst_port_path); + void remove_connection(const Shared::Port* src_port, const Shared::Port* dst_port); SharedPtr _connections; bool _editable; -- cgit v1.2.1