Skip to content

Commit d559b07

Browse files
committed
algorithms [nfc]: Pull local helper isSortedWithoutDuplicates out to here
1 parent 7ff3ac2 commit d559b07

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-17
lines changed

lib/model/algorithms.dart

+16
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,19 @@ int binarySearchByKey<E, K>(
4141
}
4242
return -1;
4343
}
44+
45+
bool isSortedWithoutDuplicates(List<int> items) {
46+
final length = items.length;
47+
if (length == 0) {
48+
return true;
49+
}
50+
int lastItem = items[0];
51+
for (int i = 1; i < length; i++) {
52+
final item = items[i];
53+
if (item <= lastItem) {
54+
return false;
55+
}
56+
lastItem = item;
57+
}
58+
return true;
59+
}

lib/model/narrow.dart

+2-17
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import '../api/model/initial_snapshot.dart';
33
import '../api/model/model.dart';
44
import '../api/model/narrow.dart';
55
import '../api/route/messages.dart';
6+
import 'algorithms.dart';
67

78
/// A Zulip narrow.
89
sealed class Narrow {
@@ -119,30 +120,14 @@ class TopicNarrow extends Narrow implements SendableNarrow {
119120
int get hashCode => Object.hash('TopicNarrow', streamId, topic);
120121
}
121122

122-
bool _isSortedWithoutDuplicates(List<int> items) {
123-
final length = items.length;
124-
if (length == 0) {
125-
return true;
126-
}
127-
int lastItem = items[0];
128-
for (int i = 1; i < length; i++) {
129-
final item = items[i];
130-
if (item <= lastItem) {
131-
return false;
132-
}
133-
lastItem = item;
134-
}
135-
return true;
136-
}
137-
138123
/// The narrow for a direct-message conversation.
139124
// Zulip has many ways of representing a DM conversation; for example code
140125
// handling many of them, see zulip-mobile:src/utils/recipient.js .
141126
// Please add more constructors and getters here to handle any of those
142127
// as we turn out to need them.
143128
class DmNarrow extends Narrow implements SendableNarrow {
144129
DmNarrow({required this.allRecipientIds, required int selfUserId})
145-
: assert(_isSortedWithoutDuplicates(allRecipientIds)),
130+
: assert(isSortedWithoutDuplicates(allRecipientIds)),
146131
assert(allRecipientIds.contains(selfUserId)),
147132
_selfUserId = selfUserId;
148133

0 commit comments

Comments
 (0)