I'm not very good at manipulating arrays in PHP, so I have a simple porting question. In C ++, I have a map std::map<int, int>for which implicit key ordering is an important part of the structure. What I want to do is summarize all the values for the initial key range, which I really like:
int pair_adder(int n, const std::map<int, int>::value_type & p) { return n + p.second; }
int total_value_up_to_time_N(int N)
{
return std::accumulate(mymap.begin(), mymap.upper_bound(N), 0, pair_adder);
}
What would be the idiomatic way of writing this data structure and battery into PHP?
Explain the context: the data structure is a simple time series, and I want to know how much I accumulated at a point in time N. C ++ is mapalways sorted by key, so I can add elements mymap[time] = value;in any order, and the map always contains elements in order of time.
To explain the accumulation: the function accumulatesums up all the values of the card, the keys of which are no more N. For example, take this map:
mymap = { { 1, 20}, {2, 30}, {3, -10}, {4, 15} };
Then for N = 2I accumulate 50, for N = 3I accumulate 40, and for N = 12accumulate 55.
Update: I just realized that there really is no reason why each timestamp should appear only once, so there should be a data structure std::multimap<int, int>. One and the same accumulation function works verbatim, but if it takes time for a PHP solution to be an array key, this will no longer work. But this is not strictly important; I believe that a solution in which each time is required to be unique will be enough.