I have a uint8 array that in the first position have the value: 0xb00000000. If i console.log(uint8[0]) i receive 0 in the console.
I know if i do uint8[0] = uint8[0] ^ 0b00000001, the last bit will change to 1, then if i execute again console.log(uint8[0]) the console logs 1.
My question is: How can i get all 256 possible values using that same way to change the bits?
Is there a formule to do this?
Basically i need the console to show:
1
2
3
4
5
...
I have an x-y grid stored in a multi-dimensional array. Each point in the x-y grid has a value.
Example:
var xy = [
[0,3,1,1,0],
[0,0,2,2,1],
[0,0,1,1,0]
];
Assuming the layout of var xy is like an x-y grid (x 1 and y 2 would be 3 for example.
Here's a larger 'print-out' of such a variable, with greater height and width:
(1) (2) (3) (4) (5) (6) (7) (8) (9) (10) (11) (12) (13)
(1) 0 0 0 1 1 1 2 2 1 1 0 0 0
(2) 0 0 1 1 1 2 2 3 2 2 1 0 0
(3) 0 0 0 1 2 2 3 3 2 1 0 0 0
(4) 0 4 0 1 1 1 2 2 1 0 0 0 8
(5) 0 0 0 0 0 0 1 1 0 0 0 0 4
(6) 0 0 0 0 9 9 9 0 0 0 0 0 0
(7) 0 0 0 0 9 9 0 0 0 0 0 0 0
Now, for the sake of example, pretend that the above layout were a map, like a chessboard. For starters, to pay no attention to the VALUES of each point on the grid, let's say we want to know which squares a 'game piece' could reach from, say, x4 y8....we could do something like this in js:
var n = ,//total # of items in grid
a = ,//a certain point on the grid
r = 5; //range of movement
for(var i = 0; i < n; i++){ //this would be a double for or something similar to go through the grid properly, but let's pretend i is a given point on the grid.
Math.abs(((a.xLocation + a.yLocation) - (i.xLocation + i.yLocation)) <= r) //if the positive difference between the current point in the loop and the location of the certain point is less than or equal to the range....true!
}
So in the rough example above a thing at point A can move 5 steps, diagonally, vertically, horizontally, whatever. We don't know the direction, just the potential for movement.
That's pretty easy to figure out. The part I'm still trying to get my head around is this: How do you know whether a thing at point a can reach point i based on the VALUES of the grid. 0 means no augmentation, but 1 or 2 takes 1 or 2 or whatever EXTRA the movement to get to.....how do you figure that out for EACH point on the map? Again, no knowing which direction or path - whether it will be optimal or not.
CLARIFICATION: Imagine a chessboard. Each square chessboard has a value that represents how many EXTRA movement points it takes to get there. A given piece, not at all using chess rules, can move, let's say, 5 'movement points' in any direction. Many squares have a value of 0, thus it requires no further expenditure of movement points. But a square of 1 would require 2 total movement points, 2 would require 3 total, etc. Really, you could just as easily add 1 to all the squares below to find out whether a piece in a neighboring square could move there. However you like. I'm just looking for some kind of forumula or suggestion that can derive an answer. Here, look at this much simpler example.
(1) (2) (3)
(1) 0 1 3
(2) 1 X 2
(3) 2 0 1
Think of it like a game, where each square represents some kind of terrain disadvantage. Some paths are easier than others, but others are more direct. You can take any path to get to a certain square, but before the move, which squares are legal and which are not? So our piece is on X2 Y2, right? He has 5 movement points. He wants to know which ones he can move to. Well, he can move to any of them. But X1Y1 will cost 1 movement point, X1Y2 will cost 2, X1Y3 will cost 4, etc etc etc. Easy to figure out. but if the board is larger, and each potential (unknown) movement takes points, which squares can he move to and which can't he? Does this make sense?
EDIT 2: A slightly more complex example:
(1) (2) (3) (4) (5)
(1) 0 1 3 0 0
(2) 1 X 2 1 0
(3) 2 0 1 0 0
(4) 1 0 0 1 3
(5) 0 0 0 0 4
So our piece in this example is in X2Y2 again, but he wants to know, for each square, if he can make it there - boolean, yes or no. It's easy with just nine squares, but as the grid grows, so does the complexity. I can do it manually of course - can he reach X4Y4? Yes. But programmatically, how do I get this?
EDIT3: The size of the grid is meaningless, I just realized. It's really the size of the range. E.g., if the range of movement is 5, I just need to figure out the viability of squares five squares out in each direction. So that simplifies it a bit.
EDIT4: I think I have a little better of an idea. Look at the outermost ring 5 out. Is it greater than 0? Then no. Next outermost ring (4 out). Greater than 1? No. Next outermost ring. Greater than 2? Then no. Etc. Will that work or could that lead to incorrect results?
Answers (or even just leads in the right direction) in js or jQuery preferred, but even if you can kind of work through the logic, I can translate that into js or jquery.
I think what you want to do is a basic kind of search, where on each iteration you check the surrounding squares. I've come up with a mockup example with this jsfiddle. Open the console, click run, and it should print out the example map and the places it can get to in 3 steps from (2, 2).
There's some extra code in there for setup, but the main code is the search_rec function:
function search_rec(
map, // The input 'difficulty' map
output, // The output map (puts '1's in spots that can be reached)
// Should be the same size as the input map
x, y, // The current/starting position
limit) { // The number of spaces you are allowed to move
// If the number of spaces allowed to move is negative, then we can't
// reach this position, so we stop
if (limit < 0) {
return;
}
// Otherwise, if the limit is >= 0, then we can make it here and we
// store that in the output
output[y][x] = 1;
// Now, for each of the four directions
// Check if we're at a map boundary
if (x > 0) {
// If we're not, then we recurse, moving our starting point in
// the given direction, and lowering our limit by 1 (for the
// base movement) as well as the 'difficulty' of the terrain of
// the space we're moving into
search_rec(map, output, x - 1, y,
// ^ change position
limit - 1 - map[y][x - 1]);
// lower limit ^ by the base ^ and the difficulty ^
}
if (x < map[0].length - 1) {
search_rec(map, output, x + 1, y, limit - map[y][x + 1] - 1);
}
if (y > 0) {
search_rec(map, output, x, y - 1, limit - map[y - 1][x] - 1);
}
if (y < map.length - 1) {
search_rec(map, output, x, y + 1, limit - map[y + 1][x] - 1);
}
}
Hopefully that logic makes sense, and it's actually solving the problem you wanted solved.
I have a working app that needs to store up to 4 matrixes of integer data per record. I'm not sure how to get there with Titanium and SQlite.
A record will contain at least 1 but up to 4 matrixes of integers:
The matrix size is variable, each matrix consists of:
1 - 20 rows with 3 columns per row
OR
1 - 20 rows with 6 columns per row
The matrix structure will be identical for each record, i.e. 3 3x20 matrixes in
a record or 4 6x10 matrixes in a record. At this point my app starts, allows the user to choose the matrix parameters then accepts the data entry to fill in the matrix values. The matrixes are actually an JS array of arrays. How can I store an array of arrays and read it back in when I need to?
Edit: Let me see if I can clarify...
The app I'm working on is a scorecard for archery tournaments, similar in concept to a scorecard in golf. In archery you shoot for a set number of ends with a set number of arrows shot per end. The app asks for the number of ends (up to 20) and the number of arrows shot per end (3 or 6). After each shot the archer enters the score (an integer value). so for argument's sake say we're scoring for three ends with three arrows per end. We might see something like this:
arrow scores
8 8 9 (end 1)
7 9 10 (end 2)
9 9 10 (end 3)
There's my matrix that I need to save for this individual record. However, the next tournament I need to score may have a different number of ends and arrows:
arrow scores
7 8 9 10 10 10 (end 1)
10 9 9 7 8 10 (end 2)
9 6 6 6 9 9 (end 3)
7 8 6 7 8 8 (end 4)
10 10 9 8 8 8 (end 5)
Let's simplify and say that I want to store the scorecard for one archer per record. I already have my data entry and score tabulation working. I just don't understand the best way to store matrices as illustrated above.
I would recommend against storing an array of arrays. Generally speaking, its not terribly efficient to write abstractions with code that are meaningful for reasoning about matrices when make array of arrays a firm concept. The only exception I've seen is matlab/octave.
I've always found I end up with simpler code when I flatten the data. With a flat array you'll have to manage the indexing yourself. A few helper functions make that simple to reason about.
I'm not given much info to go on, but I'd assume that putting the data into two different tables will make things simpler.
CREATE TABLE mat3x20 (i1j1, i1j2, i1j2 ....
CREATE TABLE mat6x10 (i1j1, i1j2, i1j2 ....
Otherwise you have some weird behavior flag in the data which will make it less obvious what the code is supposed to be doing somewhere in the call stack.
I want to find an index making the less possible yes/no questions in a x^2 array, for this I assumed a cool approach will be to find the col position by cutting at the half (or near the half) and asking is col <= half? depending on the answer, I will pick one of the halves and repeat this until the length == 1, then make the same but with is row <= half? to find the row.
This is my function (first time attempting a recursive function):
function recursiveFunc(indexPos, currentArrLen, moveIndexBy, countLoops){
var aproxHalf, relativeIndexPos;
relativeIndexPos = (indexPos-moveIndexBy);
aproxHalf = Math.floor(currentArrLen/2);
if(currentArrLen<2){
return (moveIndexBy+" "+countLoops);
}else{
countLoops++;
if(relativeIndexPos>=aproxHalf){
moveIndexBy += aproxHalf;
currentArrLen -= aproxHalf;
}else{
currentArrLen = (aproxHalf-moveIndexBy);
}
return recursiveFunc(indexPos, currentArrLen, moveIndexBy, countLoops);
}
}
The only var that seems non self explanatory might be relativeIndexPos, so I will explain it, the value of it is the index of the index we are trying to find but within only the smaller array (e.g if we had 5x5 finding index 2, the new array length after cutting it once is 3, the relative index of 2 in that array [0,1][<2>,3,4] is 0)
edit: okay maybe I should explain moveIndexBy, it basically is "the leftest index at the current working array"
It works on a 5x5 array, for example if I give x the values of 0 to 4 in recursiveFunc(x,5,0,0);, it will correctly find the index in the less possible questions <index/questions> 0:2, 1:2, 2:2, 3:3, 4:3.
But this fails with bigger arrays, for example, a 10x10 will give:
0 3
1 3
2 3
3 4
4 4
5 2
5 2
7 3
8 4
9 4
The 5 and others are wrong, it can't possibly find 5 in 2 steps: 0 1 2 3 4 (5 6 7 8 9) then 5 6 (7 8 9) you still need to see if the index is the left or right of 5 (6). Also it fails to even find the index 6
currentArrLen = (aproxHalf-moveIndexBy);
to
currentArrLen = aproxHalf;
I don't know what or why, but that fixed it:
0 3
1 3
2 3
3 4
4 4
5 3
6 3
7 3
8 4
9 4
edit: with that, an undocumented bug of length being sometimes 0 is fixed too, so change if(currentArrLen<2) to if(currentArrLen==1)
Need some javascript help.
I have a table in a database that tells me which web elements to show, based on a key numeric property. Based on the key, there are 4 boolean values. Currently, I do a ajax postback to the server but need something faster.
I want to create a function where I pass the key value and I get the 4 values back without a postback. The values are static. Some of my users will be on a slow connection and almost all visit the site frequently, so I could gain by caching.
What's the best way to do this?
Here is a sample of the data:
Key Value1 Value2 Value3 Value4
100 0 0 0 1
143 1 0 1 0
785 1 1 1 0
2654 0 1 0 1
2699 1 1 0 1
The table has about 500 combinations.
You can create many static files - every contain JSON for particular key and named as this key.
If you have big budget and need REALLY HIGH PERFOMANCE you can use inmemory database like timesten and write custom module for nginx. This will allow to get about 100k reqs/second on average desktop pc with keep-alive.