Speed up iterating over an entire PortSet by keeping a
[ardour.git] / libs / ardour / port_set.cc
1 /*
2     Copyright (C) 2006 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify it
5     under the terms of the GNU General Public License as published by the Free
6     Software Foundation; either version 2 of the License, or (at your option)
7     any later version.
8
9     This program is distributed in the hope that it will be useful, but WITHOUT
10     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12     for more details.
13
14     You should have received a copy of the GNU General Public License along
15     with this program; if not, write to the Free Software Foundation, Inc.,
16     675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19 #include <string>
20
21 #include "ardour/port_set.h"
22 #include "ardour/midi_port.h"
23 #include "ardour/audio_port.h"
24
25 using std::string;
26
27 namespace ARDOUR {
28
29 PortSet::PortSet()
30 {
31         for (size_t i=0; i < DataType::num_types; ++i)
32                 _ports.push_back( PortVec() );
33 }
34
35 static bool sort_ports_by_name (boost::shared_ptr<Port> a, boost::shared_ptr<Port> b)
36 {
37         string aname (a->name());
38         string bname (b->name());
39
40         string::size_type last_digit_position_a = aname.size();
41         string::reverse_iterator r_iterator = aname.rbegin();
42
43         while (r_iterator!= aname.rend() && Glib::Unicode::isdigit(*r_iterator)) {
44                 r_iterator++;
45                 last_digit_position_a--;
46         }
47
48         string::size_type last_digit_position_b = bname.size();
49         r_iterator = bname.rbegin();
50
51         while (r_iterator != bname.rend() && Glib::Unicode::isdigit(*r_iterator)) {
52                 r_iterator++;
53                 last_digit_position_b--;
54         }
55
56         // if some of the names don't have a number as posfix, compare as strings
57
58         if (last_digit_position_a == aname.size() or last_digit_position_b == bname.size()) {
59                 return aname < bname;
60         }
61
62         const std::string       prefix_a = aname.substr(0, last_digit_position_a - 1);
63         const unsigned int      posfix_a = std::atoi(aname.substr(last_digit_position_a, aname.size() - last_digit_position_a).c_str());
64         const std::string       prefix_b = bname.substr(0, last_digit_position_b - 1);
65         const unsigned int      posfix_b = std::atoi(bname.substr(last_digit_position_b, bname.size() - last_digit_position_b).c_str());
66
67         if (prefix_a != prefix_b) {
68                 return aname < bname;
69         } else {
70                 return posfix_a < posfix_b;
71         }
72 }
73
74
75 static bool sort_ports_by_type_and_name (boost::shared_ptr<Port> a, boost::shared_ptr<Port> b)
76 {
77         if (a->type() != b->type()) {
78                 return a->type() < b->type();
79         }
80
81         return sort_ports_by_name (a, b);
82 }
83
84 void
85 PortSet::add (boost::shared_ptr<Port> port)
86 {
87         PortVec& v = _ports[port->type()];
88
89         v.push_back(port);
90         _all_ports.push_back(port);
91
92         sort(v.begin(), v.end(), sort_ports_by_name);
93         sort(_all_ports.begin(), _all_ports.end(), sort_ports_by_type_and_name);
94         
95         _count.set(port->type(), _count.get(port->type()) + 1);
96         assert(_count.get(port->type()) == _ports[port->type()].size());
97 }
98
99 bool
100 PortSet::remove (boost::shared_ptr<Port> port)
101 {
102         PortVec::iterator i = find(_all_ports.begin(), _all_ports.end(), port);
103         if (i != _all_ports.end()) {
104                 _all_ports.erase(i);
105         }
106         
107         for (std::vector<PortVec>::iterator l = _ports.begin(); l != _ports.end(); ++l) {
108                 PortVec::iterator i = find(l->begin(), l->end(), port);
109                 if (i != l->end()) {
110                         l->erase(i);
111                         _count.set(port->type(), _count.get(port->type()) - 1);
112                         return true;
113                 }
114         }
115
116         return false;
117 }
118
119 /** Get the total number of ports (of all types) in the PortSet
120  */
121 size_t
122 PortSet::num_ports() const
123 {
124         return _all_ports.size();
125 }
126
127 bool
128 PortSet::contains (boost::shared_ptr<const Port> port) const
129 {
130         return find(_all_ports.begin(), _all_ports.end(), port) != _all_ports.end();
131 }
132
133 boost::shared_ptr<Port>
134 PortSet::port(size_t n) const
135 {
136         assert(n < _all_ports.size());
137         return _all_ports[n];
138 }
139
140 boost::shared_ptr<Port>
141 PortSet::port(DataType type, size_t n) const
142 {
143         if (type == DataType::NIL) {
144                 return port(n);
145         } else {
146                 const PortVec& v = _ports[type];
147                 assert(n < v.size());
148                 return v[n];
149         }
150 }
151
152 boost::shared_ptr<AudioPort>
153 PortSet::nth_audio_port(size_t n) const
154 {
155         return boost::dynamic_pointer_cast<AudioPort> (port (DataType::AUDIO, n));
156 }
157
158 boost::shared_ptr<MidiPort>
159 PortSet::nth_midi_port(size_t n) const
160 {
161         return boost::dynamic_pointer_cast<MidiPort> (port (DataType::MIDI, n));
162 }
163
164 void
165 PortSet::clear()
166 {
167         _ports.clear();
168         _all_ports.clear();
169 }
170
171 } // namepace ARDOUR