Implement forward bisect
This commit is contained in:
@@ -15,12 +15,21 @@ import 'package:rimworld_modman/mod_list.dart';
|
|||||||
class ModListTroubleshooter {
|
class ModListTroubleshooter {
|
||||||
final ModList originalModList;
|
final ModList originalModList;
|
||||||
ModList currentModList;
|
ModList currentModList;
|
||||||
|
int _startIndex = 0;
|
||||||
|
int _endIndex = 0;
|
||||||
|
|
||||||
ModListTroubleshooter(ModList modList)
|
ModListTroubleshooter(ModList modList)
|
||||||
: originalModList = modList,
|
: originalModList = modList,
|
||||||
currentModList = modList.copyWith();
|
currentModList = modList.copyWith(),
|
||||||
|
_startIndex = 0,
|
||||||
|
_endIndex = modList.activeMods.length;
|
||||||
|
|
||||||
ModList binaryForward() {
|
ModList binaryForward() {
|
||||||
|
final midIndex = (_startIndex + _endIndex) ~/ 2;
|
||||||
|
final subset = originalModList.activeMods.keys.toList().sublist(midIndex);
|
||||||
|
currentModList.disableAll();
|
||||||
|
currentModList.enableMods(subset);
|
||||||
|
_startIndex = midIndex;
|
||||||
return currentModList;
|
return currentModList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -50,7 +50,7 @@ Mod makeDummy() {
|
|||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
group('ModListTroubleshooter Bisect Tests', () {
|
group('ModListTroubleshooter Bisect Tests', () {
|
||||||
late ModList modList;
|
late ModList modList = ModList();
|
||||||
setUp(() {
|
setUp(() {
|
||||||
modList = ModList();
|
modList = ModList();
|
||||||
|
|
||||||
@@ -71,21 +71,30 @@ void main() {
|
|||||||
);
|
);
|
||||||
modList.mods[modId] = mod;
|
modList.mods[modId] = mod;
|
||||||
}
|
}
|
||||||
|
modList.enableAll();
|
||||||
});
|
});
|
||||||
modList.enableAll();
|
|
||||||
|
|
||||||
test(
|
test(
|
||||||
'Bisect search should end up with half the mods every iteration until 1',
|
'Bisect search should end up with half the mods every forward iteration until 1',
|
||||||
() {
|
() {
|
||||||
final troubleshooter = ModListTroubleshooter(modList);
|
final troubleshooter = ModListTroubleshooter(modList);
|
||||||
|
|
||||||
final result = troubleshooter.binaryForward();
|
var result = troubleshooter.binaryForward();
|
||||||
// Half of our initial 30
|
// Half of our initial 30
|
||||||
expect(result.activeMods.length, equals(15));
|
expect(result.activeMods.length, equals(15));
|
||||||
|
|
||||||
final result2 = troubleshooter.binaryBackward();
|
result = troubleshooter.binaryForward();
|
||||||
// Half of our previous result
|
// Half of our previous result
|
||||||
expect(result2.activeMods.length, equals(7));
|
expect(result.activeMods.length, equals(8));
|
||||||
|
|
||||||
|
result = troubleshooter.binaryForward();
|
||||||
|
expect(result.activeMods.length, equals(4));
|
||||||
|
|
||||||
|
result = troubleshooter.binaryForward();
|
||||||
|
expect(result.activeMods.length, equals(2));
|
||||||
|
|
||||||
|
result = troubleshooter.binaryForward();
|
||||||
|
expect(result.activeMods.length, equals(1));
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
Reference in New Issue
Block a user