Skip to content

Commit 3b02669

Browse files
committed
switch to binarySearch
1 parent 654aa4c commit 3b02669

File tree

7 files changed

+38
-15
lines changed

7 files changed

+38
-15
lines changed

src/findJuzByAyahId.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { JuzList } from "./lists/juzList"
22
import { AyahId, Juz } from "./types"
3+
import { binarySearch } from "./utils"
34
import { checkValidAyahId } from "./validation"
45

56
/**
@@ -11,6 +12,8 @@ import { checkValidAyahId } from "./validation"
1112
export function findJuzByAyahId(ayahId: AyahId): Juz {
1213
checkValidAyahId(ayahId)
1314

14-
const juz = JuzList.findIndex(x => x > ayahId) - 1
15+
// const juz = JuzList.findIndex(x => x > ayahId) - 1
16+
const jj = binarySearch(JuzList, ayahId)
17+
const juz = jj < 0 ? -jj - 2 : jj
1518
return juz as Juz
1619
}

src/findPage.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { findAyahIdBySurah } from "./findAyahIdBySurah"
22
import { PageList } from "./lists/pageList"
33
import { AyahId, AyahNo, Page, Surah } from "./types"
4+
import { binarySearch } from "./utils"
45
import { checkValidSurah } from "./validation"
56

67
/**
@@ -14,5 +15,8 @@ export function findPage(surah: Surah, ayah: AyahNo | AyahId): Page {
1415
checkValidSurah(surah)
1516
const ayahId: AyahId = findAyahIdBySurah(surah, ayah as AyahNo)
1617

17-
return PageList.findIndex(x => x > ayahId) - 1 as Page
18+
// return PageList.findIndex(x => x > ayahId) - 1 as Page
19+
const jj = binarySearch(PageList, ayahId)
20+
const page = jj < 0 ? -jj - 2 : jj
21+
return page as Page
1822
}

src/findPagebyAyahId.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { PageList } from "./lists/pageList"
22
import { AyahId, Page } from "./types"
3+
import { binarySearch } from "./utils"
34
import { checkValidAyahId } from "./validation"
45

56
/**
@@ -18,5 +19,8 @@ import { checkValidAyahId } from "./validation"
1819
export function findPagebyAyahId(ayahId: AyahId): Page {
1920
checkValidAyahId(ayahId)
2021

21-
return PageList.findIndex(x => x > ayahId) - 1 as Page
22+
// return PageList.findIndex(x => x > ayahId) - 1 as Page
23+
const jj = binarySearch(PageList, ayahId)
24+
const page = jj < 0 ? -jj - 2 : jj
25+
return page as Page
2226
}

src/findRubAlHizbByAyahId.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { HizbQuarterList } from "./lists/hizbQuarterList"
22
import { AyahId, RubAlHizbId } from "./types"
3+
import { binarySearch } from "./utils"
34
import { checkValidAyahId } from "./validation"
45

56
/**
@@ -11,5 +12,8 @@ import { checkValidAyahId } from "./validation"
1112
export function findRubAlHizbByAyahId(ayahId: AyahId): RubAlHizbId {
1213
checkValidAyahId(ayahId)
1314

14-
return HizbQuarterList.findIndex(x => x > ayahId) - 1 as RubAlHizbId
15+
// return HizbQuarterList.findIndex(x => x > ayahId) - 1 as RubAlHizbId
16+
const jj = binarySearch(HizbQuarterList, ayahId)
17+
const page = jj < 0 ? -jj - 2 : jj
18+
return page as RubAlHizbId
1519
}

src/findSurahAyahByAyahId.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
1-
import { SurahList as findSurahByAyahIdModule } from "./lists/surahList"
1+
import { SurahList } from "./lists/surahList"
22
import { AyahId, AyahNo, Surah, SurahAyah } from "./types"
3+
import { binarySearch } from "./utils"
34
import { checkValidAyahId } from "./validation"
45

56
/**
67
* Finds the Surah (chapter) and Ayah (verse) numbers that the given Ayah ID belongs to.
78
*
8-
* @param ayaId - The Ayah ID to find the Surah and Ayah numbers for.
9+
* @param ayahId - The Ayah ID to find the Surah and Ayah numbers for.
910
* @returns An array containing the Surah number and the Ayah number within that Surah.
1011
*/
1112

