resman: use set<> instead of vector<>
authorTomi Valkeinen <tomi.valkeinen@ti.com>
Wed, 22 Nov 2017 08:09:30 +0000 (10:09 +0200)
committerTomi Valkeinen <tomi.valkeinen@ti.com>
Wed, 22 Nov 2017 10:47:15 +0000 (12:47 +0200)
kms++util/inc/kms++util/resourcemanager.h
kms++util/src/resourcemanager.cpp

index 002b4c912824bf23301010ad85a00ec7e4d88bf5..1b5cf21ed9e1408798519c01c9c7a3187d2a6f81 100644 (file)
@@ -1,5 +1,5 @@
 #include <kms++/kms++.h>
-#include <vector>
+#include <set>
 #include <string>
 
 namespace kms {
@@ -24,9 +24,9 @@ public:
 
 private:
        Card& m_card;
-       std::vector<Connector*> m_reserved_connectors;
-       std::vector<Crtc*> m_reserved_crtcs;
-       std::vector<Plane*> m_reserved_planes;
+       std::set<Connector*> m_reserved_connectors;
+       std::set<Crtc*> m_reserved_crtcs;
+       std::set<Plane*> m_reserved_planes;
 };
 
 }
index 465d817c6e1c6ca3155cce0fead5e2694ebd1ebb..9a8a66bb06798a13b06197b512811985534a75ac 100644 (file)
@@ -5,13 +5,6 @@
 using namespace kms;
 using namespace std;
 
-template<class C, class T>
-auto contains(const C& v, const T& x)
--> decltype(end(v), true)
-{
-       return end(v) != std::find(begin(v), end(v), x);
-}
-
 ResourceManager::ResourceManager(Card& card)
        : m_card(card)
 {
@@ -24,13 +17,13 @@ void ResourceManager::reset()
        m_reserved_planes.clear();
 }
 
-static Connector* find_connector(Card& card, const vector<Connector*> reserved)
+static Connector* find_connector(Card& card, const set<Connector*> reserved)
 {
        for (Connector* conn : card.get_connectors()) {
                if (!conn->connected())
                        continue;
 
-               if (contains(reserved, conn))
+               if (reserved.count(conn))
                        continue;
 
                return conn;
@@ -39,7 +32,7 @@ static Connector* find_connector(Card& card, const vector<Connector*> reserved)
        return nullptr;
 }
 
-static Connector* resolve_connector(Card& card, const string& name, const vector<Connector*> reserved)
+static Connector* resolve_connector(Card& card, const string& name, const set<Connector*> reserved)
 {
        auto connectors = card.get_connectors();
 
@@ -49,7 +42,7 @@ static Connector* resolve_connector(Card& card, const string& name, const vector
                if (*endptr == 0) {
                        Connector* c = card.get_connector(id);
 
-                       if (!c || contains(reserved, c))
+                       if (!c || reserved.count(c))
                                return nullptr;
 
                        return c;
@@ -63,7 +56,7 @@ static Connector* resolve_connector(Card& card, const string& name, const vector
 
                        Connector* c = connectors[idx];
 
-                       if (contains(reserved, c))
+                       if (reserved.count(c))
                                return nullptr;
 
                        return c;
@@ -74,7 +67,7 @@ static Connector* resolve_connector(Card& card, const string& name, const vector
                if (to_lower(conn->fullname()).find(to_lower(name)) == string::npos)
                        continue;
 
-               if (contains(reserved, conn))
+               if (reserved.count(conn))
                        continue;
 
                return conn;
@@ -95,7 +88,7 @@ Connector* ResourceManager::reserve_connector(const string& name)
        if (!conn)
                return nullptr;
 
-       m_reserved_connectors.push_back(conn);
+       m_reserved_connectors.insert(conn);
        return conn;
 }
 
@@ -104,10 +97,10 @@ Connector* ResourceManager::reserve_connector(Connector* conn)
        if (!conn)
                return nullptr;
 
-       if (contains(m_reserved_connectors, conn))
+       if (m_reserved_connectors.count(conn))
                return nullptr;
 
-       m_reserved_connectors.push_back(conn);
+       m_reserved_connectors.insert(conn);
        return conn;
 }
 
@@ -117,15 +110,15 @@ Crtc* ResourceManager::reserve_crtc(Connector* conn)
                return nullptr;
 
        if (Crtc* crtc = conn->get_current_crtc()) {
-               m_reserved_crtcs.push_back(crtc);
+               m_reserved_crtcs.insert(crtc);
                return crtc;
        }
 
        for (Crtc* crtc : conn->get_possible_crtcs()) {
-               if (contains(m_reserved_crtcs, crtc))
+               if (m_reserved_crtcs.count(crtc))
                        continue;
 
-               m_reserved_crtcs.push_back(crtc);
+               m_reserved_crtcs.insert(crtc);
                return crtc;
        }
 
@@ -137,10 +130,10 @@ Crtc* ResourceManager::reserve_crtc(Crtc* crtc)
        if (!crtc)
                return nullptr;
 
-       if (contains(m_reserved_crtcs, crtc))
+       if (m_reserved_crtcs.count(crtc))
                return nullptr;
 
-       m_reserved_crtcs.push_back(crtc);
+       m_reserved_crtcs.insert(crtc);
 
        return crtc;
 }
@@ -157,10 +150,10 @@ Plane* ResourceManager::reserve_plane(Crtc* crtc, PlaneType type, PixelFormat fo
                if (format != PixelFormat::Undefined && !plane->supports_format(format))
                        continue;
 
-               if (contains(m_reserved_planes, plane))
+               if (m_reserved_planes.count(plane))
                        continue;
 
-               m_reserved_planes.push_back(plane);
+               m_reserved_planes.insert(plane);
                return plane;
        }
 
@@ -172,10 +165,10 @@ Plane* ResourceManager::reserve_plane(Plane* plane)
        if (!plane)
                return nullptr;
 
-       if (contains(m_reserved_planes, plane))
+       if (m_reserved_planes.count(plane))
                return nullptr;
 
-       m_reserved_planes.push_back(plane);
+       m_reserved_planes.insert(plane);
 
        return plane;
 }
@@ -192,10 +185,10 @@ Plane* ResourceManager::reserve_generic_plane(Crtc* crtc, PixelFormat format)
                if (format != PixelFormat::Undefined && !plane->supports_format(format))
                        continue;
 
-               if (contains(m_reserved_planes, plane))
+               if (m_reserved_planes.count(plane))
                        continue;
 
-               m_reserved_planes.push_back(plane);
+               m_reserved_planes.insert(plane);
                return plane;
        }