add chan-mapping count/size()
[ardour.git] / libs / ardour / chan_mapping.cc
1 /*
2     Copyright (C) 2009 Paul Davis
3     Author: David Robillard
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19     $Id: insert.cc 712 2006-07-28 01:08:57Z drobilla $
20 */
21
22 #include <stdint.h>
23 #include <iostream>
24 #include "ardour/chan_mapping.h"
25
26 using namespace std;
27
28 namespace ARDOUR {
29
30 ChanMapping::ChanMapping(ChanCount identity)
31 {
32         if (identity == ChanCount::INFINITE) {
33                 return;
34         }
35
36         for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
37                 for (size_t i = 0; i < identity.get(*t); ++i) {
38                         set(*t, i, i);
39                 }
40         }
41 }
42
43 ChanMapping::ChanMapping (const ChanMapping& other )
44 {
45         const ChanMapping::Mappings& mp (other.mappings());
46         for (Mappings::const_iterator tm = mp.begin(); tm != mp.end(); ++tm) {
47                 for (TypeMapping::const_iterator i = tm->second.begin(); i != tm->second.end(); ++i) {
48                         set (tm->first, i->first, i->second);
49                 }
50         }
51 }
52
53 uint32_t
54 ChanMapping::get(DataType t, uint32_t from, bool* valid) const
55 {
56         Mappings::const_iterator tm = _mappings.find(t);
57         if (tm == _mappings.end()) {
58                 if (valid) { *valid = false; }
59                 return -1;
60         }
61         TypeMapping::const_iterator m = tm->second.find(from);
62         if (m == tm->second.end()) {
63                 if (valid) { *valid = false; }
64                 return -1;
65         }
66         if (valid) { *valid = true; }
67         return m->second;
68 }
69
70 uint32_t
71 ChanMapping::get_src(DataType t, uint32_t to, bool* valid) const
72 {
73         Mappings::const_iterator tm = _mappings.find(t);
74         if (tm == _mappings.end()) {
75                 if (valid) { *valid = false; }
76                 return -1;
77         }
78         for (TypeMapping::const_iterator i = tm->second.begin(); i != tm->second.end(); ++i) {
79                 if (i->second == to) {
80                         if (valid) { *valid = true; }
81                         return i->first;
82                 }
83         }
84         if (valid) { *valid = false; }
85         return -1;
86 }
87
88
89
90 void
91 ChanMapping::set(DataType t, uint32_t from, uint32_t to)
92 {
93         assert(t != DataType::NIL);
94         Mappings::iterator tm = _mappings.find (t);
95         if (tm == _mappings.end()) {
96                 tm = _mappings.insert(std::make_pair(t, TypeMapping())).first;
97         }
98         tm->second.insert(std::make_pair(from, to));
99 }
100
101 void
102 ChanMapping::unset(DataType t, uint32_t from)
103 {
104         assert(t != DataType::NIL);
105         Mappings::iterator tm = _mappings.find (t);
106         if (tm == _mappings.end()) {
107                 return;
108         }
109         tm->second.erase(from);
110 }
111
112 /** Offset the 'from' field of every mapping for type @a t by @a delta */
113 void
114 ChanMapping::offset_from(DataType t, int32_t delta)
115 {
116         Mappings::iterator tm = _mappings.find(t);
117         if (tm != _mappings.end ()) {
118                 TypeMapping new_map;
119                 for (TypeMapping::iterator m = tm->second.begin(); m != tm->second.end(); ++m) {
120                         new_map.insert (make_pair (m->first + delta, m->second));
121                 }
122                 tm->second = new_map;
123         }
124 }
125
126 /** Offset the 'to' field of every mapping for type @a t by @a delta */
127 void
128 ChanMapping::offset_to(DataType t, int32_t delta)
129 {
130         Mappings::iterator tm = _mappings.find(t);
131         if (tm != _mappings.end()) {
132                 for (TypeMapping::iterator m = tm->second.begin(); m != tm->second.end(); ++m) {
133                         m->second += delta;
134                 }
135         }
136 }
137
138 bool
139 ChanMapping::is_subset (const ChanMapping& superset) const
140 {
141         const Mappings& mp (mappings());
142         for (Mappings::const_iterator tm = mp.begin(); tm != mp.end(); ++tm) {
143                 for (TypeMapping::const_iterator i = tm->second.begin(); i != tm->second.end(); ++i) {
144                         bool valid;
145                         if (i->second != superset.get (tm->first, i->first, &valid)) {
146                                 return false;
147                         }
148                         if (!valid) {
149                                 return false;
150                         }
151                 }
152         }
153         return true;
154 }
155
156 bool
157 ChanMapping::is_monotonic () const
158 {
159         const Mappings& mp (mappings());
160         for (Mappings::const_iterator tm = mp.begin(); tm != mp.end(); ++tm) {
161                 uint32_t prev = UINT32_MAX;
162                 for (TypeMapping::const_iterator i = tm->second.begin(); i != tm->second.end(); ++i) {
163                         // set keys are strictly weak ordered
164                         if (i->first < i->second || i->second == prev) {
165                                 return false;
166                         }
167                         prev = i->second;
168                 }
169         }
170         return true;
171 }
172
173 bool
174 ChanMapping::is_identity (ChanCount offset) const
175 {
176         const Mappings& mp (mappings());
177         for (Mappings::const_iterator tm = mp.begin(); tm != mp.end(); ++tm) {
178                 for (TypeMapping::const_iterator i = tm->second.begin(); i != tm->second.end(); ++i) {
179                         if (i->first + offset.get (tm->first) != i->second) {
180                                 return false;
181                         }
182                 }
183         }
184         return true;
185 }
186
187 uint32_t
188 ChanMapping::count () const
189 {
190         uint32_t rv = 0;
191         const Mappings& mp (mappings());
192         for (Mappings::const_iterator tm = mp.begin(); tm != mp.end(); ++tm) {
193                 rv += tm->second.size ();
194         }
195         return rv;
196 }
197
198 } // namespace ARDOUR
199
200 std::ostream& operator<<(std::ostream& o, const ARDOUR::ChanMapping& cm)
201 {
202         const ARDOUR::ChanMapping::Mappings& mp (cm.mappings());
203         for (ARDOUR::ChanMapping::Mappings::const_iterator tm = mp.begin(); tm != mp.end(); ++tm) {
204                 o << tm->first.to_string() << endl;
205                 for (ARDOUR::ChanMapping::TypeMapping::const_iterator i = tm->second.begin();
206                                 i != tm->second.end(); ++i) {
207                         o << "\t" << i->first << " => " << i->second << endl;
208                 }
209         }
210
211         return o;
212 }