AngularJS - Infinte Loop over a finite array - arrays

I have a list of anything from 10-500 records appearing on screen as a graphical array using standard NG-REPEAT, visualisation is a row of photographs
What I would like to achieve, but cant quite get my head around if its even possible is this...
A 'window' on screen that will only show 10 records - Done
You can scroll left or right through the entire list - Done
HOWEVER
What i would LIKE to achieve is that when the end of the list is reached, the list starts again from the beginning but in an infinite style loop. Both directions
I have never done anything like this and not quite sure how I would achieve this of my ng-repeat (item in ListofItems)
So I assume that the best solution is
when the user gets 40 records through 100 records, i push() the first 20 records from my $scope.ListofItems to the end of the array, and remove the equivalent first 20 records from the front.. this keeps a constant list of 100 records, no massive memory usage.. i can even do this on an individual record basis.. remove one record from end or beginning and add at the beginning or end of the list.
The user experience is an infinite scroll, but I suspect the browser experience could be slow and stuttering due to the processing going on
Push(), Pull(), Tracking and indexing would play a part but any suggestions would be appreciated whether this is even technically possible
EDIT : After some research. maybe
$scope.ListofItems.push($scope.ListofItems.shift());
could be a solution to move beginning to end, but not sure how to trigger this or go the other way (pull?)
EDIT2 : Just did a manually called function for the above and it shifts front item to end of list, though i would have no clue how to read the screen position to know when to fire the function

Going the other way around could be
$scope.ListofItems.unshift($scope.ListofItems.pop());
As a side note, I think it's important to keep in mind for large arrays that shift() and unshift() naturally cause a full reindexing and have a time complexity of O(n) where n is the length of the array, as opposed to push() and pop() which have a time complexity of O(1)

Related

Dynamic Array of flash AS3

I have an array to store true answers and false answer of random frame multiple choices questions:
var arraytruefalseanswer=[];
I use push method to insert every true and false answer in the array:
arraytruefalseanswer.push(trueanswer)
arraytruefalseanswer.push(falseanswer)
The problem is:
I can not remove the last element of arraytruefalseanswer .
Because
If I use pop method arraytruefalseanswer.pop(),
it will remove all elements in the array arraytruefalseanswer or bring back to
arraytruefalseanswer=[]
If i use delete, it is still leaving null.
Please help... how can I remove the last element of arraytruefalseanswer using flash AS3?
Thank you.
The documentation says: When you do a pop(), it returns items that were popped, and the array gets modified as a side effect. Therefore, in order to just remove the last element, you call arraytruefalseanswer.pop(); as is. You can use trace(arraytruefalseanswer) to verify if anything popped. Also check your code flow, it's possible that when you're popping your last element, you think the code reaches the call once but it's not so, and it say pops your entire array so you've left with an empty array. I can't say more without ever seeing your entire block of code where you work eith your truefalseanswer.

removeFromParent is not working in when having other functions

I am trying to write a code where balls are dropped and as soon as they get below half of the screen they get removed, so that nodes don't keep pilling up.
As the balls are generated, they are being added to a nodes array "balls", then I am removing them as needed with a for loop.
The interesting part is that as long as I do not have any other action in the loop, nodes are being removed properly. However when I add the flyPoints, nodes just keep on pilling up and and no longer get removed, no error though. Does anyone know why? At the same time I need to keep the fly points function in the for loop and remove the nodes.
Thanks
for ball in balls {
if ball.Ball.position.y < self.frame.height/2 {
flyPoints(location: ball.position(), points: "+1", view: self)
ball.Ball.removeFromParent()
balls.removeFirst()
}
}

Solve maze using queue data structure?

I'm taking a class in data structures and was given the assignment to find the shortest path through a maze using C and implementing the queue data structure. However, I can't really wrap my head around how to use a queue here.
I know the idea is to count every possible move from the start position, and when you hit the target, you're supposed to trace back to the initial position. This is what I don't understand. Because if I use a queue and delete all the moves that leads up to the target, I have no data to use to do the trace back, and if I don't delete the moves that lead to the target (i.e. saving all the possible moves and deleting them when I actually do the trace back), I might as well be using a stack.
I know there's something I don't quite get, but I can't figure out what it is. How would I utilize the queue data structure in this case?
What your professor is trying to get you to use is called "breadth-first search". The queue comes in for deciding which spaces to explore next. When you are looking at the possible paths to take, you enqueue all the paths you have yet to explore. Instead of continuing down the path you're on (which would be "depth-first search"), you dequeue the next spot you need to check, which will take you back to one of the positions you were considering earlier.
The actual implementation is up to you, I'd recommend looking for examples of breadth-first search online.

Compare changes among an array of hashes