12-
export function findSurahAyahByAyahId(ayaId: AyahId): SurahAyah {
13-
checkValidAyahId(ayaId)
13+
export function findSurahAyahByAyahId(ayahId: AyahId): SurahAyah {
14+
checkValidAyahId(ayahId)
1415

15-
const suraNum: Surah = (findSurahByAyahIdModule.findIndex(x => x[0] > ayaId) - 1) as Surah
16-
const ayahNo = (ayaId - findSurahByAyahIdModule[suraNum][0] + 1) as AyahNo
17-
return [suraNum, ayahNo]
16+
// const suraNum: Surah = (SurahList.findIndex(x => x[0] > ayahId) - 1) as Surah
17+
const ss = binarySearch(SurahList, ayahId, (aya, b) => (aya - b[0]))
18+
const suraNum = ss < 0 ? -ss - 2 : ss
19+
20+
const ayahNo = (ayahId - SurahList[suraNum][0] + 1) as AyahNo
21+
return [suraNum as Surah, ayahNo]
1822
}

src/getAyahMeta.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import { getRubAlHizbMetaByAyahId } from "./getRubAlHizbMetaByAyahId"
44
import { HizbQuarterList } from "./lists/hizbQuarterList"
55
import { JuzList } from "./lists/juzList"
66
import { PageList } from "./lists/pageList"
7+
import { RukuList } from "./lists/rukuList"
78
import { SajdaList } from "./lists/sajdaList"
89
import { SurahList } from "./lists/surahList"
910
import { AyahId, AyahMeta, Page } from "./types"
11+
import { binarySearch } from "./utils"
1012
import { checkValidAyahId } from "./validation"
1113

1214
/**
@@ -25,11 +27,13 @@ export function getAyahMeta(ayahId: AyahId): AyahMeta {
2527
const isSajdahAyah = SajdaList.some(([sajdaAyahId]) => sajdaAyahId === ayahId)
2628
const isStartOfSurah = SurahList[surah][0] === ayahId
2729
const isStartOfPage = PageList[page] === ayahId
30+
// const isStartOfRuku = RukuList[page] === ayahId
2831
const isStartOfJuz = JuzList[quarterData.juz] === ayahId
2932
const isStartOfQuarter = HizbQuarterList[quarterData.rubAlHizbId] === ayahId
3033
const isEndOfSurah = SurahList[surah + 1][0] - 1 === ayahId
3134
const isEndOfPage = PageList[page + 1] - 1 === ayahId
3235
const isEndOfJuz = JuzList[quarterData.juz + 1] - 1 === ayahId
36+
const isEndOfRuku = JuzList[quarterData.juz + 1] - 1 === ayahId
3337
const isEndOfQuarter = HizbQuarterList[quarterData.rubAlHizbId + 1] - 1 === ayahId
3438

3539
return {

src/utils.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
* @param compare_fn - An optional comparison function to use for the search. Defaults to a simple numeric comparison.
77
* @returns The index of the element if found, or a negative value indicating the insertion point if not found.
88
*/
9-
export function binarySearch(
10-
ar: Array<number>,
11-
el: number,
12-
compare_fn: (a: number, b: number) => number = (a, b) => a - b
9+
export function binarySearch<T, X>(
10+
ar: Array<T>,
11+
el: X,
12+
compare_fn: (a: X, b: T) => number = (a, b) => a as number - (b as number)
1313
): number | -1 {
1414
let m = 0
1515
let n = ar.length - 1

0 commit comments

Comments
 (0)