Skip to content

Commit 20cf0f7

Browse files
improve sort speed
Signed-off-by: mateoconlechuga <matthewwaltzis@gmail.com>
1 parent a2759ef commit 20cf0f7

File tree

2 files changed

+32
-70
lines changed

2 files changed

+32
-70
lines changed

include/macros.inc

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -247,13 +247,6 @@ setting_enable_usb := 2
247247
settings_default := (1 shl setting_show_battery) or (1 shl setting_enable_shortcuts) or (1 shl setting_delete_confirm) or (1 shl setting_basic_indicator)
248248
settings_adv_default := (1 shl setting_special_directories)
249249

250-
; sorting flags
251-
252-
sort_flag := $21
253-
sort_first_item_found := 0
254-
sort_first_hidden := 1
255-
sort_second_hidden := 2
256-
257250
; cesium flags
258251

259252
cesium_flag := $21

src/sort.asm

Lines changed: 32 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,24 @@ sort_vat_entry_temp_end := ti.mpLcdCrsrImage + 12 + 15
3939

4040
sort_vat:
4141
ld iy,ti.flags
42-
ld a,(iy + sort_flag)
43-
push af
44-
call sort_vat_internal
45-
pop af
46-
ld (iy + sort_flag),a
47-
ret
48-
49-
sort_vat_internal:
50-
res sort_first_item_found,(iy + sort_flag)
42+
or a,a
43+
sbc hl,hl
44+
ld (sort_first_item_found_ptr),hl
5145
ld hl,(ti.progPtr)
5246
.sort_next:
5347
call .find_next_item
5448
ret nc
5549
.found_item:
56-
bit sort_first_item_found,(iy + sort_flag)
57-
jp z,.first_found
50+
push hl
51+
ld hl,(sort_first_item_found_ptr)
52+
compare_hl_zero
53+
pop hl
54+
jr nz,.not_first
55+
ld (sort_first_item_found_ptr),hl ; to make it only execute once
56+
call .skip_name
57+
ld (sort_end_of_part_ptr),hl
58+
jr .sort_next
59+
.not_first:
5860
push hl
5961
call .skip_name
6062
pop de
@@ -135,13 +137,6 @@ sort_vat_internal:
135137
ld (sort_end_of_part_ptr),hl
136138
jp .sort_next
137139

138-
.first_found:
139-
set sort_first_item_found,(iy + sort_flag)
140-
ld (sort_first_item_found_ptr),hl ; to make it only execute once
141-
call .skip_name
142-
ld (sort_end_of_part_ptr),hl
143-
jp .sort_next
144-
145140
.skip_to_next:
146141
ld bc,-6
147142
add hl,bc
@@ -156,71 +151,45 @@ sort_vat_internal:
156151
ret
157152

158153
.compare_names: ; hl and de pointers to strings output=carry if de is first
159-
res sort_first_hidden,(iy + sort_flag)
160-
res sort_second_hidden,(iy + sort_flag)
161-
154+
ld b,(hl)
155+
ld a,(de)
156+
ld c,0
157+
cp a,b ; check if same length
158+
jr z,.hl_longer
159+
jr nc,.hl_longer ; b = smaller than a
160+
inc c ; to remember that b was larger
161+
ld b,a ; b was larger than a
162+
.hl_longer:
163+
push bc
164+
ld b,64
162165
dec hl
163166
dec de
164-
ld b,64
165167
ld a,(hl)
166168
cp a,b
167169
jr nc,.first_not_hidden ; check if files are hidden
168170
add a,b
169-
ld (hl),a
170-
set sort_first_hidden,(iy + sort_flag)
171171
.first_not_hidden:
172+
ld c,a
172173
ld a,(de)
173174
cp a,b
174175
jr nc,.second_not_hidden
175176
add a,b
176-
ld (de),a
177-
set sort_second_hidden,(iy + sort_flag)
178177
.second_not_hidden:
179-
push hl
180-
push de
181-
inc hl
182-
inc de
183-
ld b,(hl)
184-
ld a,(de)
185-
ld c,0
186-
cp a,b ; check if same length
187-
jr z,.compare_names_continue
188-
jr nc,.compare_names_continue ; b = smaller than a
189-
inc c ; to remember that b was larger
190-
ld b,a ; b was larger than a
191-
.compare_names_continue:
178+
cp a,c
179+
pop bc
180+
jr .start
181+
.loop:
192182
dec hl
193183
dec de
194184
ld a,(de)
195185
cp a,(hl)
196-
jr nz,.match
197-
djnz .compare_names_continue
198-
pop de
199-
pop hl
200-
call .reset_hidden_flags
186+
.start:
187+
ret nz
188+
djnz .loop
201189
dec c
202190
ret nz
203191
ccf
204192
ret
205-
.match:
206-
pop de
207-
pop hl
208-
.reset_hidden_flags:
209-
push af
210-
bit sort_first_hidden,(iy + sort_flag)
211-
jr z,.first_not_hidden_check
212-
ld a,(hl)
213-
sub a,64
214-
ld (hl),a
215-
.first_not_hidden_check:
216-
bit sort_second_hidden,(iy + sort_flag)
217-
jr z,.second_not_hidden_check
218-
ld a,(de)
219-
sub a,64
220-
ld (de),a
221-
.second_not_hidden_check:
222-
pop af
223-
ret
224193

225194
.find_next_item: ; carry = found, nc = notfound
226195
ex de,hl

0 commit comments

Comments
 (0)