I have a TList which' items are continuously processed by many for-loops. I sometimes need to exchange items in the list in order to rearrange the order of the visual representation of the list (in a StringGrid).
How do I exchange these items?
My preliminary thoughts are:
During the for-loop I think the items should not be exchanged.
If I do the exchange in a Timers' OnTimer event, having set the Timers' interval to a very short interval (e.g. 1 millisecond), then I think the for-loop will have only an intermission of that one millisecond.
Will this work? Or are there better alternatives?
As long as you ensure that the count of the items in a TList does not change, exchanging items is perfectly fine during a for-loop. Note that, depending on the index of the items that are about to be exchanged, some of the items may not be processed or may be processed twice.
If the exchange operation is not called from within the for-loop, then an already started for-loop will run until it is done. You cannot expect to "break in" with a Timer, because that Timer's message will not be processed until the for-loop and all surrounding code is done.
So, the solution for your problem could be:
exchange the items within the for-loop,
use a threading solution to be able to do two different things simultaniously on one list (this may need some learning about threads),
wait until the for-loop is done, and exchange then,
split the for-loop in multiple slices to reduce the time needed, or
use a timer to start multiple for-loops in order to give your program some breathing time in between.
Related
I'd like to measure how many events arrive within allowed lateness grouped by particular feature of the event. We assume particular type of events have way more late arrivals and would like to verify this.
The place to make the measurement I thought of is our custom trigger within onElement method as this is the place where we know whether event is late of not. Yet in case of SlidingEventTimeWindows that means that a single element can be counted multiple timess if it's late by more than a slide.
Any suggestions?
You might do this separately from the windowing. You could set the allowed lateness to zero, and divert all late events to a side output. You can then key that stream of late events by the feature(s) of interest, and use a RichFlatMapFunction or KeyedProcessFunction to count the events, which can then be reported as a custom metric, or sent to a sink.
I'm using Firebase for my iOS application and I'm having trouble implement infinite scroll and filtering data together.
What I need to do is:
Display items with order/filter on multiple property (location, category, status . . .)
Implement infinite scroll when the user scrolled to bottom of the screen.
I tried to think about some solutions:
The first, I think that I'll query the data with the necessary conditions then limit the number of records by use queryLimitedToFirst(N), and increase N when need to load the next items. But because Firebase can only filter on one property at a time and it's also a waste to reload data. So, I was thinking about the second solution.
As approaches are suggested from Frank van Puffelen (Query based on multiple where clauses in firebase):
filter most on the server, do the rest on the client
Yes, exactly like that. I'll execute queryOrderedByKey, queryStartingAtValue, queryEndingAtValue to implement infinite scroll, pull down the remaining data and filter that on client. But there is one problem that is I would not have enough items to display for the user if execute filter on the client.
For example: each time run the query, I receive 10 items. After data filtering process on the client, I just left 5 (can be 0) items meet the conditions to display to the user.
I don't want this because user may think there is a problem
Can I please get some pointers on this? If I didn't structured the data properly, can I also get some tips there?
I am using CMStepCounter and CMMotionActivityManager.
What I would like to do is work out my total walking time throughout the day, and from this, my average speed.
However, looking at the data in CMMotionActivityManager, it is clear than a number of steps throughout the day are actually logged during periods that are of 'unknown activity' and not walking or running. This does make sense, as you need to do a handful of steps for iOS to know you are walking. However, these add up over the course of any given day.
Querying CMMotionActivity, it is possible to get the timestamp of every event. However, whilst it is clear to me that every step must be timestamped in CMStepCounter, I can only see a method to return the total number of steps between two points in time. What would be great is if I could return an array of every step with its time stamp, and if so, how?
Many thanks.
With the current Core Motion API, you can't get an array of every step with its time stamp directly.
But you can use queryActivityStartingFromDate:toDate:toQueue:withHandler: to get an array of CMMotionActivity objects. Then use the time stamps to calculate the number of steps by calling queryStepCountStartingFrom:to:toQueue:withHandler:.
I don't think the M7 processor stores every step with its own time stamp. If you pass 1 as stepCounts to the method startStepCountingUpdatesToQueue:updateOn:withHandler:, you'll notice the handler is not executed on every step. Like the document says: The handler block is executed on a best effort basis each time the step count threshold is exceeded.
So I have some questions concerning the solution to the problem of scheduling n activities that may overlap using the least amount of classrooms possible. The solution is below:
Find the smallest number of classrooms to schedule a set of activities S in. To do this efefficiently
move through the activities according to starting and finishing times. Maintain two lists of classrooms: Rooms that are busy at time t and rooms that are free at time t. When t is the starting time
for some activity schedule this activity to a free room and move the room to the busy list.
Similarly, move the room to the free list when the activity stops. Initially start with zero rooms. If
there are no rooms in the free list create a new room.
The algorithm can be implemented by sorting the activities. At each start or finish time we can
schedule the activities and move the rooms between the lists in constant time. The total time is thus
dominated by sorting and is therefore O(n lg n).
My questions are
1) First, how do you move through the activities by both starting and finishing time at the same time?
2) I don't quite understand how it's possible to move the rooms between lists in constant time. If you want to move rooms from the busy list to the free list, don't you have to iterate over all the rooms in the busy list and see which ones have end times that have already passed?
3) Are there any 'state' variables that we need to keep track of while doing this to make it work?
The way the algorithm works, you need to create a list containing an element for each start time and an element for each end time (so 2n elements in total if there are n activities). Sort this list. When an end time and a start time are equal, sort the end time first -- this will cause back-to-back bookings for halls to work.
If you use linked lists for holding the free and booked halls, you can have the elements you created in step 1 hold pointers back to an activity structure, and this structure can hold a pointer to the list element containing the hall that this activity is assigned to. This will be NULL initially, and will take on a value when that hall is used for that activity. Then when that activity ends, its hall can be looked up in constant time by following two pointers from the activity-end element (first to the activity object, and from there to the hall element).
That should be clear from the above description, hopefully.
I am running a hittest on an array of thousands of MCs a little a part, due to the nature of this sometimes two can be hit at once.
How would I narrow it down so that the one which is hit the most is the one returned value?
Have a specific variable increment each time a specific movieclip is hit.
Place all of these variables in an array.
sortOn the array for DESCENDING order.
select the first element in the array (myArray[0]), as that will have the highest value