Преглед изворни кода

BeatSequence::reduceErasingConflicts() implemented

Fabian Peter Hammerle пре 10 година
родитељ
комит
377f02d6f9
3 измењених фајлова са 38 додато и 23 уклоњено
  1. 28 18
      BeatSequence.cpp
  2. 7 5
      BeatSequence.h
  3. 3 0
      tests/beat-sequence-reducation.cpp

+ 28 - 18
BeatSequence.cpp

@@ -16,7 +16,7 @@ BeatSequence::BeatSequence(BeatIndex size)
 void BeatSequence::expand(BeatIndex factor)
 {
     if(factor < 1) {
-        throw "factor may not be less than 1."; 
+        throw "factor may not be less than 1.";
     }
 
     resize(size() * factor);
@@ -24,7 +24,7 @@ void BeatSequence::expand(BeatIndex factor)
     for(signed int targetIndex = (size() - factor); targetIndex > 0; targetIndex -= factor)
     {
         BeatIndex sourceIndex = targetIndex / factor;
-        at(sourceIndex).swap(at(targetIndex));  
+        at(sourceIndex).swap(at(targetIndex));
     }
 }
 
@@ -33,29 +33,29 @@ void BeatSequence::reduce(BeatIndex factor)
     reduce(factor, 0, 0);
 }
 
-void BeatSequence::reduce(BeatIndex factor, ReducationConflictCallback conflictCallback, void* conflictCallbackData)
+void BeatSequence::reduce(BeatIndex factor, ReductionConflictCallback conflictCallback, void* conflictCallbackData)
 {
     if(factor < 1) {
-        throw "factor may not be less than 1."; 
+        throw "factor may not be less than 1.";
     }
 
     if(conflictCallback != 0) {
         for(BeatIndex beatIndex = 0; beatIndex < size(); beatIndex++) {
             if(beatIndex % factor != 0 && at(beatIndex).size() > 0) {
-                conflictCallback(*this, factor, beatIndex, conflictCallbackData); 
+                conflictCallback(*this, factor, beatIndex, conflictCallbackData);
             }
-        } 
+        }
     }
-        
+
     for(BeatIndex beatIndex = 0; beatIndex < size(); beatIndex++) {
         if(beatIndex % factor != 0 && at(beatIndex).size() > 0) {
-            throw new UnresolvedBeatSequenceReducationConflict(beatIndex);
+            throw new UnresolvedBeatSequenceReductionConflict(beatIndex);
         }
-    } 
+    }
 
     BeatIndex targetSize = (size() + factor - 1) / factor;
     for(BeatIndex targetIndex = 0; targetIndex < targetSize; targetIndex++) {
-        BeatIndex sourceIndex = targetIndex * factor;  
+        BeatIndex sourceIndex = targetIndex * factor;
         at(sourceIndex).swap(at(targetIndex));
     }
     resize(targetSize);
@@ -95,13 +95,23 @@ void BeatSequence::print(std::ostream& stream) const
     stream << "\n";
     stream.flush();
 }
-    
+
 void BeatSequence::reduceToNeighbour(BeatIndex factor)
 {
-    reduce(factor, moveReducationConflictToNextNeighbour, 0);
+    reduce(factor, moveReductionConflictToNextNeighbour, 0);
 }
-    
-void BeatSequence::moveReducationConflictToNextNeighbour(BeatSequence& sequence, BeatIndex factor, BeatIndex beatIndex, void* data)
+
+void BeatSequence::reduceErasingConflicts(BeatIndex factor)
+{
+    reduce(factor, eraseReductionConflict, 0);
+}
+
+void BeatSequence::eraseReductionConflict(BeatSequence& sequence, BeatIndex factor, BeatIndex beatIndex, void* data)
+{
+    sequence.at(beatIndex).clear();
+}
+
+void BeatSequence::moveReductionConflictToNextNeighbour(BeatSequence& sequence, BeatIndex factor, BeatIndex beatIndex, void* data)
 {
     BeatSequence::BeatIndex previousIndex = beatIndex / factor * factor;
 
@@ -114,24 +124,24 @@ void BeatSequence::moveReducationConflictToNextNeighbour(BeatSequence& sequence,
             targetIndex = 0;
         }
     }
-    
+
     MessageList& targetBeat = sequence[targetIndex];
     targetBeat.splice(targetBeat.end(), sequence[beatIndex]);
 }
 
-UnresolvedBeatSequenceReducationConflict::UnresolvedBeatSequenceReducationConflict(BeatSequence::BeatIndex beatIndex)
+UnresolvedBeatSequenceReductionConflict::UnresolvedBeatSequenceReductionConflict(BeatSequence::BeatIndex beatIndex)
     : parent("unresolved beat sequence reducation conflict"), beatIndex(beatIndex)
 {
 }
 
-const char* UnresolvedBeatSequenceReducationConflict::what() const throw()
+const char* UnresolvedBeatSequenceReductionConflict::what() const throw()
 {
     std::stringstream info;
     info << parent::what() << " at beat #" << getBeatIndex();
     return info.str().c_str();
 }
 
-BeatSequence::BeatIndex UnresolvedBeatSequenceReducationConflict::getBeatIndex() const
+BeatSequence::BeatIndex UnresolvedBeatSequenceReductionConflict::getBeatIndex() const
 {
     return beatIndex;
 }

+ 7 - 5
BeatSequence.h

@@ -12,27 +12,29 @@ class BeatSequence : public std::vector<MessageList>
 
 public:
     typedef unsigned int BeatIndex;
-    typedef void (*ReducationConflictCallback)(BeatSequence& sequence, BeatIndex factor, BeatIndex beatIndex, void* data); 
+    typedef void (*ReductionConflictCallback)(BeatSequence& sequence, BeatIndex factor, BeatIndex beatIndex, void* data);
 
     BeatSequence();
     BeatSequence(BeatIndex size);
     void expand(BeatIndex factor);
     void reduce(BeatIndex factor);
-    void reduce(BeatIndex factor, ReducationConflictCallback conflictCallback, void* conflictCallbackData);
+    void reduce(BeatIndex factor, ReductionConflictCallback conflictCallback, void* conflictCallbackData);
     void reduceToNeighbour(BeatIndex factor);
+    void reduceErasingConflicts(BeatIndex factor);
     void print(std::ostream& stream) const;
 
 private:
-    static void moveReducationConflictToNextNeighbour(BeatSequence& sequence, BeatIndex factor, BeatIndex beatIndex, void* data); 
+    static void eraseReductionConflict(BeatSequence& sequence, BeatIndex factor, BeatIndex beatIndex, void* data);
+    static void moveReductionConflictToNextNeighbour(BeatSequence& sequence, BeatIndex factor, BeatIndex beatIndex, void* data);
 };
 
-class UnresolvedBeatSequenceReducationConflict : public std::runtime_error
+class UnresolvedBeatSequenceReductionConflict : public std::runtime_error
 {
     typedef std::runtime_error parent;
     BeatSequence::BeatIndex beatIndex;
 
 public:
-    UnresolvedBeatSequenceReducationConflict(BeatSequence::BeatIndex beatIndex);
+    UnresolvedBeatSequenceReductionConflict(BeatSequence::BeatIndex beatIndex);
     virtual const char* what() const throw();
     BeatSequence::BeatIndex getBeatIndex() const;
 };

+ 3 - 0
tests/beat-sequence-reducation.cpp

@@ -16,5 +16,8 @@ int main()
     s.reduceToNeighbour(3);
     s.print(std::cout);
 
+    s.reduceErasingConflicts(2);
+    s.print(std::cout);
+
     return 0;
 }