Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!

Kwolok posted:

I am working on a spa at a new company. They are using the new Google Identity Services.
Related question - is there a library that makes Oauth/OpenID just for login easy for the developer and user-friendly for the user, and doesn't require a bunch of special cases for each of the different providers? I'd like shortcut buttons for the common ID sources, but I don't want to have to write (or even *have in the library*) special Google code, special Facebook code, special Github code, etc. on both server and client. And excluding real user-selected openid services seems like a dick move, even if it's the case that 99.9% of users will just use one of the common services.

I realize the server-side end of this is a pain because who knows what language you're using even. Something simple and convenient for the client end of the deal would be a great start.

As far as I can tell, google and facebook both require the server-owner to register an API key and jump through hoops for the privilege of users being able to log in, which seems like it goes against the openid design which I thought was supposed to be "you can log in to anywhere with an ID from anywhere else that implements support for this, just tell the server which other server to ask", but maybe I'm missing something.

Adbot
ADBOT LOVES YOU

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!

quote:

Passport can be unobtrusively dropped in to any Express-based web application
That's what I get for not being specific enough I suppose. I was hoping for a flat javascript library, not something tied to a framework I don't want to use.

I guess that does at least reveal the answer to my final question about properly open openid, which is as expected a big "gently caress you" from Google and Facebook. (Which also means the other goal of a simple library is going to be a no as well, because any such library necessarily has to handle everything as a special case because the big boys are assholes.)

roomforthetuna fucked around with this message at 14:14 on Feb 24, 2023

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!
I was curious whether doing a bunch of drawImage to a single canvas, or having a bunch of canvas objects moving around, would be better for a game-style "redrawing the entire screen every frame" sort of thing, so I made a little test. Turns out drawing 2000 64x64 sprites from off-screen canvases with drawImage runs at 6fps on my machine, versus moving 2000 on-screen canvases with the same content runs at 20fps. (So obviously it would be even more-better to use moving elements if there's some part of the content that doesn't update.)

Though now I feel like I should also test how it handles with a 3D context, making the sprites as textures associated with triangle-pairs, and refreshing a vertexbuffer to move them around. But for my purposes I'm pretty sure on-screen canvases is going to be adequate, and so much less annoying to code than the real high performance option.

Edit: to be clear, "for my purposes" is a lot fewer than 2000 on-screen items. And probably also interacting with some vector-drawing.

roomforthetuna fucked around with this message at 22:49 on Mar 11, 2023

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!

Data Graham posted:

That makes total sense once you know about it, but now I'm just trying to imagine troubleshooting it without that knowledge

Or doing that on purpose
Enjoying the idea of doing that in ways where you can't just remove the nonsense as well, like the extra clauses are necessary.
code:
someObject[x+="\n", (q=5), "someKey"] = "hello";
Edit: I especially like that I'm not sure whether that makes x+="someKey" or the thing it looks like, not sure about the precedence of comma versus += operator.

roomforthetuna fucked around with this message at 01:40 on Mar 16, 2023

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!

gbut posted:

I never even heard of it before this discussion, and it doesn’t even make that much sense. It’s a bit funny, yes, but if I was interviewing someone and they used that, I’d dock a point.
Same, except "dock a point" is a bit harsh, I'd probably have said "the what" and reproduced this discussion in the interview, thereby wasting some of their time.

Now that I know it's a thing, an interviewee could just do it and be fine.

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!

Jabor posted:

How long does it take to render a single card once? I wouldn't expect it to be an issue to re-render all 200 cards within a single frame - what might be an issue is if you're updating all 200 cards once each, and each update ends up re-rendering all 200 cards, resulting in 40,000 renders (less than 1% of which are actually required).
You can get some sluggishness from re-rendering 200 things if each render is like "here's an image, and some canvas operations, and some text on a canvas". Even 200 simple blit operations onto a canvas can be a *little* sluggish.

One thing I'd suggest is, if you're rendering cards as text etc. give them a "cache canvas" - render to the canvas only if the card (or canvas size) actually changed, otherwise just blit from the canvas, or reposition the canvas (depending on the underlying structure you have). Doing this was about a 5x speed improvement on my mahjong game where the tiles were originally rendered as fairly simple vector graphics.

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!

Vino posted:

I'm the only one using this code, it's a small game. I don't usually write code straight into files but the one line in main.js is initializing stuff. You have to have at least one line of code in the top level or the game won't do anything right?
Your one line of code at the top level could be document.onload="let c = new C()", or in the associated HTML file do <body onload="js_main()"> or something like that.

i.e. no, you don't have to have at least one line of code in the top level. But I don't think that's what's causing your problem anyway, circular dependencies in JS suck.

Here's a really long painful explanation.

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!

Vino posted:

Thank you for the explanations. Makes a little bit more sense.

So this can be solved with some kind of packager? I am using npm, which packager could I use?

Or is there some way I can organize my files to keep things simpler? Like have a rule that all top level code should be in one file that has no function definitions or classes, would that work?
A packager won't help, and your top level file can contain whatever it wants but nothing should import from it, and no other file should do top level calls, would solve the problem.

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!

Vino posted:

This almost got me everything I need in that I managed to get a typescript + electron/webpack + i write my code with ESM and not CJS, but I can't seem to figure out how to preserve this build path and also regular running it on the web without webpack at the same time. Is that something I should aspire to or should I lean 100% into webpack? The main reason I'm hesitant to go 100% webpack is I'm skeptical of the debugging experience of a webpack'd app.
Webpack configured right with map files is pretty debuggable (even has a devmode with dynamic updates if your app can run locally). Getting it into that state correctly can be quite a chore though. The big curse of webpack is that the config structure has been through multiple not-backwards-compatible iterations so if you ask the internet how to do a thing it's about 50:50 whether the answers will be completely wrong for the version you have.

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!

go play outside Skyler posted:

i went through the pain of getting an electron+ts+import app running and also had some problems with debug working but release failing. it took me a whole loving day of fiddling with webpack and i hope i never have to do it again
I have a project that I keep occasionally coming back to, and I have to remember how the webpack mess works each time. I ended up also putting some one-liner scripts in the folder so I don't have to remember, I can just do "./build" and "./run" and the like. Pro tip.

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!

MREBoy posted:

code:
url='../../../../../map_tiles/{z}/{z}_{x}_{y}.png'
which makes more sense to me if something were to happen to something.com in the future as this would be a more relative(-ish?) link instead of hard coded.
URLs aren't accessing the filesystem, they're accessing the web server, which evidently has /map_tiles/ pointing at the directory in which those files reside.

url="/map_tiles/{z}/{z}_{x}_{y}.png"

should work and make it not dependent on the server address remaining unchanged - but it *does* make it dependent on the image files continuing to be served on the same domain as the html, versus the current very explicit URLs make it so if the html was moved to somethingelse.com and the images stayed where they were, it would still work.

(Your mess of .. would probably also work, but only because .. from the top level path does nothing.)

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!

some kinda jackal posted:

I've gotten a lot done with Node, and it's not like I'm flailing. I'm googling a lot, reading a lot of docs, but then I come across some cool concept I didn't know and I'm left wondering what else is useful that I could be making use of, even at a basic level. Absent finding anything above I still think I've had more fun programming in the past week and change than I have in the last 20 blindly groping around google for python snippets and it's really rewarding. I could probably continue blindly groping for node and TS stuff on google instead and be just fine, but I guess I find this valuable enough that I can dedicate at least some time to learn the basics. I just don't relish sitting through a 12 hour youtube video on JS.
I am really sympathetic to this particular plight because I do *all the languages* at work and it's frustrating when something comes up that isn't how you might expect (e.g. python's async/await doesn't actually set a thing in motion at all until you await that specific thing, unless you also do something with a Task, versus javascript's does, and golang's equivalent does.)

Some things that are good to know about, may not be familiar from other languages, and make a huge difference:
1. async/await (and using them without converting them into promises in almost every context)
2. if you're doing front-end, document.querySelectorAll is massive (though less so if you're also using some framework)
3. generics in typescript can solve a lot of typing problems
4. The magic of backtick strings for formatting (never do str1+" "+str2+" "+str3!)

Fun thing I learned at work yesterday - react <div>{someBigIntVariable}</div> gives an empty div, but `${someBigIntVariable}` stringifies it like you'd expect.

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!

Lumpy posted:

Meh, on the front end you just compress the giant background image marketing _has_ to have on the login screen by an extra 1% and you save the file size of Luxon in your bundle.
But that's how marketing excuses their giant image, because if you just compress the humungous machine learning model that the telemetrics team wants in there you save the file size of their giant image. Where will it end?!

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!

Nolgthorn posted:

I don't have anything to contribute but I want to compliment your easy to read well organized code.

Leng posted:

JavaScript code:
// Select relevant HTML elements
    }} else {
      if (selectedFilterParams.includes(button.dataset.filter)) {
        console.log("Skipped as already in filter params");
      } else {
        selectedFilterParams.push(button.dataset.filter);
  }
I do not find these indents-not-matching-brackets to be well-organized or easy to read, and I suspect without having done much inspection that the problem lies in a condition being in the wrong place from this. Run it through an auto-formatter! (Though I also would be suspicious of a block that says it removes something calling Array.push which is an action that doesn't remove anything.)

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!

necrotic posted:

One of the biggest lessons to learn from that help is to work at breaking down what you are doing into smaller chunks. One big function doing a lot of stuff is generally a signal that it can be broken down into smaller "units of work" that each individually solve a smaller part of the larger problem.
But also don't go nuts with it. I know someone who insists that if a function is more than ten lines long you should break it into more functions, but really if it's just doing one sequential bunch of things it's fine to be lots of lines.

If you have a three-line *condition*, on the other hand, that's something that is probably worth making a little function for, because if nothing else if (thing.smellsBad()) is much easier to quickly understand in passing when you're reading your code later than if (thing.scentRatio>0.8 && thing.scentType==ScentType.Bad && !dog.hasNoNose).

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!

Nolgthorn posted:

You have to do something like render it on the page with no height or width, measure it, then adjust the size using a percentage. There's no easy way to do it because different fonts and operating systems and browsers and etc.
I *think* you can do it render-measuring in a hidden element to avoid creating screen jitter. If not, there's definitely TextMetrics from a CanvasRenderingContext2D.measureText() you can use for this. My experience is the least problematic way to get to a good font size for some constraints is to start with a maximum and minimum acceptable size and binary search for the best fit to within a precision tolerance. i.e. binary search until the difference between "largest one that fit" and "smallest one that was too big" is less than some arbitrary threshold, then use the "largest one that fit" value.

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!
I've just started migrating my big javascript game project from being bare javascript drawing everything on a canvas (which worked okay!) to using svelte, which in general feels like a simplicity improvement, except when I run into problems where css seems super reluctant to do the layout things I want.

One in particular that seems to be a recurring bugbear for many people (such that stackoverflow answers seem to mostly end with "here's a poo poo option that kinda vaguely worked for this one specific situation") and which I can't find a good answer to, is I frequently want to make an element have a fixed aspect ratio and [otherwise] take up all the space available to it. (e.g. a square element in a wide rectangle should be square with equal height, centered, and in a tall rectangle should be a square with equal width, centered.)

css has an aspect-ratio style but it seems to be lower precedence than width and height, so width: 100%, height: 100%; aspect-ratio: 1, for example, just ignores the aspect ratio.

You can get what I want in the *full screen* by doing width: 100vw; height: 100vh; max-width: 100vh; max-height: 100vw, but there doesn't seem to be an equivalent of vw and vh for "the size of the container you're in".

max-width: 100%; max-height: 100%; aspect-ratio: 1 works for *some* situations (where the child elements want to be big and also square) but doesn't work if the child elements are also wanting to decide their size based on the available space.

I guess one option would be to bind the width and height of the parent element and change the style between `width: 100%` if parentHeight>parentWidth and `height: 100%' if parentWidth>parentHeight (with a fixed aspect-ratio either way), but I want to do this layout style quite frequently and all that binding would be a loving mess.

Is there a way that works?

(Parent element is often a cell of a display:grid element which may be making this better or worse since there's style elements for centering like that, but a grid element with width/height 100% seems frustratingly willing to break outside of the constraints it was given. In a pinch I could bind parent element dimensions and just do a lot of absolute positioning instead, but that would end up more like my old canvas app and I'm *trying* to take advantage of things that one would expect to make life easier!)

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!

N.Z.'s Champion posted:

If the element is an <img> or <canvas> etc you can use CSS object-fit to format it like a background image (contain, cover, etc.) to fit the container. Another approach is to size with a CSS unit that's the same horizontally and vertically (ie vw or vh but not both) so your code might look like <div style="width: 5vw; height: 5vw">...</div>. I'm guessing the hacky approaches you've found are those old padding-bottom-based approaches and yeah, I don't like those either.
The thing that makes it extra awkward is that I'm trying to stack two canvas objects the same size (the upper one being to intercept inputs, the underlying idea being that when you're done drawing on it, the lower one moves on and retains what you were drawing and the input one goes away - could be achieved by conditionally binding events instead but this way feels cleaner, apart from the css!)

So what I'm aiming for is *either* the container object being sized to available space and both canvases expand-centered into it, or the container object being expand-centered into *its* available space, and the canvases just 100% sized.

Though actually, now I mention it, I'm already doing dynamic width= and height= on the canvases based on the size of the parent, so image-style scaling might work, if you can do object-fit at the same time as position: absolute/relative. (Or, since I'm doing dynamic sizing anyway, I could just plain set the position and size for those ones.)

The worse case is trying to get a grid of 7x3 squares to appear in one cell of a parent grid, scaled up to the biggest size that fits, while not spacing out the contents.
i.e.
code:
I want

+-------------+
|   XXXXXXX   |
|   XXXXXXX   |
|   XXXXXXX   |
+-------------+

or

+-------+
|       |
|XXXXXXX|
|XXXXXXX|
|XXXXXXX|
|       |
+-------+

but I keep getting

+-------------+
|X X X X X X X|
|X X X X X X X|
|X X X X X X X|
+-------------+

or even worse

+-------------+
|x x x x x x x|
|x x x x x x x|
|x x x x x x x|
+-------------+
(where lowercase x indicates the sub-squares are smaller even though there is room)

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!

N.Z.'s Champion posted:

Hmm could you combine the canvases? ie make a single composition canvas draw ctx.drawImage(otherCanvas);ctx.drawImage(anotherCanvas) in your requestAnimationFrame render loop, and dispatch events to offscreen/unmounted canvases

Then with a single canvas I'm guessing the sizing problem would be easier
It's really more the getting a thing to size-and-position as I want in a grid cell, it turns out. I can put the canvases inside one element so they can do absolute positioning relative to that element, but even if there's no canvases involved, getting that one element to be positioned and sized right is a problem.
If I inspect the grid I can see that the cells are the expected shape, but I just cannot get a rectangular cell to contain a centered square child element scaled to "fit".
(Current state: I gave up and just put a scaled-up rectangle in the cell then bound the size of that to some variables and bound calculations on those variables to top, left, width and height of the child element, which gets it right but fucks up the first frame.)

Edit: Extra fun - I had a grid in a grid cell where both grids were width:100% height:100%, then I scaled up the window, they would grow as expected, but if I then shrunk the window (still bigger than its original size), the grids would end up outside the bounds of the window - I think the inner grid forced the outer grid to stay "big", which in turn kept the inner grid from being scaled down because it was scaled to 100% of the available size, which didn't shrink because...

roomforthetuna fucked around with this message at 05:39 on Apr 10, 2024

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!

biznatchio posted:

I would have answered a couple days ago but I was on the road, but this does what I think you were asking for. There's a resize handle on the blue container div to resize it to test the behavior.
Nice, thanks, that looks like it does the trick, even with nested grids involved. https://playcode.io/1831865

(Bit of weirdness in there, the chessboard-grid is somehow sometimes 1 pixel "down" relative to what should be its constraints. I guess the translate -50% operation does its math not quite the same as the left/top 50%, maybe resulting in rounding the other way.)

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!

biznatchio posted:

I do see that behavior at some zoom levels. It's a result of the layout calculations working on floating point pixels which get snapped to whole device pixels during rendering; and I don't think there's any good controls in CSS for avoiding it.

But it's easy enough to hack in some more precise custom layout logic instead; but it does require some Javascript.

edit: Or a more efficient, but more complex version.
Noice. I had working javascript positioning already but I *think* I prefer the css "less interpreted more automatic" version even with the pixel miss. I'm mostly not having visible grids and the intent is just to lay things out dynamically in a way that makes a single-screen view usable in portrait or landscape, and look reasonable and use most of the space regardless of overall aspect ratio - being off by one pixel therefore won't tend to meaningfully show up, because it will be a one pixel difference in "gap" not (as in the example) a one pixel difference where edged rectangles overlap and it looks like a mistake.

Related but changing topic, Svelte has some magic css animation support that tries to do a smooth positional animation when you change the location of a thing (e.g. as in this tutorial - click solve then some of the todo items to see what it does easily); I'll be super impressed if it manages to perform a clean positional animation when the item gets repositioned between totally distinct multilayered layout monstrosities. But it seems like it might do it, since the things it's positioning between in that tutorial don't have any direct relationships.

I'm curious whether I'll be able to persuade the css animation automation to do the high-math animation technique I previously implemented in flutter (I might have done it in js as well, I don't remember), as some sort of custom easing - the effect I wanted was to animate based on *maximum acceleration* rather than merely easing between positions. A simple example difference is if you moved an item from 0 to 100 it would accelerate until it reaches 50 then start decelerating, just like an easing curve, but then if, for example, when it's at position 25 in that animation, you reset its target position to 25, standard easing-style animation would just make a new curve, say "it's already there" and you'd get a janky sudden stop. My animation goes "the new track is starting at 25,0 with velocity n,0, so it has to decelerate first", then recalculates the subsequent curve from where it will be when velocity-x equals zero - each animation consists of one to four curves depending on how many stages it has to do to smoothly stop at its destination. (It also tries to get the motion duration to be equalized so x and y fully resolve at the same time, because if you just animate x and y independently then you get stupid arcs instead of straight lines.)

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!
Is there a css thing for associating some style with a #shadow-root node itself?
Specifically, I'm curious if I can do
code:
<custom-element>
  #shadow-root
    <style> ??? </style>
    some text in Verdana font
</custom-element>
versus having to do
code:
<custom-element>
  #shadow-root
    <style>span {font-family: Verdana;}</style>
    <span>some text in Verdana font</span>
</custom-element>

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!

biznatchio posted:

You can use the CSS selector :root in a stylesheet within a shadow DOM to refer to the shadow-root pseudo element.

And as necrotic mentioned, if your <style> or <link rel="stylesheet"> elements for all instances of a custom element are identical, you can save memory and processing time by loading the stylesheet into a CSSStyleSheet object instead and assigning it in an array to the shadow root's adoptedStylesheets property; that way you can share one in-memory stylesheet among multiple shadow DOMs; which is very highly recommended if you're making lots of instances of your custom element.
Awesome, thank you. Googling this just kept turning up loads of results saying stick <style>bla</style> into a shadow root, and all of the examples only styling child elements. Sharing the stylesheet will be good too (I don't think it really matters for my use-case where the whole thing is tiny, but I always love a good micro-optimize!)

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!
Hm, is there dynamic styling for shadowRoot too somehow?
Typescript complains about
code:
this.shadowRoot!.style.font = whatever;
(Property style does not exist on type ShadowRoot)

The other answer simplified one of my custom elements, but looks like maybe this one is still gonna have to have a span inside it.

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!

Jabor posted:

You can mutate the CSSStyleSheet you've already given it and have those changes take effect, yeah?
Ah, that would probably work, but seems likely to be more expensive than having a child element, right? (And especially problematic if I'm sharing the style sheet between multiple elements and mutating them individually.)

In this case I'm migrating a custom element from being main-style-sheeted by type name, e.g. my-element {fixed formatting} and scaling it by doing this.style.fontSize=fittedSize in the custom element. Probably makes more sense to put the text in a span, style it as span{whatever} rather than :root{whatever}, and then be able to apply the font size override the simple way. Just a shame that using shadow dom for this is making the tree two elements deeper than it was with a "globally styled" custom element.

Adbot
ADBOT LOVES YOU

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!

Combat Pretzel posted:

Does anyone happen to know a package that implements a BST with sort of getNextSmallest/getNextLargest methods?

I'm implementing some scheduling stuff that mixes vacation, holiday, shift and exception calendars and I need a way to look up things fast. However when I'm like "it's today, when the next exception", I'm currently doing a linear search for every chunk I compute, and the longer the list of exceptions, the bigger the performance problem obviously. The few packages I checked always search for exact values.
Not aware of a package, but a binary search is simple enough that you can pretty much just copy-paste one, e.g. https://www.geeksforgeeks.org/binary-search-in-javascript/ - I'd take the iterative one, have it return an index (mid instead of true, start instead of false), and maybe have it take a target comparison function instead of === x and < x.

That's if you're okay with sorting the data in a linear structure as a precursor to search. If you need fast insert/remove as well then you probably want a redblack tree which is quite a bit more involved.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply