| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
ve.dm.AnnotationSet = function VeDmAnnotationSet( store, storeHashes ) { |
| 15 |
|
| 16 |
this.store = store; |
| 17 |
this.storeHashes = storeHashes || []; |
| 18 |
|
| 19 |
if ( this.get().indexOf( undefined ) !== -1 ) { |
| 20 |
throw new Error( 'Annotation with hash ' + |
| 21 |
this.storeHashes[ this.get().indexOf( undefined ) ] + |
| 22 |
' not found in store' |
| 23 |
); |
| 24 |
} |
| 25 |
}; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
ve.dm.AnnotationSet.prototype.getStore = function () { |
| 35 |
return this.store; |
| 36 |
}; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
ve.dm.AnnotationSet.prototype.clone = function () { |
| 44 |
return new ve.dm.AnnotationSet( this.getStore(), this.storeHashes.slice() ); |
| 45 |
}; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
ve.dm.AnnotationSet.prototype.getAnnotationsByName = function ( name ) { |
| 54 |
return this.filter( ( annotation ) => annotation.name === name ); |
| 55 |
}; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
ve.dm.AnnotationSet.prototype.getComparableAnnotations = function ( annotation ) { |
| 65 |
return this.filter( ( a ) => ve.compare( |
| 66 |
annotation.getComparableObject(), |
| 67 |
a.getComparableObject() |
| 68 |
) ); |
| 69 |
}; |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
ve.dm.AnnotationSet.prototype.getComparableAnnotationsFromSet = function ( annotations ) { |
| 79 |
return this.filter( ( a ) => annotations.containsComparable( a ) ); |
| 80 |
}; |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
ve.dm.AnnotationSet.prototype.hasAnnotationWithName = function ( name ) { |
| 89 |
return this.containsMatching( ( annotation ) => annotation.name === name ); |
| 90 |
}; |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
ve.dm.AnnotationSet.prototype.get = function ( offset ) { |
| 103 |
if ( offset !== undefined ) { |
| 104 |
return this.getStore().value( this.getHash( offset ) ); |
| 105 |
} else { |
| 106 |
return this.getStore().values( this.getHashes() ); |
| 107 |
} |
| 108 |
}; |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
ve.dm.AnnotationSet.prototype.getHash = function ( offset ) { |
| 117 |
return this.storeHashes[ offset ]; |
| 118 |
}; |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
ve.dm.AnnotationSet.prototype.getHashes = function () { |
| 126 |
return this.storeHashes; |
| 127 |
}; |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
ve.dm.AnnotationSet.prototype.getLength = function () { |
| 135 |
return this.storeHashes.length; |
| 136 |
}; |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
ve.dm.AnnotationSet.prototype.isEmpty = function () { |
| 144 |
return this.getLength() === 0; |
| 145 |
}; |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
ve.dm.AnnotationSet.prototype.contains = function ( annotation ) { |
| 156 |
return this.offsetOf( annotation ) !== -1; |
| 157 |
}; |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
ve.dm.AnnotationSet.prototype.containsHash = function ( storeHash ) { |
| 166 |
return this.getHashes().indexOf( storeHash ) !== -1; |
| 167 |
}; |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
ve.dm.AnnotationSet.prototype.containsAnyOf = function ( set ) { |
| 176 |
const setHashes = set.getHashes(), |
| 177 |
thisHashes = this.getHashes(); |
| 178 |
for ( let i = 0, length = setHashes.length; i < length; i++ ) { |
| 179 |
if ( thisHashes.indexOf( setHashes[ i ] ) !== -1 ) { |
| 180 |
return true; |
| 181 |
} |
| 182 |
} |
| 183 |
return false; |
| 184 |
}; |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
ve.dm.AnnotationSet.prototype.containsAllOf = function ( set ) { |
| 193 |
const setHashes = set.getHashes(), |
| 194 |
thisHashes = this.getHashes(); |
| 195 |
for ( let i = 0, length = setHashes.length; i < length; i++ ) { |
| 196 |
if ( thisHashes.indexOf( setHashes[ i ] ) === -1 ) { |
| 197 |
return false; |
| 198 |
} |
| 199 |
} |
| 200 |
return true; |
| 201 |
}; |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
ve.dm.AnnotationSet.prototype.offsetOf = function ( annotation ) { |
| 210 |
return this.offsetOfHash( this.getStore().hashOfValue( annotation ) ); |
| 211 |
}; |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
ve.dm.AnnotationSet.prototype.offsetOfHash = function ( storeHash ) { |
| 220 |
return this.getHashes().indexOf( storeHash ); |
| 221 |
}; |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
ve.dm.AnnotationSet.prototype.filter = function ( callback, returnBool ) { |
| 233 |
let result; |
| 234 |
|
| 235 |
if ( !returnBool ) { |
| 236 |
result = this.clone(); |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
result.removeAll(); |
| 241 |
} |
| 242 |
for ( let i = 0, length = this.getLength(); i < length; i++ ) { |
| 243 |
const storeHash = this.getHash( i ); |
| 244 |
const annotation = this.getStore().value( storeHash ); |
| 245 |
if ( callback( annotation ) ) { |
| 246 |
if ( returnBool ) { |
| 247 |
return true; |
| 248 |
} else { |
| 249 |
result.storeHashes.push( storeHash ); |
| 250 |
} |
| 251 |
} |
| 252 |
} |
| 253 |
return returnBool ? false : result; |
| 254 |
}; |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
ve.dm.AnnotationSet.prototype.containsComparable = function ( annotation ) { |
| 266 |
return this.filter( ( a ) => annotation.compareTo( a ), true ); |
| 267 |
}; |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
ve.dm.AnnotationSet.prototype.getComparable = function ( annotation ) { |
| 276 |
for ( let i = 0, len = this.getLength(); i < len; i++ ) { |
| 277 |
const ann = this.getStore().value( this.getHash( i ) ); |
| 278 |
if ( ann.compareTo( annotation ) ) { |
| 279 |
return ann; |
| 280 |
} |
| 281 |
} |
| 282 |
return null; |
| 283 |
}; |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
ve.dm.AnnotationSet.prototype.containsComparableForSerialization = function ( annotation ) { |
| 296 |
return this.filter( ( a ) => annotation.compareToForSerialization( a ), true ); |
| 297 |
}; |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
ve.dm.AnnotationSet.prototype.containsMatching = function ( callback ) { |
| 310 |
return this.filter( callback, true ); |
| 311 |
}; |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
ve.dm.AnnotationSet.prototype.compareTo = function ( annotationSet ) { |
| 322 |
const length = this.getHashes().length; |
| 323 |
|
| 324 |
if ( length === annotationSet.getLength() ) { |
| 325 |
for ( let i = 0; i < length; i++ ) { |
| 326 |
if ( !annotationSet.containsComparable( this.get( i ) ) ) { |
| 327 |
return false; |
| 328 |
} |
| 329 |
} |
| 330 |
} else { |
| 331 |
return false; |
| 332 |
} |
| 333 |
return true; |
| 334 |
}; |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
ve.dm.AnnotationSet.prototype.withoutComparableSet = function ( set ) { |
| 345 |
return this.filter( ( annotation ) => !set.containsComparable( annotation ) ); |
| 346 |
}; |
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
ve.dm.AnnotationSet.prototype.equalsInOrder = function ( set ) { |
| 359 |
const ourHashes = this.getHashes(), |
| 360 |
theirHashes = set.getHashes(); |
| 361 |
if ( ourHashes.length !== theirHashes.length ) { |
| 362 |
return false; |
| 363 |
} |
| 364 |
for ( let i = 0, len = ourHashes.length; i < len; i++ ) { |
| 365 |
if ( ourHashes[ i ] !== theirHashes[ i ] ) { |
| 366 |
return false; |
| 367 |
} |
| 368 |
} |
| 369 |
return true; |
| 370 |
}; |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
ve.dm.AnnotationSet.prototype.add = function ( annotation, offset ) { |
| 385 |
const storeHash = this.getStore().hash( annotation ); |
| 386 |
|
| 387 |
if ( offset < 0 ) { |
| 388 |
offset = this.getLength() + offset; |
| 389 |
} |
| 390 |
|
| 391 |
if ( offset >= this.getLength() ) { |
| 392 |
this.push( annotation ); |
| 393 |
return; |
| 394 |
} |
| 395 |
|
| 396 |
if ( !this.containsHash( storeHash ) ) { |
| 397 |
this.storeHashes.splice( offset, 0, storeHash ); |
| 398 |
} |
| 399 |
}; |
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
ve.dm.AnnotationSet.prototype.addSet = function ( set, offset ) { |
| 410 |
const hashes = this.getHashes(); |
| 411 |
if ( offset === undefined ) { |
| 412 |
offset = hashes.length; |
| 413 |
} |
| 414 |
this.storeHashes = OO.simpleArrayUnion( |
| 415 |
hashes.slice( 0, offset ), |
| 416 |
set.getHashes(), |
| 417 |
hashes.slice( offset ) |
| 418 |
); |
| 419 |
}; |
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
ve.dm.AnnotationSet.prototype.push = function ( annotation ) { |
| 427 |
this.pushHash( this.getStore().hash( annotation ) ); |
| 428 |
}; |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
ve.dm.AnnotationSet.prototype.pushHash = function ( storeHash ) { |
| 436 |
this.storeHashes.push( storeHash ); |
| 437 |
}; |
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
ve.dm.AnnotationSet.prototype.removeAt = function ( offset ) { |
| 446 |
if ( offset < 0 ) { |
| 447 |
offset = this.getLength() + offset; |
| 448 |
} |
| 449 |
if ( offset >= this.getLength() ) { |
| 450 |
throw new Error( 'Offset out of bounds' ); |
| 451 |
} |
| 452 |
this.storeHashes.splice( offset, 1 ); |
| 453 |
}; |
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
ve.dm.AnnotationSet.prototype.removeHash = function ( storeHash ) { |
| 463 |
const offset = this.offsetOfHash( storeHash ); |
| 464 |
if ( offset !== -1 ) { |
| 465 |
this.storeHashes.splice( offset, 1 ); |
| 466 |
} |
| 467 |
}; |
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
ve.dm.AnnotationSet.prototype.remove = function ( annotation ) { |
| 477 |
const offset = this.offsetOf( annotation ); |
| 478 |
if ( offset !== -1 ) { |
| 479 |
this.storeHashes.splice( offset, 1 ); |
| 480 |
} |
| 481 |
}; |
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
ve.dm.AnnotationSet.prototype.removeAll = function () { |
| 487 |
this.storeHashes = []; |
| 488 |
}; |
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
ve.dm.AnnotationSet.prototype.removeSet = function ( set ) { |
| 498 |
this.storeHashes = OO.simpleArrayDifference( this.getHashes(), set.getHashes() ); |
| 499 |
}; |
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
ve.dm.AnnotationSet.prototype.removeNotInSet = function ( set ) { |
| 507 |
this.storeHashes = OO.simpleArrayIntersection( this.getHashes(), set.getHashes() ); |
| 508 |
}; |
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
ve.dm.AnnotationSet.prototype.reversed = function () { |
| 518 |
const newSet = this.clone(); |
| 519 |
newSet.storeHashes.reverse(); |
| 520 |
return newSet; |
| 521 |
}; |
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
ve.dm.AnnotationSet.prototype.mergeWith = function ( set ) { |
| 532 |
const newSet = this.clone(); |
| 533 |
newSet.addSet( set ); |
| 534 |
return newSet; |
| 535 |
}; |
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
ve.dm.AnnotationSet.prototype.diffWith = function ( set ) { |
| 544 |
const newSet = this.clone(); |
| 545 |
newSet.removeSet( set ); |
| 546 |
return newSet; |
| 547 |
}; |
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
ve.dm.AnnotationSet.prototype.intersectWith = function ( set ) { |
| 559 |
const newSet = this.clone(); |
| 560 |
newSet.removeNotInSet( set ); |
| 561 |
return newSet; |
| 562 |
}; |