Chris@53
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@53
|
2 /*
|
Chris@53
|
3 This file is Copyright (c) 2012 Chris Cannam
|
Chris@53
|
4
|
Chris@53
|
5 Permission is hereby granted, free of charge, to any person
|
Chris@53
|
6 obtaining a copy of this software and associated documentation
|
Chris@53
|
7 files (the "Software"), to deal in the Software without
|
Chris@53
|
8 restriction, including without limitation the rights to use, copy,
|
Chris@53
|
9 modify, merge, publish, distribute, sublicense, and/or sell copies
|
Chris@53
|
10 of the Software, and to permit persons to whom the Software is
|
Chris@53
|
11 furnished to do so, subject to the following conditions:
|
Chris@53
|
12
|
Chris@53
|
13 The above copyright notice and this permission notice shall be
|
Chris@53
|
14 included in all copies or substantial portions of the Software.
|
Chris@53
|
15
|
Chris@53
|
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
Chris@53
|
17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
Chris@53
|
18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
Chris@53
|
19 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
|
Chris@53
|
20 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
Chris@53
|
21 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
Chris@53
|
22 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
Chris@53
|
23 */
|
Chris@53
|
24
|
Chris@53
|
25 #include "AgentFeeder.h"
|
Chris@53
|
26
|
Chris@53
|
27 void AgentFeeder::feed(NoteHypothesis::Estimate e)
|
Chris@53
|
28 {
|
Chris@54
|
29 if (m_haveCurrent) {
|
Chris@54
|
30 if (m_current.accept(e)) {
|
Chris@54
|
31 return;
|
Chris@54
|
32 }
|
Chris@54
|
33 if (m_current.getState() == NoteHypothesis::Expired) {
|
Chris@54
|
34 m_accepted.push_back(m_current);
|
Chris@54
|
35 m_haveCurrent = false;
|
Chris@54
|
36 }
|
Chris@54
|
37 }
|
Chris@53
|
38
|
Chris@54
|
39 bool swallowed = false;
|
Chris@53
|
40
|
Chris@54
|
41 Hypotheses newCandidates;
|
Chris@53
|
42
|
Chris@54
|
43 for (Hypotheses::iterator i = m_candidates.begin();
|
Chris@54
|
44 i != m_candidates.end(); ++i) {
|
Chris@54
|
45
|
Chris@54
|
46 NoteHypothesis h = *i;
|
Chris@54
|
47
|
Chris@54
|
48 if (swallowed) {
|
Chris@54
|
49
|
Chris@54
|
50 // don't offer: each observation can only belong to one
|
Chris@54
|
51 // satisfied hypothesis
|
Chris@54
|
52 newCandidates.push_back(h);
|
Chris@54
|
53
|
Chris@54
|
54 } else {
|
Chris@54
|
55
|
Chris@54
|
56 if (h.accept(e)) {
|
Chris@53
|
57
|
Chris@54
|
58 if (h.getState() == NoteHypothesis::Satisfied) {
|
Chris@54
|
59
|
Chris@54
|
60 swallowed = true;
|
Chris@54
|
61
|
Chris@54
|
62 if (!m_haveCurrent ||
|
Chris@54
|
63 m_current.getState() == NoteHypothesis::Expired ||
|
Chris@54
|
64 m_current.getState() == NoteHypothesis::Rejected) {
|
Chris@54
|
65 m_current = h;
|
Chris@54
|
66 m_haveCurrent = true;
|
Chris@54
|
67 } else {
|
Chris@54
|
68 newCandidates.push_back(h);
|
Chris@54
|
69 }
|
Chris@54
|
70
|
Chris@54
|
71 } else {
|
Chris@54
|
72 newCandidates.push_back(h);
|
Chris@54
|
73 }
|
Chris@54
|
74 }
|
Chris@54
|
75 }
|
Chris@54
|
76 }
|
Chris@54
|
77
|
Chris@54
|
78 if (!swallowed) {
|
Chris@54
|
79 NoteHypothesis h;
|
Chris@54
|
80 if (h.accept(e)) {
|
Chris@54
|
81 newCandidates.push_back(h);
|
Chris@54
|
82 }
|
Chris@54
|
83 }
|
Chris@54
|
84
|
Chris@54
|
85 m_candidates = reap(newCandidates);
|
Chris@53
|
86 }
|
Chris@53
|
87
|
Chris@54
|
88 AgentFeeder::Hypotheses
|
Chris@54
|
89 AgentFeeder::reap(Hypotheses candidates)
|
Chris@54
|
90 {
|
Chris@54
|
91 // reap rejected/expired hypotheses from list of candidates
|
Chris@54
|
92
|
Chris@54
|
93 Hypotheses survived;
|
Chris@54
|
94 for (Hypotheses::const_iterator i = candidates.begin();
|
Chris@54
|
95 i != candidates.end(); ++i) {
|
Chris@54
|
96 NoteHypothesis h = *i;
|
Chris@54
|
97 if (h.getState() != NoteHypothesis::Rejected &&
|
Chris@54
|
98 h.getState() != NoteHypothesis::Expired) {
|
Chris@54
|
99 survived.push_back(h);
|
Chris@54
|
100 }
|
Chris@54
|
101 }
|
Chris@54
|
102
|
Chris@54
|
103 return survived;
|
Chris@54
|
104 }
|
Chris@54
|
105
|
Chris@53
|
106 void
|
Chris@53
|
107 AgentFeeder::finish()
|
Chris@53
|
108 {
|
Chris@53
|
109 if (m_current.getState() == NoteHypothesis::Satisfied) {
|
Chris@53
|
110 m_accepted.push_back(m_current);
|
Chris@53
|
111 }
|
Chris@53
|
112 }
|
Chris@53
|
113
|