make the bookmarks sparse...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2014-01-16 15:43:30 +04:00
parent c945b3413f
commit f5a6fc8c87
2 changed files with 69 additions and 57 deletions

View File

@ -25,11 +25,11 @@ var BOOKMARKS_FILE_PATTERN = /^[0-9]*-bookmarked.json$/
*/
var getBookmarked = makeMarkedLister(function(){ return BOOKMARKS })
var getUnbookmarked = makeUnmarkedLister(function(){ return BOOKMARKS })
var getUnbookmarked = makeUnmarkedSparseLister(function(){ return BOOKMARKS })
var getBookmarkedGIDBefore = makeGIDBeforeGetterFromList(
function(){
return BOOKMARKS
return compactSparceList(BOOKMARKS)
})
@ -83,12 +83,14 @@ var toggleBookmark = makeMarkToggler(
// add a bookmark...
if(action == 'on'){
if(BOOKMARKS.indexOf(gid) == -1){
insertGIDToPosition(gid, BOOKMARKS)
//insertGIDToPosition(gid, BOOKMARKS)
BOOKMARKS[DATA.order.indexOf(gid)] = gid
}
// remove a bookmark...
} else {
BOOKMARKS.splice(BOOKMARKS.indexOf(gid), 1)
//BOOKMARKS.splice(BOOKMARKS.indexOf(gid), 1)
delete BOOKMARKED[BOOKMARKED.indexOf(gid)]
}
bookmarksUpdated()
@ -99,10 +101,10 @@ var toggleBookmark = makeMarkToggler(
//
var nextBookmark = makeNextFromListAction(
getBookmarkedGIDBefore,
function(){ return BOOKMARKS })
function(){ return compactSparceList(BOOKMARKS) })
var prevBookmark = makePrevFromListAction(
getBookmarkedGIDBefore,
function(){ return BOOKMARKS })
function(){ return compactSparceList(BOOKMARKS) })
@ -116,7 +118,7 @@ var loadFileBookmarks = makeFileLoader(
BOOKMARKS_FILE_PATTERN,
[[], {}],
function(data){
BOOKMARKS = data[0]
BOOKMARKS = populateSparceGIDList(data[0])
BOOKMARKS_DATA = data[1]
})
@ -126,7 +128,7 @@ var saveFileBookmarks = makeFileSaver(
BOOKMARKS_FILE_DEFAULT,
function(){
return [
BOOKMARKS,
compactSparceList(BOOKMARKS),
BOOKMARKS_DATA
]
})
@ -159,12 +161,21 @@ function setupBookmarks(viewer){
return viewer
.on('sortedImages', function(){
BOOKMARKS = fastSortGIDsByOrder(BOOKMARKS)
BOOKMARKS = populateSparceGIDList(BOOKMARKS)
bookmarksUpdated()
})
.on('horizontalShiftedImage', function(evt, gid, direction){
if(shiftGIDToOrderInList(gid, direction, BOOKMARKS)){
bookmarksUpdated()
var n = DATA.order.indexOf(gid)
var o = BOOKMARKS.indexOf(gid)
// move the marked gid...
BOOKMARKS.splice(o, 1)
BOOKMARKS.splice(n, 0, gid)
// test if there are any marked images between n and o...
var shift = compactSparceList(BOOKMARKS.slice(Math.min(n, o)+1, Math.max(n, o)))
if(shift.length > 0){
marksUpdated()
}
})
}

View File

@ -78,17 +78,10 @@ function makeMarkedLister(get_marked){
return function(mode){
var marked = get_marked()
mode = mode == null ? 'all' : mode
var gids = mode == 'all' ? getLoadedGIDs(marked)
return mode == 'all' ? getLoadedGIDs(marked)
: mode.constructor.name == 'Array' ? getLoadedGIDs(mode)
: typeof(mode) == typeof(123) ? getRibbonGIDs(mode)
: getRibbonGIDs()
if(mode == 'all'){
return gids
}
return gids.filter(function(e){
return marked.indexOf(e) >= 0
})
: typeof(mode) == typeof(123) ? getRibbonGIDs(marked, mode)
: getRibbonGIDs(marked)
}
}
@ -105,63 +98,71 @@ function makeMarkedLister(get_marked){
// XXX with sparce lists this is trivial: get all the null indexes...
function makeUnmarkedLister(get_marked, get_cache){
return function(mode){
mode = mode == null ? 'all' : mode
var marked = get_marked()
var cache = get_cache != null ? get_cache() : null
mode = mode == null ? 'all' : mode
var gids = mode == 'all' ? getLoadedGIDs()
: mode.constructor.name == 'Array' ? getLoadedGIDs(mode)
: typeof(mode) == typeof(123) ? getRibbonGIDs(mode)
: getRibbonGIDs()
mode = mode == 'ribbon' ? getRibbonIndex() : mode
// get the cached set...
if(cache != null && mode in cache){
return cache[mode]
}
: typeof(mode) == typeof(123) ? getRibbonGIDs(marked, mode)
: getRibbonGIDs(marked)
// calculate the set...
var res = gids.filter(function(e){
// keep only unmarked...
return marked.indexOf(e) < 0
})
if(cache != null && typeof(mode) == typeof(123)){
cache[mode] = res
}
return res
}
}
// NOTE: this is about an order of magnitude faster than the non-sparse
// version...
function makeUnmarkedSparseLister(get_marked, get_cache){
// mode can be:
// - null - default, same as 'all'
// - 'all' - process all loases gids
// - 'ribbon' - process curent ribbon
// - number - ribbon index
// - Array - list of gids
return function(mode){
mode = mode == null ? 'all' : mode
var marked = get_marked()
//var cache = get_cache != null ? get_cache() : null
var res = mode == 'all' ?
DATA.order.slice()
: mode == 'ribbon' ?
populateSparceGIDList(getRibbonGIDs())
: typeof(mode) == typeof(123) ?
populateSparceGIDList(getRibbonGIDs(mode))
: mode
// for ribbon modes, remove non-ribbon marks...
if(mode == 'ribbon'){
marked = getRibbonGIDs(marked)
} else if(typeof(mode) == typeof(123)){
marked = getRibbonGIDs(marked, mode)
mode = 'ribbon'
}
// negate the list...
marked.forEach(function(e, i){
delete res[i]
})
return getLoadedGIDs(compactSparceList(res))
}
}
var getMarked = makeMarkedLister(function(){ return MARKED })
var getUnmarked = makeUnmarkedLister(
var getUnmarked = makeUnmarkedSparseLister(
function(){ return MARKED },
function(){ return _UNMARKED_CACHE })
// these two are about 3 orders of magnitude faster than the above...
// ...which is better I do not yet know...
function getUnmarked_s0(){
var order = DATA.order
var res = []
for(var i=0; i < order.length; i++){
if(MARKED[i] == null){
res.push(order[i])
}
}
//return res
return getLoadedGIDs(res)
}
function getUnmarked_s1(){
var res = DATA.order.slice()
// XXX is it normal that .map() and .forEach() skip undefined values?
MARKED.map(function(e, i){
delete res[i]
})
//return compactSparceList(res)
return getLoadedGIDs(compactSparceList(res))
}
// XXX make this undefined tolerant -- sparse list compatibility...
var getMarkedGIDBefore = makeGIDBeforeGetterFromList(