Skip to content

Commit b85b01f

Browse files
reformat the code
1 parent 516814a commit b85b01f

File tree

2 files changed

+17
-33
lines changed

2 files changed

+17
-33
lines changed

lib/features/searching/view_model/grid_notifier.dart

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -269,10 +269,10 @@ class SearchingNotifier extends StateNotifier<GridNotifierState> {
269269
const right = 1;
270270

271271
final directions = [
272-
up, // up
273-
down, // down
274-
left, // left
275-
right, // right
272+
up,
273+
down,
274+
left,
275+
right,
276276
];
277277

278278
while (queue.isNotEmpty) {
@@ -299,7 +299,7 @@ class SearchingNotifier extends StateNotifier<GridNotifierState> {
299299
}
300300
}
301301

302-
// for marking the current grid as visited
302+
/// for marking the current grid as visited
303303
if (gridData[currentIndex] != GridStatus.startPoint &&
304304
gridData[currentIndex] != GridStatus.targetPoint) {
305305
gridData[currentIndex] = GridStatus.searcher;
@@ -345,7 +345,6 @@ class SearchingNotifier extends StateNotifier<GridNotifierState> {
345345

346346
distance[startPointIndex] = 0;
347347

348-
// priority queue to get the minimum distance vertex
349348
final pq = PriorityQueue<int>((a, b) => distance[a].compareTo(distance[b]));
350349
pq.add(startPointIndex);
351350

@@ -359,10 +358,9 @@ class SearchingNotifier extends StateNotifier<GridNotifierState> {
359358
while (pq.isNotEmpty) {
360359
final currentIndex = pq.removeFirst();
361360

362-
// Mark the current node as visited
361+
/// mark the current node as visited
363362
visited[currentIndex] = true;
364363

365-
// If we reached the target, we trace back the path
366364
if (currentIndex == targetPointIndex) {
367365
_tracePath(previous, currentIndex);
368366
_isBuildingGrid = false;
@@ -377,14 +375,15 @@ class SearchingNotifier extends StateNotifier<GridNotifierState> {
377375
continue;
378376
}
379377

380-
final tentativeDistance = distance[currentIndex] + 1; // Assume weight of 1 for each move
378+
final tentativeDistance = distance[currentIndex] + 1;
379+
380+
/// assume weight of 1 for each move
381381
382382
if (tentativeDistance < distance[neighborIndex]) {
383383
distance[neighborIndex] = tentativeDistance;
384384
previous[neighborIndex] = currentIndex;
385385
pq.add(neighborIndex);
386386

387-
// Visualize the search process
388387
if (gridData[neighborIndex] != GridStatus.startPoint &&
389388
gridData[neighborIndex] != GridStatus.targetPoint) {
390389
gridData[neighborIndex] = GridStatus.searcher;
@@ -403,8 +402,8 @@ class SearchingNotifier extends StateNotifier<GridNotifierState> {
403402
final isFirstLeftInRowIndex = neighborIndex % cross == 0;
404403
final isEndRightInRowIndex = (neighborIndex + 1) % cross == 0;
405404

406-
if (direction == 1 && isFirstLeftInRowIndex) return false; // avoid exiting the boundaries
407-
if (direction == -1 && isEndRightInRowIndex) return false; // avoid exiting the boundaries
405+
if (direction == 1 && isFirstLeftInRowIndex) return false;
406+
if (direction == -1 && isEndRightInRowIndex) return false;
408407

409408
return neighborIndex >= 0 &&
410409
neighborIndex < gridData.length &&
@@ -506,7 +505,6 @@ class SearchingNotifier extends StateNotifier<GridNotifierState> {
506505
pq.add(neighborIndex);
507506
}
508507

509-
// visualize
510508
if (gridData[neighborIndex] != GridStatus.startPoint &&
511509
gridData[neighborIndex] != GridStatus.targetPoint) {
512510
gridData[neighborIndex] = GridStatus.searcher;
@@ -521,7 +519,6 @@ class SearchingNotifier extends StateNotifier<GridNotifierState> {
521519
}
522520

523521
double _heuristic(int index, int targetIndex, int cross) {
524-
// Manhattan distance
525522
final x1 = index % cross;
526523
final y1 = index ~/ cross;
527524
final x2 = targetIndex % cross;
@@ -549,17 +546,15 @@ class SearchingNotifier extends StateNotifier<GridNotifierState> {
549546

550547
final gridData = List<GridStatus>.from(state.gridData);
551548

552-
// Clear the maze but keep start and target points
549+
/// clear the maze but keep start and target points
553550
for (int i = 0; i < gridData.length; i++) {
554551
if (gridData[i] != GridStatus.startPoint && gridData[i] != GridStatus.targetPoint) {
555552
gridData[i] = GridStatus.empty;
556553
}
557554
}
558555

559-
// 🔹 Draw outer border walls
560556
await _drawBorders(gridData);
561557

562-
// 🔹 Force the second border inside as a corridor (always empty)
563558
_makeSafeCorridor(gridData);
564559

565560
final random = Random();
@@ -574,7 +569,6 @@ class SearchingNotifier extends StateNotifier<GridNotifierState> {
574569
}
575570

576571
Future<void> _drawBorders(List<GridStatus> gridData) async {
577-
// Top & bottom rows
578572
for (int c = 0; c < state.columnCrossAxisCount; c++) {
579573
for (final r in [0, state.rowMainAxisCount - 1]) {
580574
final idx = r * state.columnCrossAxisCount + c;
@@ -586,7 +580,6 @@ class SearchingNotifier extends StateNotifier<GridNotifierState> {
586580
}
587581
}
588582

589-
// Left & right columns
590583
for (int r = 1; r < state.rowMainAxisCount - 1; r++) {
591584
for (final c in [0, state.columnCrossAxisCount - 1]) {
592585
final idx = r * state.columnCrossAxisCount + c;
@@ -664,7 +657,6 @@ class SearchingNotifier extends StateNotifier<GridNotifierState> {
664657
}
665658
}
666659

667-
// Recursive Division Maze Generation
668660
Future<void> _generateRecursiveDivisionMazeFun() async {
669661
if (_isSearched) await clearTheGrid(clearAnyway: true);
670662

@@ -707,7 +699,7 @@ class SearchingNotifier extends StateNotifier<GridNotifierState> {
707699
final horizontal = random.nextBool();
708700

709701
if (horizontal) {
710-
// Horizontal wall
702+
/// horizontal wall
711703
int wallRow = row + (random.nextInt(height ~/ 2)) * 2 + 1;
712704
int passageCol = col + (random.nextInt(width ~/ 2)) * 2;
713705

@@ -717,14 +709,14 @@ class SearchingNotifier extends StateNotifier<GridNotifierState> {
717709
gridData[wallRow * state.columnCrossAxisCount + c] != GridStatus.targetPoint) {
718710
gridData[wallRow * state.columnCrossAxisCount + c] = GridStatus.wall;
719711
state = state.copyWith(gridData: List.from(gridData));
720-
await Future.delayed(mazeDuration); // 🔹 Delay for each cell
712+
await Future.delayed(mazeDuration);
721713
}
722714
}
723715

724716
await _divide(row, col, wallRow - row, width, gridData);
725717
await _divide(wallRow + 1, col, row + height - wallRow - 1, width, gridData);
726718
} else {
727-
// Vertical wall
719+
/// vertical wall
728720
int wallCol = col + (random.nextInt(width ~/ 2)) * 2 + 1;
729721
int passageRow = row + (random.nextInt(height ~/ 2)) * 2;
730722

@@ -734,7 +726,7 @@ class SearchingNotifier extends StateNotifier<GridNotifierState> {
734726
gridData[r * state.columnCrossAxisCount + wallCol] != GridStatus.targetPoint) {
735727
gridData[r * state.columnCrossAxisCount + wallCol] = GridStatus.wall;
736728
state = state.copyWith(gridData: List.from(gridData));
737-
await Future.delayed(mazeDuration); // 🔹 Delay for each cell
729+
await Future.delayed(mazeDuration);
738730
}
739731
}
740732

lib/features/sorting/counting/view_model/counting_sort_notifier.dart

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,25 @@ class CountingSortNotifier extends SortingNotifier {
77
final steps = <SortingStep>[];
88
final arr = List<int>.from(values);
99

10-
if (arr.isEmpty) {
11-
return SortingResult(sortedValues: arr, steps: steps);
12-
}
10+
if (arr.isEmpty) return SortingResult(sortedValues: arr, steps: steps);
1311

14-
// 1. Find range of input values
1512
int maxVal = arr.reduce((a, b) => a > b ? a : b);
1613
int minVal = arr.reduce((a, b) => a < b ? a : b);
1714
int range = maxVal - minVal + 1;
1815

19-
// 2. Frequency array
2016
final count = List<int>.filled(range, 0);
2117
for (int i = 0; i < arr.length; i++) {
2218
count[arr[i] - minVal]++;
2319
}
2420

25-
// 3. Build the sorted array
2621
int index = 0;
2722
for (int i = 0; i < range; i++) {
2823
while (count[i] > 0) {
2924
int correctValue = i + minVal;
3025

3126
if (arr[index] != correctValue) {
32-
// Find where the correctValue currently is
3327
int targetIndex = arr.indexOf(correctValue, index);
3428

35-
// Log swap
3629
steps.add(SortingStep(index1: index, index2: targetIndex, action: SortingStatus.swapping));
3730
arr.swap(index, targetIndex);
3831
steps.add(SortingStep(index1: index, index2: targetIndex, action: SortingStatus.swapped));
@@ -43,7 +36,6 @@ class CountingSortNotifier extends SortingNotifier {
4336
}
4437
}
4538

46-
// Mark all sorted at the end
4739
steps.add(SortingStep(index1: arr.length - 1, index2: arr.length - 1, action: SortingStatus.sorted));
4840

4941
return SortingResult(sortedValues: arr, steps: steps);

0 commit comments

Comments
 (0)