@@ -254,8 +254,6 @@ func (o PositionBefore) Move(entries []Movable, existing []Movable) ([]MoveActio
254
254
return nil , err
255
255
}
256
256
257
- slog .Debug ("PositionBefore.Move()" , "existing" , existing , "expected" , expected , "entries" , entries )
258
-
259
257
actions , err := GenerateMovements (existing , expected , entries , ActionWhereBefore , o .Pivot , o .Directly )
260
258
if err != nil {
261
259
return nil , err
@@ -292,23 +290,11 @@ func updateSimulatedIdxMap(idxMap *map[Movable]int, moved Movable, startingIdx i
292
290
func OptimizeMovements (existing []Movable , expected []Movable , entries []Movable , actions []MoveAction , position Position ) []MoveAction {
293
291
simulated := make ([]Movable , len (existing ))
294
292
copy (simulated , existing )
295
-
296
293
simulatedIdxMap := createIdxMapFor (simulated )
297
- expectedIdxMap := createIdxMapFor (expected )
298
294
299
295
var optimized []MoveAction
300
-
301
- switch position .(type ) {
302
- case PositionBefore , PositionAfter :
303
- default :
304
- return actions
305
- }
306
-
307
296
for _ , action := range actions {
308
297
currentIdx := simulatedIdxMap [action .Movable ]
309
- if currentIdx == expectedIdxMap [action .Movable ] {
310
- continue
311
- }
312
298
313
299
var targetIdx int
314
300
switch action .Where {
@@ -317,11 +303,13 @@ func OptimizeMovements(existing []Movable, expected []Movable, entries []Movable
317
303
case ActionWhereBottom :
318
304
targetIdx = len (simulated ) - 1
319
305
case ActionWhereBefore :
320
- targetIdx = simulatedIdxMap [action .Destination ] - 1
306
+ targetIdx = simulatedIdxMap [action .Destination ]
321
307
case ActionWhereAfter :
322
308
targetIdx = simulatedIdxMap [action .Destination ] + 1
323
309
}
324
310
311
+ slog .Debug ("OptimizeMovements()" , "action" , action , "currentIdx" , currentIdx , "targetIdx" , targetIdx )
312
+
325
313
if targetIdx != currentIdx {
326
314
optimized = append (optimized , action )
327
315
simulatedIdxMap [action .Movable ] = targetIdx
@@ -346,11 +334,7 @@ func GenerateMovements(existing []Movable, expected []Movable, entries []Movable
346
334
var movements []MoveAction
347
335
var previous Movable
348
336
for _ , elt := range entries {
349
- slog .Debug ("GenerateMovements()" , "elt" , elt , "existing" , existingIdxMap [elt ], "expected" , expectedIdxMap [elt ], "len(expected)" , len (expected ))
350
- // If existing index for the element matches the expected one, skip it over
351
- if existingIdxMap [elt ] == expectedIdxMap [elt ] {
352
- continue
353
- }
337
+ slog .Debug ("GeneraveMovements()" , "elt" , elt , "existing" , existingIdxMap [elt ], "expected" , expectedIdxMap [elt ])
354
338
355
339
if previous != nil {
356
340
movements = append (movements , MoveAction {
@@ -392,10 +376,10 @@ func GenerateMovements(existing []Movable, expected []Movable, entries []Movable
392
376
eltExpectedIdx := expectedIdxMap [elt ]
393
377
pivot = expected [eltExpectedIdx + 1 ]
394
378
where = ActionWhereBefore
395
- // if previous was nil (we are processing the first element in entries set)
396
- // and selected pivot is part of the entries set it means the order of entries
397
- // changes between existing and expected sets. If direct move has been requested,
398
- // we need to find the correct pivot point for the move .
379
+ // If previous was nil (we are processing the first element in entries set)
380
+ // and selected pivot is part of the entries set, it means the order of elements
381
+ // changes between existing adn expected sets. In this case the actual pivot
382
+ // is element from expected set that follows all moved elements .
399
383
if _ , ok := entriesIdxMap [pivot ]; ok && directly {
400
384
// The actual pivot for the move is the element that follows all elements
401
385
// from the existing set.
@@ -406,6 +390,7 @@ func GenerateMovements(existing []Movable, expected []Movable, entries []Movable
406
390
return nil , ErrInvalidMovementPlan
407
391
}
408
392
pivot = expected [pivotIdx ]
393
+
409
394
}
410
395
}
411
396
@@ -419,7 +404,7 @@ func GenerateMovements(existing []Movable, expected []Movable, entries []Movable
419
404
420
405
}
421
406
422
- slog .Debug ("GeneraveMovements ()" , "movements" , movements )
407
+ slog .Debug ("GenerateMovements ()" , "movements" , movements )
423
408
424
409
return movements , nil
425
410
}
@@ -464,14 +449,14 @@ func (o PositionBottom) GetExpected(entries []Movable, existing []Movable) ([]Mo
464
449
}
465
450
466
451
func (o PositionBottom ) Move (entries []Movable , existing []Movable ) ([]MoveAction , error ) {
467
- slog .Debug ("PositionBottom.Move())" , "entries" , entries , "existing" , existing )
468
452
expected , err := o .GetExpected (entries , existing )
469
453
if err != nil {
470
454
return nil , err
471
455
}
472
456
473
457
actions , err := GenerateMovements (existing , expected , entries , ActionWhereBottom , nil , false )
474
458
if err != nil {
459
+ slog .Debug ("PositionBottom()" , "err" , err )
475
460
return nil , err
476
461
}
477
462
return OptimizeMovements (existing , expected , entries , actions , o ), nil
@@ -487,7 +472,6 @@ func MoveGroups(existing []Movable, movements []Movement) ([]MoveAction, error)
487
472
for idx := range len (movements ) - 1 {
488
473
position := movements [idx ].Position
489
474
entries := movements [idx ].Entries
490
- slog .Debug ("MoveGroups()" , "position" , position , "existing" , existing , "entries" , entries )
491
475
result , err := position .GetExpected (entries , expected )
492
476
if err != nil {
493
477
if ! errors .Is (err , errNoMovements ) {
@@ -500,7 +484,6 @@ func MoveGroups(existing []Movable, movements []Movement) ([]MoveAction, error)
500
484
501
485
entries := movements [len (movements )- 1 ].Entries
502
486
position := movements [len (movements )- 1 ].Position
503
- slog .Debug ("MoveGroups()" , "position" , position , "expected" , expected , "entries" , entries )
504
487
return position .Move (entries , expected )
505
488
}
506
489
0 commit comments