From e1a63732f796c8057751cb7f8ac98a595b91692e Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sat, 12 Jan 2013 03:17:47 +0000 Subject: Bulk reformat. git-svn-id: http://svn.drobilla.net/lad/trunk/machina@4929 a436a847-0d15-0410-975c-d299462d15a1 --- src/engine/Machine.cpp | 128 ++++++++++++++++++++++++++++++------------------- 1 file changed, 78 insertions(+), 50 deletions(-) (limited to 'src/engine/Machine.cpp') diff --git a/src/engine/Machine.cpp b/src/engine/Machine.cpp index 8a7f0b4..92a0d7e 100644 --- a/src/engine/Machine.cpp +++ b/src/engine/Machine.cpp @@ -43,8 +43,7 @@ Machine::Machine(TimeUnit unit) , _time(unit, 0, 0) , _is_activated(false) , _is_finished(false) -{ -} +{} /** Copy a Machine. * @@ -60,14 +59,16 @@ Machine::Machine(const Machine& copy) { std::map< SharedPtr, SharedPtr > replacements; - for (Nodes::const_iterator n = copy._nodes.begin(); n != copy._nodes.end(); ++n) { + for (Nodes::const_iterator n = copy._nodes.begin(); n != copy._nodes.end(); + ++n) { SharedPtr node(new Machina::Node(*n->get())); _nodes.push_back(node); replacements[*n] = node; } for (Nodes::const_iterator n = _nodes.begin(); n != _nodes.end(); ++n) { - for (Node::Edges::const_iterator e = (*n)->edges().begin(); e != (*n)->edges().end(); ++e) { + for (Node::Edges::const_iterator e = (*n)->edges().begin(); + e != (*n)->edges().end(); ++e) { (*e)->set_tail(*n); (*e)->set_head(replacements[(*e)->head()]); assert((*e)->head()); @@ -78,23 +79,26 @@ Machine::Machine(const Machine& copy) Machine& Machine::operator=(const Machine& other) { - _active_nodes = std::vector< SharedPtr >(MAX_ACTIVE_NODES, SharedPtr()); - _is_activated = false; - _is_finished = false; - _time = other._time; + _active_nodes = std::vector< SharedPtr >(MAX_ACTIVE_NODES, + SharedPtr()); + _is_activated = false; + _is_finished = false; + _time = other._time; _pending_learn = SharedPtr(); _nodes.clear(); map< SharedPtr, SharedPtr > replacements; - for (Nodes::const_iterator n = other._nodes.begin(); n != other._nodes.end(); ++n) { + for (Nodes::const_iterator n = other._nodes.begin(); n != other._nodes.end(); + ++n) { SharedPtr node(new Machina::Node(*n->get())); _nodes.push_back(node); replacements[*n] = node; } for (Nodes::const_iterator n = _nodes.begin(); n != _nodes.end(); ++n) { - for (Node::Edges::const_iterator e = (*n)->edges().begin(); e != (*n)->edges().end(); ++e) { + for (Node::Edges::const_iterator e = (*n)->edges().begin(); + e != (*n)->edges().end(); ++e) { (*e)->set_tail(*n); (*e)->set_head(replacements[(*e)->head()]); assert((*e)->head()); @@ -108,15 +112,19 @@ Machine::operator=(const Machine& other) SharedPtr Machine::random_node() { - if (_nodes.empty()) + if (_nodes.empty()) { return SharedPtr(); + } size_t i = rand() % _nodes.size(); // FIXME: O(n) worst case :( - for (Nodes::const_iterator n = _nodes.begin(); n != _nodes.end(); ++n, --i) - if (i == 0) + for (Nodes::const_iterator n = _nodes.begin(); n != _nodes.end(); ++n, + --i) { + if (i == 0) { return *n; + } + } return SharedPtr(); } @@ -127,8 +135,9 @@ Machine::random_edge() { SharedPtr tail = random_node(); - for (size_t i = 0; i < _nodes.size() && tail->edges().empty(); ++i) + for (size_t i = 0; i < _nodes.size() && tail->edges().empty(); ++i) { tail = random_node(); + } return tail ? tail->random_edge() : SharedPtr(); } @@ -145,8 +154,9 @@ Machine::remove_node(SharedPtr node) { _nodes.erase(std::find(_nodes.begin(), _nodes.end(), node)); - for (Nodes::const_iterator n = _nodes.begin(); n != _nodes.end(); ++n) + for (Nodes::const_iterator n = _nodes.begin(); n != _nodes.end(); ++n) { (*n)->remove_edge_to(node); + } } /** Exit all active states and reset time to 0. @@ -155,20 +165,23 @@ void Machine::reset(MIDISink* sink, Raul::TimeStamp time) { if (!_is_finished) { - for (Nodes::const_iterator n = _nodes.begin(); n != _nodes.end(); ++n) { + for (Nodes::const_iterator n = _nodes.begin(); n != _nodes.end(); + ++n) { SharedPtr node = (*n); - if (sink && node->is_active()) + if (sink && node->is_active()) { node->exit(sink, time); + } assert(!node->is_active()); } - for (size_t i=0; i < MAX_ACTIVE_NODES; ++i) + for (size_t i = 0; i < MAX_ACTIVE_NODES; ++i) { _active_nodes.at(i).reset(); + } } - _time = TimeStamp(_time.unit(), 0, 0); + _time = TimeStamp(_time.unit(), 0, 0); _is_finished = false; } @@ -179,12 +192,12 @@ Machine::earliest_node() const { SharedPtr earliest; - for (size_t i=0; i < MAX_ACTIVE_NODES; ++i) { + for (size_t i = 0; i < MAX_ACTIVE_NODES; ++i) { SharedPtr node = _active_nodes.at(i); if (node) { assert(node->is_active()); - if (!earliest || node->exit_time() < earliest->exit_time()) { + if (!earliest || ( node->exit_time() < earliest->exit_time()) ) { earliest = node; } } @@ -198,14 +211,16 @@ Machine::earliest_node() const * Returns true if node was entered, or false if the maximum active nodes has been reached. */ bool -Machine::enter_node(Context& context, SharedPtr node, SharedPtr updates) +Machine::enter_node(Context& context, + SharedPtr node, + SharedPtr updates) { assert(!node->is_active()); /* FIXME: Would be best to use the MIDI note here as a hash key, at least * while all actions are still MIDI notes... */ size_t index = (rand() % MAX_ACTIVE_NODES); - for (size_t i=0; i < MAX_ACTIVE_NODES; ++i) { + for (size_t i = 0; i < MAX_ACTIVE_NODES; ++i) { if (_active_nodes.at(index) == NULL) { node->enter(context.sink(), _time); assert(node->is_active()); @@ -227,7 +242,9 @@ Machine::enter_node(Context& context, SharedPtr node, SharedPtr node, SharedPtr updates) +Machine::exit_node(Context& context, + SharedPtr node, + SharedPtr updates) { node->exit(context.sink(), _time); write_set(updates, @@ -237,9 +254,11 @@ Machine::exit_node(Context& context, SharedPtr node, SharedPtris_active()); - for (size_t i=0; i < MAX_ACTIVE_NODES; ++i) - if (_active_nodes.at(i) == node) + for (size_t i = 0; i < MAX_ACTIVE_NODES; ++i) { + if (_active_nodes.at(i) == node) { _active_nodes.at(i).reset(); + } + } // Activate successors to this node // (that aren't aready active right now) @@ -247,14 +266,14 @@ Machine::exit_node(Context& context, SharedPtr node, SharedPtris_selector()) { const double rand_normal = rand() / (double)RAND_MAX; // [0, 1] - double range_min = 0; + double range_min = 0; for (Node::Edges::const_iterator s = node->edges().begin(); - s != node->edges().end(); ++s) { + s != node->edges().end(); ++s) { if (!(*s)->head()->is_active() - && rand_normal > range_min - && rand_normal < range_min + (*s)->probability()) { + && ( rand_normal > range_min) + && ( rand_normal < range_min + (*s)->probability()) ) { enter_node(context, (*s)->head(), updates); break; @@ -267,15 +286,16 @@ Machine::exit_node(Context& context, SharedPtr node, SharedPtredges().begin(); - e != node->edges().end(); ++e) { + e != node->edges().end(); ++e) { const double rand_normal = rand() / (double)RAND_MAX; // [0, 1] if (rand_normal <= (*e)->probability()) { SharedPtr head = (*e)->head(); - if ( ! head->is_active()) + if (!head->is_active()) { enter_node(context, head, updates); + } } } @@ -294,19 +314,23 @@ Machine::exit_node(Context& context, SharedPtr node, SharedPtr updates) { - if (_is_finished) + if (_is_finished) { return 0; + } - const TimeStamp cycle_end_frames = context.time().start_ticks() + context.time().length_ticks(); - const TimeStamp cycle_end = context.time().ticks_to_beats(cycle_end_frames); + const TimeStamp cycle_end_frames = context.time().start_ticks() + + context.time().length_ticks(); + const TimeStamp cycle_end = context.time().ticks_to_beats( + cycle_end_frames); assert(_is_activated); // Initial run, enter all initial states if (_time.is_zero()) { bool entered = false; - if ( ! _nodes.empty()) { - for (Nodes::const_iterator n = _nodes.begin(); n != _nodes.end(); ++n) { + if (!_nodes.empty()) { + for (Nodes::const_iterator n = _nodes.begin(); n != _nodes.end(); + ++n) { if ((*n)->is_active()) { (*n)->exit(context.sink(), _time); write_set(updates, @@ -316,8 +340,9 @@ Machine::run(Context& context, SharedPtr updates) } if ((*n)->is_initial()) { - if (enter_node(context, (*n), updates)) + if (enter_node(context, (*n), updates)) { entered = true; + } } } } @@ -334,13 +359,16 @@ Machine::run(Context& context, SharedPtr updates) if (!earliest) { // No more active states, machine is finished #ifndef NDEBUG - for (Nodes::const_iterator n = _nodes.begin(); n != _nodes.end(); ++n) - assert( ! (*n)->is_active()); + for (Nodes::const_iterator n = _nodes.begin(); n != _nodes.end(); + ++n) { + assert(!(*n)->is_active()); + } #endif _is_finished = true; break; - } else if (context.time().beats_to_ticks(earliest->exit_time()) < cycle_end_frames) { + } else if (context.time().beats_to_ticks(earliest->exit_time()) < + cycle_end_frames) { // Earliest active state ends this cycle _time = earliest->exit_time(); exit_node(context, earliest, updates); @@ -375,8 +403,8 @@ Machine::write_state(Sord::Model& model) model.world().add_prefix("machina", "http://drobilla.net/ns/machina#"); model.add_statement(model.base_uri(), - Sord::Curie(model.world(), "rdf:type"), - Sord::Curie(model.world(), "machina:Machine")); + Sord::Curie(model.world(), "rdf:type"), + Sord::Curie(model.world(), "machina:Machine")); for (Nodes::const_iterator n = _nodes.begin(); n != _nodes.end(); ++n) { @@ -384,25 +412,25 @@ Machine::write_state(Sord::Model& model) if ((*n)->is_initial()) { model.add_statement(model.base_uri(), - Sord::Curie(model.world(), "machina:initialNode"), - (*n)->rdf_id(model.world())); + Sord::Curie(model.world(), "machina:initialNode"), + (*n)->rdf_id(model.world())); } else { model.add_statement(model.base_uri(), - Sord::Curie(model.world(), "machina:node"), - (*n)->rdf_id(model.world())); + Sord::Curie(model.world(), "machina:node"), + (*n)->rdf_id(model.world())); } } for (Nodes::const_iterator n = _nodes.begin(); n != _nodes.end(); ++n) { for (Node::Edges::const_iterator e = (*n)->edges().begin(); - e != (*n)->edges().end(); ++e) { + e != (*n)->edges().end(); ++e) { (*e)->write_state(model); model.add_statement(model.base_uri(), - Sord::Curie(model.world(), "machina:edge"), - (*e)->rdf_id(model.world())); + Sord::Curie(model.world(), "machina:edge"), + (*e)->rdf_id(model.world())); } } -- cgit v1.2.1