I have an array where each element is a hash representing a simplified version of an entire Chess Board. I am trying to implement the fifty-move draw rule which states a draw can be claimed if in fifty moves, no piece has been captured and no pawn has moved.
In doing so, I'm trying to keep DRY and use code that I've already implemented for another draw scenario, which is currently working properly.
A new "snapshot" of the board is saved after each turn and looks like the following (after a pawn has moved from "a2" to "a4" on the first turn):
board_snapshot = [{
"a1"=>"Rook", "a2"=>nil, "a3"=>nil, "a4"=>"Pawn", "a5"=>nil, "a6"=>nil,
"a7"=>"Pawn", "a8"=>"Rook", "b1"=>"Knight", "b2"=>"Pawn", "b3"=>nil,
"b4"=>nil, "b5"=>nil, "b6"=>nil, "b7"=>"Pawn", "b8"=>"Knight",
"c1"=>"Bishop", "c2"=>"Pawn", "c3"=>nil, "c4"=>nil, "c5"=>nil, "c6"=>nil,
"c7"=>"Pawn", "c8"=>"Bishop", "d1"=>"Queen", "d2"=>"Pawn", "d3"=>nil,
"d4"=>nil, "d5"=>nil, "d6"=>nil, "d7"=>"Pawn", "d8"=>"Queen", "e1"=>"King",
"e2"=>"Pawn", "e3"=>nil, "e4"=>nil, "e5"=>nil, "e6"=>nil, "e7"=>"Pawn",
"e8"=>"King", "f1"=>"Bishop", "f2"=>"Pawn", "f3"=>nil, "f4"=>nil,
"f5"=>nil, "f6"=>nil, "f7"=>"Pawn", "f8"=>"Bishop", "g1"=>"Knight",
"g2"=>"Pawn", "g3"=>nil, "g4"=>nil, "g5"=>nil, "g6"=>nil, "g7"=>"Pawn",
"g8"=>"Knight", "h1"=>"Rook", "h2"=>"Pawn", "h3"=>nil, "h4"=>nil,
"h5"=>nil, "h6"=>nil, "h7"=>"Pawn", "h8"=>"Rook"
}]
In pseudocode, I'm thinking of implementing this fifty move rule check by creating a method which looks at the previous fifty board snapshots to see if the amount of nil values are the same (no piece captured) and if so, somehow looking to see that each of the Pawns are on the same square.
I've found a way to compare two boards to see if the nil values are the same:
board_snapshot[index].values.count(nil) == board_snapshot[index + 1].values.count(nil)
However, I'm still having trouble coming up with a way to iterate over 50 board "snapshots" to run this test on each one. Also not sure how to iterate over the 50 "snapshots" to ensure that no Pawn has moved.
If it would just be easier to implement this rule by creating a "counter" which resets when a piece is captured and when a Pawn is moved let me know, I was trying to be efficient and utilize code that was already around.
I think #sawa has the right idea in the comments. You only need to check that the Pawns are in the same position as they were 50 moves ago (since pawns can't move backwards, they can't move in one snapshot and be returned in the next)
board_snapshot.last.delete_if{|_,v| v != "Pawn"} == board_snapshot[-50].delete_if{|_,v| v != "Pawn"}
Similarly (and using your suggested code)
board_snapshotlast.values.count(nil) == board_snapshot[-50].values.count(nil)
Since pieces can't be added to the board, you don't need to worry about a piece disappearing in one move and reappearing in the next move.

How many rows should be in the (main) buffer of a virtual Listview control?

How many rows should be in the (main) buffer of a virtual Listview control?
I am witting an application in pure 'c' to the Win32 API. There is an ODBC connection to a database which will retrieve the items (actually rows).
The MSDN sample code implies a fixed size buffer of 30 for the end cache (Which would almost certainly not be optimal). I think the end cache and the main cache should be the same size.
My thinking is that the buffer should be more than the maximum number of items that could be displayed by the list view at one time. I guess this could be re-calculated each time the Listivew was resized?
Or, is it just better to go with a large fixed value. If so what is that value?
Use the ListView_ApproximateViewRect (or the LVM_APPROXIMATEVIEWRECT message) to get the view rect height.
Use the ListView_GetItemRect (or the LVM_GETITEMRECT message) to get the height of an item.
Divide the view rect height by the height of an item to get the number of items that can fit in your view.
Do this calculation on each size event.
Then create your buffer accordingly.
The LVN_ODCACHEHINT notification message will let you know how many items it is going to ask. This could help you in deciding how big your cache should be.
#Brian R. Bondy Thanks for the explicit help for how to do get the number of items. In fact I was all ready working my way to understanding that it could be done (for list or report view) with ListView_GetCountPerPage, and I would use you way to get it for for the others, though I don't need the ListView_ApproximateViewRect since I will all ready know the new size of the ListView.
#Lars Truijens I am already using the LVN_ODCACHEHINT and had though about using that to set the buffer size, however I need to read to the end of the SQL data to find the last item to get the number of rows returned from ODBC. Since that would be the best time to fill the 'end cache' I think I have to set up the number of items (and therefore fill the buffer) before we get a call to LVN_ODCACHEHIN.
I guess my real question is one of optimization for which I think Brian hinted at the answer. The amount of overhead in trashing your buffer and reallocating memory is smaller than the overhead of going out to the network and doing an ODBC read, some make the buffer fairly small and change it often rather.
Is this right?
I have done a little more playing around, and it seems that think that LVN_ODCACHEHINT generally fills the main buffer correctly, and only misses if a row (in report mode) is partially visible.
So I think the answer for the size of the cache is: The total number of displayed items, plus one row of displayed items (since in the icons views you have multiple items per row).
You would then re-read the cache with each WM_SIZE and LVN_ODCACHEHINT if either had a different begin and end item number.
The answer would seem to be: (Or a random collection of notes as I fiddle around with ideas)
As a general answer for buffers:
Start with some amount, in this case a screen full (I add an extra row in case the next is partially uncovered), and then every time the screen is scrolled, double the buffer size (up to the point before you run out of memory).
Which would seem to be wrong. As it turns out, most ways of loading data are all ready buffered. ODBC calls of File I/O. Pretty much anything that isn't that I can think off is either in memory or is recalculated on the fly. This means the answer really is: take the values provided in LVN_ODCACHEHINT (and add 1 either side - this just seems to work faster if you don't have an integral height).

Resources