comparison bits/PluginOutputIdMapper.h @ 50:12e3b396544c

Simplify
author Chris Cannam <c.cannam@qmul.ac.uk>
date Fri, 16 Sep 2016 14:15:41 +0100
parents f3f7561233d6
children f4244a2d55ac
comparison
equal deleted inserted replaced
49:f3f7561233d6 50:12e3b396544c
42 42
43 namespace vampipe { 43 namespace vampipe {
44 44
45 class PluginOutputIdMapper 45 class PluginOutputIdMapper
46 { 46 {
47 // NB not threadsafe. Does this matter?
48
49 //!!! simplify. A single vector is almost certainly faster.
50
51 public: 47 public:
52 PluginOutputIdMapper(Vamp::Plugin *p) : m_plugin(p) { } 48 PluginOutputIdMapper(Vamp::Plugin *p) {
53 49 Vamp::Plugin::OutputList outputs = p->getOutputDescriptors();
54 class NotFound : virtual public std::runtime_error { 50 for (const auto &d: outputs) {
55 public: 51 m_ids.push_back(d.identifier);
56 NotFound() : runtime_error("output id or index not found in mapper") { } 52 }
57 }; 53 }
58 54
59 int idToIndex(std::string outputId) const { 55 int idToIndex(std::string outputId) const {
60 if (m_idIndexMap.empty()) populate(); 56 int n = int(m_ids.size());
61 auto i = m_idIndexMap.find(outputId); 57 for (int i = 0; i < n; ++i) {
62 if (i == m_idIndexMap.end()) throw NotFound(); 58 if (outputId == m_ids[i]) {
63 return i->second; 59 return i;
60 }
61 }
64 } 62 }
65 63
66 std::string indexToId(int index) const { 64 std::string indexToId(int index) const {
67 if (m_ids.empty()) populate();
68 if (index < 0 || size_t(index) >= m_ids.size()) throw NotFound();
69 return m_ids[index]; 65 return m_ids[index];
70 } 66 }
71 67
72 private: 68 private:
73 Vamp::Plugin *m_plugin; 69 std::vector<std::string> m_ids;
74 mutable std::vector<std::string> m_ids;
75 mutable std::map<std::string, int> m_idIndexMap;
76
77 void populate() const {
78 Vamp::Plugin::OutputList outputs = m_plugin->getOutputDescriptors();
79 for (const auto &d: outputs) {
80 m_idIndexMap[d.identifier] = m_ids.size();
81 m_ids.push_back(d.identifier);
82 }
83 }
84 }; 70 };
85 71
86 } 72 }
87 73
88 #endif 74 #endif