Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return the negative index position if not found. #7

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions example/example2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
var bounds = require('../search-bounds')

//Create an array
var array = [1, 2, 3, 3, 3, 5, 6, 10, 11, 13, 50, 1000, 2200]
console.log({array})

//Print all elements in array contained in the interval [3, 50)
console.log(
array.slice(
bounds.ge(array, 3),
bounds.lt(array, 50)))

//Test if array contains the element 4
console.log('indexOf(6)=', bounds.eq(array, 6))
let index = bounds.eq(array, 4)
console.log('indexOf(4)=', index)

function insert (array, value) {
let index = bounds.eq(array,value)
console.log({index})
if (index < 0) array.splice(-index-1,0,value)
else array.splice(index,0,value)
}
console.log('Insert')
insert(array, 4)
console.log({array})

insert(array, 4)
insert(array, 4)
insert(array, 4)
insert(array, 0)
insert(array, 12)
insert(array, 200)
insert(array, 12200)
console.log({array})
36 changes: 36 additions & 0 deletions example/example3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
var bounds = require('../search-bounds')

// Create an array of objects
var creatures = [
{ legs: 8, name: 'spider' },
{ legs: 4, name: 'mouse' },
{ legs: 4, name: 'cat' },
{ legs: 2, name: 'Ben Franklin' },
{ legs: 4, name: 'table', isCreature: false },
{ legs: 100, name: 'centipede' },
{ legs: 4, name: 'dog' },
{ legs: 6, name: 'ant' }
]

// Sort the array by number of legs
function byLegs(a,b) { return a.legs - b.legs }
creatures.sort(byLegs)

// Find the next creature with more than 4 legs
console.log('What has more than 4 legs? Answer:', creatures[bounds.gt(creatures, {legs:4}, byLegs)])

let names = [
'jan',
'piet',
'koos',
'klaas',
'alta',
'kaspaas'
]
function nameSort(a,b) {
return (a).localeCompare(b)
}
names.sort(nameSort)
console.log({names})

console.log('Who is before klaas?', names[ bounds.lt(names, 'klaas', nameSort)])
2 changes: 1 addition & 1 deletion search-bounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function compileSearch(funcName, predicate, reversed, extraArgs, earlyOut) {
}
code.push("}")
if(earlyOut) {
code.push("return -1};")
code.push("return -l - 1};")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return -1 -1 is just return 0, looks like a bug.

} else {
code.push("return i};")
}
Expand Down