mirror of
https://github.com/flynx/ImageGrid.git
synced 2025-10-29 10:20:08 +00:00
further optimizing and some cleanup...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
0c1f110f50
commit
a04ac505c5
@ -1516,7 +1516,7 @@ var ChangesActions = actions.Actions({
|
|||||||
}
|
}
|
||||||
changes[section] = (changes[section] || [])
|
changes[section] = (changes[section] || [])
|
||||||
.concat(items)
|
.concat(items)
|
||||||
.uniqueStrings()
|
.unique()
|
||||||
this.changes = changes
|
this.changes = changes
|
||||||
|
|
||||||
// section(s)...
|
// section(s)...
|
||||||
|
|||||||
@ -333,8 +333,6 @@ var DataPrototype = {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// try and avoid the expensive .indexOf(..) as much as possible...
|
|
||||||
//var j = e != order[i] ? order.indexOf(e) : i
|
|
||||||
var j = order_idx[e]
|
var j = order_idx[e]
|
||||||
|
|
||||||
if(j >= 0){
|
if(j >= 0){
|
||||||
@ -360,8 +358,12 @@ var DataPrototype = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// avoid duplicating target items...
|
// avoid duplicating target items...
|
||||||
|
// XXX not yet sure here what is faster, .toKeys(..) or Set(..)
|
||||||
|
//var target_idx = target.toKeys()
|
||||||
|
var target_idx = new Set(target)
|
||||||
rest = rest
|
rest = rest
|
||||||
.filter(function(e){ return target.indexOf(e) < 0 })
|
//.filter(function(e){ return e in target_idx })
|
||||||
|
.filter(function(e){ return target_idx.has(e) })
|
||||||
|
|
||||||
if(rest.length > 0){
|
if(rest.length > 0){
|
||||||
target.length = Math.max(order.length, target.length)
|
target.length = Math.max(order.length, target.length)
|
||||||
@ -2964,7 +2966,7 @@ var DataWithTagsPrototype = {
|
|||||||
// iterate through all the gids (both images and buffer/data)
|
// iterate through all the gids (both images and buffer/data)
|
||||||
for(var gid in Object.keys(images)
|
for(var gid in Object.keys(images)
|
||||||
.concat(Object.keys(buffer))
|
.concat(Object.keys(buffer))
|
||||||
.uniqueStrings()){
|
.unique()){
|
||||||
// no tags / remove...
|
// no tags / remove...
|
||||||
if(buffer[gid] == null || buffer[gid].tags.length == 0){
|
if(buffer[gid] == null || buffer[gid].tags.length == 0){
|
||||||
// the image exists and has tags...
|
// the image exists and has tags...
|
||||||
@ -2994,7 +2996,7 @@ var DataWithTagsPrototype = {
|
|||||||
var l = img.tags.length
|
var l = img.tags.length
|
||||||
img.tags = img.tags
|
img.tags = img.tags
|
||||||
.concat(buffer[gid].tags)
|
.concat(buffer[gid].tags)
|
||||||
.uniqueStrings()
|
.unique()
|
||||||
// we are updated iff length changed...
|
// we are updated iff length changed...
|
||||||
// NOTE: this is true as we are not removing anything
|
// NOTE: this is true as we are not removing anything
|
||||||
// thus the length can only increase if changes are
|
// thus the length can only increase if changes are
|
||||||
|
|||||||
@ -38,8 +38,7 @@ Object.defineProperty(Object.prototype, 'run', {
|
|||||||
//
|
//
|
||||||
// NOTE: this will not compact in-place.
|
// NOTE: this will not compact in-place.
|
||||||
Array.prototype.compact = function(){
|
Array.prototype.compact = function(){
|
||||||
return this.filter(function(){ return true })
|
return this.filter(function(){ return true }) }
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Convert an array to object...
|
// Convert an array to object...
|
||||||
@ -63,8 +62,32 @@ Array.prototype.toKeys = function(normalize){
|
|||||||
: this.reduce(function(r, e, i){
|
: this.reduce(function(r, e, i){
|
||||||
r[e] = i
|
r[e] = i
|
||||||
return r
|
return r
|
||||||
}, {})
|
}, {}) }
|
||||||
}
|
|
||||||
|
|
||||||
|
// Convert an array to a map...
|
||||||
|
//
|
||||||
|
// This is similar to Array.prototype.toKeys(..) but does not restrict
|
||||||
|
// value type to string.
|
||||||
|
//
|
||||||
|
// Format:
|
||||||
|
// Map([
|
||||||
|
// [<item>, <index>],
|
||||||
|
// ...
|
||||||
|
// ])
|
||||||
|
//
|
||||||
|
// NOTE: this will forget repeating items...
|
||||||
|
// NOTE: normalize will slow things down...
|
||||||
|
Array.prototype.toMap = function(normalize){
|
||||||
|
return normalize ?
|
||||||
|
this.reduce(function(m, e, i){
|
||||||
|
m.set(normalize(e), i)
|
||||||
|
return m
|
||||||
|
}, new Map())
|
||||||
|
: this.reduce(function(m, e, i){
|
||||||
|
m.set(e, i)
|
||||||
|
return m
|
||||||
|
}, new Map()) }
|
||||||
|
|
||||||
|
|
||||||
// Return an array with duplicate elements removed...
|
// Return an array with duplicate elements removed...
|
||||||
@ -72,22 +95,9 @@ Array.prototype.toKeys = function(normalize){
|
|||||||
// NOTE: we are not using an Object as an index here as an Array can
|
// NOTE: we are not using an Object as an index here as an Array can
|
||||||
// contain any type of item while Object keys can only be strings...
|
// contain any type of item while Object keys can only be strings...
|
||||||
// NOTE: for an array containing only strings use a much faster .uniqueStrings(..)
|
// NOTE: for an array containing only strings use a much faster .uniqueStrings(..)
|
||||||
|
// NOTE: this may not work on IE...
|
||||||
Array.prototype.unique = function(normalize){
|
Array.prototype.unique = function(normalize){
|
||||||
if(normalize){
|
return new Array(...(new Set(normalize ? normalize(this) : this))) }
|
||||||
var cache = this.map(function(e){ return normalize(e) })
|
|
||||||
return this.filter(function(e, i, a){ return cache.indexOf(cache[i]) == i })
|
|
||||||
|
|
||||||
} else {
|
|
||||||
return this.filter(function(e, i, a){ return a.indexOf(e) == i })
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Special case of .unique(), allot faster on arrays of strings...
|
|
||||||
//
|
|
||||||
// NOTE: this may jield unexpected results for non-string items...
|
|
||||||
Array.prototype.uniqueStrings = function(normalize){
|
|
||||||
return Object.keys(this.toKeys(normalize)) }
|
|
||||||
|
|
||||||
|
|
||||||
// Compare two arrays...
|
// Compare two arrays...
|
||||||
@ -110,6 +120,8 @@ Array.prototype.cmp = function(other){
|
|||||||
// Compare two Arrays as sets...
|
// Compare two Arrays as sets...
|
||||||
//
|
//
|
||||||
// This will ignore order
|
// This will ignore order
|
||||||
|
//
|
||||||
|
// XXX should we use Set(..) here???
|
||||||
Array.prototype.setCmp = function(other){
|
Array.prototype.setCmp = function(other){
|
||||||
return this === other
|
return this === other
|
||||||
|| this
|
|| this
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user