1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
|
/*
* EditedZone.cs
* Copyright © 2010-2011 kbinani
*
* This file is part of org.kbinani.cadencii.
*
* org.kbinani.cadencii is free software; you can redistribute it and/or
* modify it under the terms of the GPLv3 License.
*
* org.kbinani.cadencii is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
#if JAVA
package org.kbinani.cadencii;
import java.util.*;
#else
using System;
using org.kbinani.java.util;
namespace org.kbinani.cadencii {
using boolean = System.Boolean;
#endif
#if JAVA
public class EditedZone implements Cloneable {
#else
public class EditedZone : ICloneable {
#endif
private Vector<EditedZoneUnit> mSeries = new Vector<EditedZoneUnit>();
public EditedZone(){
}
public int size() {
return mSeries.size();
}
public Iterator<EditedZoneUnit> iterator() {
return mSeries.iterator();
}
public Object clone() {
EditedZone ret = new EditedZone();
int count = mSeries.size();
for ( int i = 0; i < count; i++ ) {
EditedZoneUnit p = mSeries.get( i );
ret.mSeries.add( (EditedZoneUnit)p.clone() );
}
return ret;
}
public EditedZoneCommand add( int start, int end ) {
EditedZoneCommand com = generateCommandAdd( start, end );
return executeCommand( com );
}
public EditedZoneCommand add( EditedZoneUnit[] items ) {
EditedZoneCommand com = generateCommandAdd( items );
return executeCommand( com );
}
public EditedZoneCommand executeCommand( EditedZoneCommand run ) {
for ( Iterator<EditedZoneUnit> itr = run.mRemove.iterator(); itr.hasNext(); ) {
EditedZoneUnit item = itr.next();
int count = mSeries.size();
for ( int i = 0; i < count; i++ ) {
EditedZoneUnit item2 = mSeries.get( i );
if ( item.mStart == item2.mStart && item.mEnd == item2.mEnd ) {
mSeries.removeElementAt( i );
break;
}
}
}
for ( Iterator<EditedZoneUnit> itr = run.mAdd.iterator(); itr.hasNext(); ) {
EditedZoneUnit item = itr.next();
mSeries.add( (EditedZoneUnit)item.clone() );
}
Collections.sort( mSeries );
return new EditedZoneCommand( run.mRemove, run.mAdd );
}
private EditedZoneCommand generateCommandClear() {
Vector<EditedZoneUnit> remove = new Vector<EditedZoneUnit>();
for ( Iterator<EditedZoneUnit> itr = mSeries.iterator(); itr.hasNext(); ) {
EditedZoneUnit item = itr.next();
remove.add( (EditedZoneUnit)item.clone() );
}
return new EditedZoneCommand( new Vector<EditedZoneUnit>(), remove );
}
private EditedZoneCommand generateCommandAdd( EditedZoneUnit[] areas ) {
EditedZone work = (EditedZone)clone();
for ( int i = 0; i < areas.Length; i++ ) {
EditedZoneUnit item = areas[i];
if ( item == null ) {
continue;
}
work.mSeries.add( new EditedZoneUnit( item.mStart, item.mEnd ) );
}
work.normalize();
// thisに存在していて、workに存在しないものをremoveに登録
Vector<EditedZoneUnit> remove = new Vector<EditedZoneUnit>();
for ( Iterator<EditedZoneUnit> itrThis = iterator(); itrThis.hasNext(); ) {
boolean found = false;
EditedZoneUnit itemThis = itrThis.next();
for ( Iterator<EditedZoneUnit> itrWork = work.iterator(); itrWork.hasNext(); ) {
EditedZoneUnit itemWork = itrWork.next();
if ( itemThis.mStart == itemWork.mStart && itemThis.mEnd == itemWork.mEnd ) {
found = true;
break;
}
}
if ( !found ) {
remove.add( new EditedZoneUnit( itemThis.mStart, itemThis.mEnd ) );
}
}
// workに存在していて、thisに存在しないものをaddに登録
Vector<EditedZoneUnit> add = new Vector<EditedZoneUnit>();
for ( Iterator<EditedZoneUnit> itrWork = work.iterator(); itrWork.hasNext(); ) {
boolean found = false;
EditedZoneUnit itemWork = itrWork.next();
for ( Iterator<EditedZoneUnit> itrThis = iterator(); itrThis.hasNext(); ) {
EditedZoneUnit itemThis = itrThis.next();
if ( itemThis.mStart == itemWork.mStart && itemThis.mEnd == itemWork.mEnd ) {
found = true;
break;
}
}
if ( !found ) {
add.add( new EditedZoneUnit( itemWork.mStart, itemWork.mEnd ) );
}
}
work = null;
return new EditedZoneCommand( add, remove );
}
private EditedZoneCommand generateCommandAdd( int start, int end ) {
return generateCommandAdd( new EditedZoneUnit[] { new EditedZoneUnit( start, end ) } );
}
/// <summary>
/// 重複している部分を統合する
/// </summary>
private void normalize() {
boolean changed = true;
while ( changed ) {
changed = false;
int count = mSeries.size();
for ( int i = 0; i < count - 1; i++ ) {
EditedZoneUnit itemi = mSeries.get( i );
if ( itemi.mEnd < itemi.mStart ) {
int d = itemi.mStart;
itemi.mStart = itemi.mEnd;
itemi.mEnd = d;
}
for ( int j = i + 1; j < count; j++ ) {
EditedZoneUnit itemj = mSeries.get( j );
if ( itemj.mEnd < itemj.mStart ) {
int d = itemj.mStart;
itemj.mStart = itemj.mEnd;
itemj.mEnd = d;
}
if ( itemj.mStart == itemi.mStart && itemj.mEnd == itemi.mEnd ) {
mSeries.removeElementAt( j );
changed = true;
break;
} else if ( itemj.mStart <= itemi.mStart && itemi.mEnd <= itemj.mEnd ) {
mSeries.removeElementAt( i );
changed = true;
break;
} else if ( itemi.mStart <= itemj.mStart && itemj.mEnd <= itemi.mEnd ) {
mSeries.removeElementAt( j );
changed = true;
break;
} else if ( itemi.mStart <= itemj.mEnd && itemj.mEnd < itemi.mEnd ) {
itemj.mEnd = itemi.mEnd;
mSeries.removeElementAt( i );
changed = true;
break;
} else if ( itemi.mStart <= itemj.mStart && itemj.mStart <= itemi.mEnd ) {
itemi.mEnd = itemj.mEnd;
mSeries.removeElementAt( j );
changed = true;
break;
}
}
if ( changed ) {
break;
}
}
}
Collections.sort( mSeries );
}
#if !JAVA
public Object Clone(){
return clone();
}
#endif
}
#if !JAVA
}
#endif
|