Chris@53: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@53: /* Chris@53: This file is Copyright (c) 2012 Chris Cannam Chris@53: Chris@53: Permission is hereby granted, free of charge, to any person Chris@53: obtaining a copy of this software and associated documentation Chris@53: files (the "Software"), to deal in the Software without Chris@53: restriction, including without limitation the rights to use, copy, Chris@53: modify, merge, publish, distribute, sublicense, and/or sell copies Chris@53: of the Software, and to permit persons to whom the Software is Chris@53: furnished to do so, subject to the following conditions: Chris@53: Chris@53: The above copyright notice and this permission notice shall be Chris@53: included in all copies or substantial portions of the Software. Chris@53: Chris@53: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, Chris@53: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF Chris@53: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND Chris@53: NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR Chris@53: ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF Chris@53: CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION Chris@53: WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Chris@53: */ Chris@53: Chris@53: #include "AgentFeeder.h" Chris@53: Chris@53: void AgentFeeder::feed(NoteHypothesis::Estimate e) Chris@53: { Chris@54: if (m_haveCurrent) { Chris@54: if (m_current.accept(e)) { Chris@54: return; Chris@54: } Chris@54: if (m_current.getState() == NoteHypothesis::Expired) { Chris@54: m_accepted.push_back(m_current); Chris@54: m_haveCurrent = false; Chris@54: } Chris@54: } Chris@53: Chris@54: bool swallowed = false; Chris@53: Chris@54: Hypotheses newCandidates; Chris@53: Chris@54: for (Hypotheses::iterator i = m_candidates.begin(); Chris@54: i != m_candidates.end(); ++i) { Chris@54: Chris@54: NoteHypothesis h = *i; Chris@54: Chris@54: if (swallowed) { Chris@54: Chris@54: // don't offer: each observation can only belong to one Chris@54: // satisfied hypothesis Chris@54: newCandidates.push_back(h); Chris@54: Chris@54: } else { Chris@54: Chris@54: if (h.accept(e)) { Chris@53: Chris@54: if (h.getState() == NoteHypothesis::Satisfied) { Chris@54: Chris@54: swallowed = true; Chris@54: Chris@54: if (!m_haveCurrent || Chris@54: m_current.getState() == NoteHypothesis::Expired || Chris@54: m_current.getState() == NoteHypothesis::Rejected) { Chris@54: m_current = h; Chris@54: m_haveCurrent = true; Chris@54: } else { Chris@54: newCandidates.push_back(h); Chris@54: } Chris@54: Chris@54: } else { Chris@54: newCandidates.push_back(h); Chris@54: } Chris@54: } Chris@54: } Chris@54: } Chris@54: Chris@54: if (!swallowed) { Chris@54: NoteHypothesis h; Chris@54: if (h.accept(e)) { Chris@54: newCandidates.push_back(h); Chris@54: } Chris@54: } Chris@54: Chris@54: m_candidates = reap(newCandidates); Chris@53: } Chris@53: Chris@54: AgentFeeder::Hypotheses Chris@54: AgentFeeder::reap(Hypotheses candidates) Chris@54: { Chris@54: // reap rejected/expired hypotheses from list of candidates Chris@54: Chris@54: Hypotheses survived; Chris@54: for (Hypotheses::const_iterator i = candidates.begin(); Chris@54: i != candidates.end(); ++i) { Chris@54: NoteHypothesis h = *i; Chris@54: if (h.getState() != NoteHypothesis::Rejected && Chris@54: h.getState() != NoteHypothesis::Expired) { Chris@54: survived.push_back(h); Chris@54: } Chris@54: } Chris@54: Chris@54: return survived; Chris@54: } Chris@54: Chris@53: void Chris@53: AgentFeeder::finish() Chris@53: { Chris@53: if (m_current.getState() == NoteHypothesis::Satisfied) { Chris@53: m_accepted.push_back(m_current); Chris@53: } Chris@53: } Chris@53: