If I want to maintain an ordered list in Firebase, it seems like the best way to do it is to manually assign a priority to each item in my list. That means if I insert or remove an item from the list, I have to update the priorities of all the items following it. For an item at the beginning of the list, this means updating every item in the list. Is there a better performing data structure or algorithm to use in this case?
You can create an ordered list by setting the priority of elements appropriately. Items in a list are ordered lexigraphically by priority, or if the priority can be parsed to a number, by numeric value.
If you want to insert items into the middle of an existing list, modifying the priorities of the existing items would work, but would be horribly inefficient. A better approach is just to pick a priority between the two items where you want to insert the value and set that priority for the new item.
For example, if you had element 1 with priority "a", and element 2 with priority "b", you could insert element 3 between the two with priority "aa" (or "aq", "az", etc).
In our experience, most times when you create an ordered list, you don't necessarily know the position in the list you want to insert the item beforehand. For example, if you're creating a Leader Board for a game, you don't know in advance that you want to place a new score 3rd in the list, rather you know you want to insert it at whatever position score 10000 gets you (which might happen to be third). In this case, simply setting the priority to the score will accomplish this. See our Leader Board example here:
https://www.firebase.com/tutorial/#example-leaderboard
The Ruby gem ranked_model has an interesting approach to this problem. It uses a position integer like many other "acts as list" implementations, but it doesn't rely on re-writing all the integers on each position move. Instead, it spaces the integers widely apart, and so each update may only affect one or two rows. Might be worth looking through the readme and code to see if this approach could fit here.
Related
I'm attempting to show elements based on an input's value, and would like it so that people can type in multiple words, and sort through the elements to show only the elements that apply to their search.
JS Bin, extremely basic example: https://jsbin.com/zabikejuxa/edit?html,js,output
Such as having a full list of vehicles, and if a customer types in '2015', only the 2015 vehicles would display. If the customer types in '2015 Buick', it would loop through, and only display 2015 Buicks, not displaying elements on the based that had '2015' or 'Buick'.
The first word of the query is working just fine, it's when the value is greater than 2 words that everything gets complicated.
Working modifications here
There were a couple of issues:
if($('#searchText').val() > 2) caused the search to be parsed as a number and compared with 2, what you want is $('#searchText').val().length
the loops conceptually were inside-out: you want to find whether each row contains all terms, so the for loop should be inside the $.each.
unless you need case-sensitive matching, call toLowerCase() on the text and the search terms
I have the following data structure which describes an object and the time period which it's valid. Pretend the numbers below are unix timestamps.
{
"id": 1234,
"valid_from": 2000
"valid_to": 4000
},
{
"id": 1235,
"valid_from": 1000,
"valid_to": 2200,
}
...
I want to quickly be able to store these items in JavaScript and then query for items which are valid at a certain time.
For example if I were to query for objects valid at 2100 I would get [1234, 1235]. If I were to query for objects valid at 3999 I would get [1234], and at 4999 nothing.
I will have a size of about 50-100k items in the structure and I'd like fast lookup speeds but inserts, and deletes could be slower.
Items will have duplicate valid_from and valid_to values so it needs to support duplicates. Items will overlap.
I will need to be continually inserting data into the structure (probably by bulk for initial load, and then one off updates as data changes). I will also be periodically modifying records so likely a remove and insert.
I am not sure what the best approach to this is in a highly efficient manner?
Algorithms are not my strong suit but if I just know the correct approach I can research the algorithms themselves.
My Idea:
I was originally thinking a modified binary search tree to support duplicate keys and closest lookup, but this would only allow me to query objects that are > valid_from or < valid_to.
This would involve me bisecting the array or tree to find all items > valid_from and then manually checking each one for valid_to.
I suppose I could have two search trees, one for valid_to and valid_from, then I could check which id's from the results overlap and return those id's?
This still seems kind of hacky to me? Is there a better approach someone can recommend or is this how it's done.
Imagine you have two lists: initiation/begin and expiration/end. Both are sorted by TIME.
Given a particular time, you can find where in each list the first item is that meets the criteria by binary search. You can also do inserts by binary search into each list.
For example, if there are 1000 items and begin location is 342, then items 1-342 are possible, and if end location is 901, then items 901-1000 in the termination list are possible. You now need to intersect both sublists.
Take item IDs from 1-342 in begin and 901-1000 in end, and put them in a separate array (allocated ahead of time). Sort the array. Traverse the array. Whenever the same ID appears twice in a row, it is a hit, a valid match.
I have two issues that both resemble each other.
Each player has access to 50 inventory slots. 1 slot holds 1 item.
I figured that I can make a table of items. Items have qualities and ID's.
I have come to this solution:
Each character table can have a textbox that holds json data that is key value pairs of item slots [0-49] and item id's, since every item has a unique id.
However I am not sure this is the most elegant solution.
And then this problem, that should be solved the same way? [I think]
Each player has access to 50 skills. Each skill can grow 100 levels.
I wouldn't encode JSON data in a database -- you want a separate table to link things together.
So, maybe player_item_map which would have fields item_id, player_id (and maybe an internal id if you ever need to refer to the map itself). Then you link arbitrary numbers of items to users (you can use constraints in item ownership is unique, and enforce the item limit somewhere else). Alternatively prepopulate an inventory table with the slots, using the fields player_id, slot, item_id (can be null). Then you don't need to insert or worry about missing rows.
You can do similar for skills, but had a skill_level field as well.
Here is my Code
http://pastebin.com/RXUNxcct
What i would like to know is, how i can use my list to only affect the listed units?
If I understand correctly, here is some pseudocode:
I would first put both sets into two lists or arrays. One array/list for the total heroes, and one for the "to be buffed" heroes. I would then check each element of the total heroes list, and see if the current hero that you are working with (through a for-loop, perhaps?) is in the "to be buffed" list. If so, buff it.
Let me know if this helps you.
I'm trying to write some javascript that will stack objects by setting z-index.
Test Case:
http://christophermeyers.name/stacker/
I've hacked that together, but I'd like to extrapolate that behavior to something a little more logical. That is:
Given x number of elements, when element C is moved to the top, all elements above that element must move down 1, while all elements below that element should remain in place.
A "linked list" makes for a good data structure when you're doing this kind of thing. Keep track of the order of your stackable elements via a series of simple nodes..
// ListNode
{
value: {}
next: {<ListNode>}
}
..and update the sequence as new nodes are added or selected.
I have posted a working example of a list being used for depth sorting at the following URL:
http://aethermedia.net/sandbox/depth-sorting.html
Sorry I don't have time to pull up a more appropriate tutorial =/