mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/VisualEditor
synced 2024-11-25 14:56:20 +00:00
fdf30b1ac8
Created an IndexValueStore class which can store any object and return an integer index to its hash map. Linear data is now stored in ve.dm.LinearData instances. Two subclasses for element and meta data contain methods specific to those data types (ElementLinearData and MetaLinearData). The static methods in ve.dm.Document that inspected data at a given offset are now instance methods of ve.dm.ElementLinearData. AnnotationSets (which are no longer OrderedHashSets) have been moved to /dm and also have to be instantiated with a pointer the store. Bug: 46320 Change-Id: I249a5d48726093d1cb3e36351893f4bff85f52e2
103 lines
2.5 KiB
JavaScript
103 lines
2.5 KiB
JavaScript
/*!
|
|
* VisualEditor IndexValueStore class.
|
|
*
|
|
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
*/
|
|
|
|
/**
|
|
* Index-value store
|
|
*
|
|
* @class
|
|
* @constructor
|
|
*/
|
|
ve.dm.IndexValueStore = function VeDmIndexValueStore() {
|
|
// maps hashes to indexes
|
|
this.hashStore = {};
|
|
// maps indexes to values
|
|
this.valueStore = [];
|
|
};
|
|
|
|
/* Methods */
|
|
|
|
/**
|
|
* Get the index of a value in the store.
|
|
*
|
|
* If the hash is not found the value is added to the store.
|
|
*
|
|
* @method
|
|
* @param {Object|String} value Value to lookup or store
|
|
* @param {String} [hash] Value hash. Uses ve.getHash( value ) if not provided.
|
|
* @returns {number} The index of the value in the store
|
|
*/
|
|
ve.dm.IndexValueStore.prototype.index = function ( value, hash ) {
|
|
var index;
|
|
if ( typeof hash !== 'string' ) {
|
|
hash = ve.getHash( value );
|
|
}
|
|
index = this.indexOfHash( hash );
|
|
if ( index === null ) {
|
|
index = this.valueStore.push( typeof value === 'object' ? ve.cloneObject( value ) : value ) - 1;
|
|
this.hashStore[hash] = index;
|
|
}
|
|
return index;
|
|
};
|
|
|
|
/**
|
|
* Get the index of a hash in the store.
|
|
*
|
|
* Returns null if the hash is not found.
|
|
*
|
|
* @method
|
|
* @param {String} hash Value hash.
|
|
* @returns {number|null} The index of the value in the store, or undefined if it is not found
|
|
*/
|
|
ve.dm.IndexValueStore.prototype.indexOfHash = function ( hash ) {
|
|
return hash in this.hashStore ? this.hashStore[hash] : null;
|
|
};
|
|
|
|
/**
|
|
* Get the indexes of values in the store
|
|
*
|
|
* Sames as index but with arrays.
|
|
*
|
|
* @method
|
|
* @param {Object[]} values Values to lookup or store
|
|
* @returns {Array} The indexes of the values in the store
|
|
*/
|
|
ve.dm.IndexValueStore.prototype.indexes = function ( values ) {
|
|
var i, length, indexes = [];
|
|
for ( i = 0, length = values.length; i < length; i++ ) {
|
|
indexes.push( this.index( values[i] ) );
|
|
}
|
|
return indexes;
|
|
};
|
|
|
|
/**
|
|
* Get the value at a particular index
|
|
*
|
|
* @method
|
|
* @param {number} index Index to lookup
|
|
* @returns {Object|undefined} Value at this index, or undefined if out of bounds
|
|
*/
|
|
ve.dm.IndexValueStore.prototype.value = function ( index ) {
|
|
return this.valueStore[index];
|
|
};
|
|
|
|
/**
|
|
* Get the values at a set of indexes
|
|
*
|
|
* Same as value but with arrays.
|
|
*
|
|
* @method
|
|
* @param {number[]} index Index to lookup
|
|
* @returns {Array} Values at these indexes, or undefined if out of bounds
|
|
*/
|
|
ve.dm.IndexValueStore.prototype.values = function ( indexes ) {
|
|
var i, length, values = [];
|
|
for ( i = 0, length = indexes.length; i < length; i++ ) {
|
|
values.push( this.value( indexes[i] ) );
|
|
}
|
|
return values;
|
|
};
|