-
-
Notifications
You must be signed in to change notification settings - Fork 292
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
feat(orama): adds zip tree #430
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
Looks great! I'm not sure, function randomRank(): number {
let heads = 0;
while (Math.random() < 0.5) {
heads += 1;
}
return heads;
} |
@hastebrot thanks for the suggestion, I added it as follows: function randomRank (maxAttempts = 32): number {
let heads = 0
for (let i = 0; i < maxAttempts; i++) {
if (Math.random() >= 0.5) break
heads += 1
}
return heads
} |
I was thinking if adding a better random algorithm like one from stdlibjs? |
It's ok, but we can't use external dependencies. |
// Function to generate a random number from a geometric distribution.
function randomGeom(p, r) {
return Math.floor(Math.log(1 - r) / Math.log(1 - p)) + 1;
}
const p = 0.5; // Probability of getting a head in a fair coin flip.
const r = Math.random(); // Generate a random number between 0 and 1.
const numberOfFlips = randomGeom(p, r); > randomGeom(0.5, 0)
1
> randomGeom(0.5, 0.49999)
1
> randomGeom(0.5, 0.5)
2
> randomGeom(0.5, 0.9)
4
> randomGeom(0.5, 0.999)
10
> randomGeom(0.5, 0.99999)
17 so to get > 1 - 1 / Math.pow(2, 17 - 1)
0.9999847412109375 update: function randomRank(r: number): number {
return Math.floor(Math.log(1 - r) / Math.log(1 - 0.5));
} > [...Array(20).keys()].map(() => randomRank(Math.random()))
[
0, 0, 3, 0, 0, 2, 2,
0, 0, 1, 1, 0, 0, 0,
4, 0, 7, 0, 1, 0
] I just don't know if Checked it:
Also Which gives us function randomRank(r: number): number {
if (r === 0) return 0;
return Math.floor(Math.log(1 - r) / Math.log(1 - 0.5));
} |
@thebergamo it's a jungle of code but I managed to find the line with geometric distribution in stdlibjs. what is cool is that they even have a proof for it in the jsdocs. but in the end it's really just a single line of code. |
I'm going to merge this PR without replacing AVL tree for the number index. I'll dedicate a separate PR for this. Thanks @hastebrot for the great help 🙏 |
This PR aims to add an initial implementation of a Zip Tree to substitute AVL Trees on indexes with a lot of numeric properties. It's 100% backward compatible with the AVL Trees and exposes the same APIs.