@@ -199,22 +199,29 @@ void RT_API::insert_or_assign_edge_RT(Node &n, uint64_t to, const std::vector<fl
199199 auto rot_pack = rot_pack_o.value_or (std::vector<float > (BLOCK_SIZE * HISTORY_SIZE, 0 .f ));
200200
201201 auto timestamp_index = 0 ;
202- uint32_t index = 0 ;
202+ int index = 0 ;
203203 if (timestamp.has_value ()) {
204- std::vector<size_t > diffs;
204+ std::vector<int64_t > diffs;
205205 std::transform (time_stamps.begin (), time_stamps.end (), std::back_inserter (diffs),
206206 [t = *timestamp](auto &val) {
207- return (val - t > 0 ) ? (val - t) : std::numeric_limits<int >::max ();
207+ return (( int64_t )t - ( int64_t )val > 0 ) ? (( int64_t )t - ( int64_t )val) : std::numeric_limits<int64_t >::min ();
208208 });
209-
210-
211- auto pos = (((std::min_element (diffs.begin (), diffs.end ())) - diffs.begin ()) + 1 ) % HISTORY_SIZE;
212209
213- timestamp_index = (int )(head_o.value_or (0 )/BLOCK_SIZE+1 ) % HISTORY_SIZE;
214- uint32_t index = timestamp_index * BLOCK_SIZE;
210+ auto pos = (((std::min_element (diffs.begin (), diffs.end ())) - diffs.begin ())) % HISTORY_SIZE;
211+
212+ if (!head_o.has_value ()) {
213+ timestamp_index = 0 ;
214+ } else {
215+ timestamp_index = (int )(head_o.value_or (0 )/BLOCK_SIZE) % HISTORY_SIZE;
216+ }
217+
218+ index = timestamp_index * BLOCK_SIZE;
215219
216220 // too old to insert it
217221 if (pos == timestamp_index && *timestamp < time_stamps[pos]) {return ;}
222+ if (pos > timestamp_index) {
223+ pos = timestamp_index;
224+ }
218225
219226 time_stamps.erase (time_stamps.begin () + timestamp_index);
220227 tr_pack.erase (tr_pack.begin () + index, tr_pack.begin () + index + 3 );
@@ -230,8 +237,12 @@ void RT_API::insert_or_assign_edge_RT(Node &n, uint64_t to, const std::vector<fl
230237
231238
232239 } else {
233- timestamp_index = (int )(head_o.value_or (0 )/BLOCK_SIZE+1 ) % HISTORY_SIZE;
234- uint32_t index = timestamp_index * BLOCK_SIZE;
240+ if (!head_o.has_value ()) {
241+ timestamp_index = 0 ;
242+ } else {
243+ timestamp_index = (int )(head_o.value_or (0 )/BLOCK_SIZE) % HISTORY_SIZE;
244+ }
245+ index = timestamp_index * BLOCK_SIZE;
235246
236247 tr_pack[index] = trans[0 ];
237248 tr_pack[index + 1 ] = trans[1 ];
@@ -246,7 +257,7 @@ void RT_API::insert_or_assign_edge_RT(Node &n, uint64_t to, const std::vector<fl
246257
247258 CRDTAttribute tr (std::move (tr_pack), get_unix_timestamp (), 0 );
248259 CRDTAttribute rot (std::move (rot_pack), get_unix_timestamp (), 0 );
249- CRDTAttribute head_index (index, get_unix_timestamp (), 0 );
260+ CRDTAttribute head_index (index+ 3 , get_unix_timestamp (), 0 );
250261 CRDTAttribute timestamps (std::move (time_stamps), get_unix_timestamp (), 0 );
251262
252263 auto [it, new_el] = e.attrs ().insert_or_assign (" rt_rotation_euler_xyz" , mvreg<CRDTAttribute> ());
@@ -375,23 +386,31 @@ void RT_API::insert_or_assign_edge_RT(Node &n, uint64_t to, std::vector<float> &
375386 auto rot_pack = rot_pack_o.value_or (std::vector<float > (BLOCK_SIZE * HISTORY_SIZE, 0 .f ));
376387
377388 auto timestamp_index = 0 ;
378- uint32_t index = 0 ;
389+ int index = 0 ;
390+ bool update_index = true ;
379391 if (timestamp.has_value ()) {
380- std::vector<size_t > diffs;
392+ std::vector<int64_t > diffs;
381393 std::transform (time_stamps.begin (), time_stamps.end (), std::back_inserter (diffs),
382394 [t = *timestamp](auto &val) {
383- return (val - t > 0 ) ? (val - t) : std::numeric_limits<int >::max ();
395+ return (( int64_t )t - ( int64_t )val > 0 ) ? (( int64_t )t - ( int64_t )val) : std::numeric_limits<int64_t >::min ();
384396 });
385397
386-
387- auto pos = (((std::min_element (diffs.begin (), diffs.end ())) - diffs.begin ()) + 1 ) % HISTORY_SIZE;
398+ auto pos = (((std::min_element (diffs.begin (), diffs.end ())) - diffs.begin ())) % HISTORY_SIZE;
388399
389- timestamp_index = (int )(head_o.value_or (0 )/BLOCK_SIZE+1 ) % HISTORY_SIZE;
390- uint32_t index = timestamp_index * BLOCK_SIZE;
400+
401+ if (!head_o.has_value ()) {
402+ timestamp_index = 0 ;
403+ } else {
404+ timestamp_index = (int )(head_o.value_or (0 )/BLOCK_SIZE) % HISTORY_SIZE;
405+ }
406+ index = timestamp_index * BLOCK_SIZE;
391407
392408 // too old to insert it
393409 if (pos == timestamp_index && *timestamp < time_stamps[pos]) {return ;}
394-
410+ if (pos > timestamp_index) {
411+ pos = timestamp_index;
412+ update_index=false ;
413+ }
395414 time_stamps.erase (time_stamps.begin () + timestamp_index);
396415 tr_pack.erase (tr_pack.begin () + index, tr_pack.begin () + index + 3 );
397416 rot_pack.erase (rot_pack.begin () + index, rot_pack.begin () + index + 3 );
@@ -406,8 +425,12 @@ void RT_API::insert_or_assign_edge_RT(Node &n, uint64_t to, std::vector<float> &
406425
407426
408427 } else {
409- timestamp_index = (int )(head_o.value_or (0 )/BLOCK_SIZE+1 ) % HISTORY_SIZE;
410- uint32_t index = timestamp_index * BLOCK_SIZE;
428+ if (!head_o.has_value ()) {
429+ timestamp_index = 0 ;
430+ } else {
431+ timestamp_index = (int )(head_o.value_or (0 )/BLOCK_SIZE) % HISTORY_SIZE;
432+ }
433+ index = timestamp_index * BLOCK_SIZE;
411434
412435 tr_pack[index] = trans[0 ];
413436 tr_pack[index + 1 ] = trans[1 ];
@@ -423,15 +446,17 @@ void RT_API::insert_or_assign_edge_RT(Node &n, uint64_t to, std::vector<float> &
423446
424447 CRDTAttribute tr (std::move (tr_pack), get_unix_timestamp (), 0 );
425448 CRDTAttribute rot (std::move (rot_pack), get_unix_timestamp (), 0 );
426- CRDTAttribute head_index (index, get_unix_timestamp (), 0 );
449+ CRDTAttribute head_index (index+ 3 , get_unix_timestamp (), 0 );
427450 CRDTAttribute timestamps (std::move (time_stamps), get_unix_timestamp (), 0 );
428451
429452 auto [it, new_el] = e.attrs ().insert_or_assign (" rt_rotation_euler_xyz" , mvreg<CRDTAttribute> ());
430453 it->second .write (std::move (rot));
431454 std::tie (it, new_el) = e.attrs ().insert_or_assign (" rt_translation" , mvreg<CRDTAttribute> ());
432455 it->second .write (std::move (tr));
433- std::tie (it, new_el) = e.attrs ().insert_or_assign (" rt_head_index" , mvreg<CRDTAttribute> ());
434- it->second .write (std::move (head_index));
456+ if (update_index) {
457+ std::tie (it, new_el) = e.attrs ().insert_or_assign (" rt_head_index" , mvreg<CRDTAttribute> ());
458+ it->second .write (std::move (head_index));
459+ }
435460 std::tie (it, new_el) = e.attrs ().insert_or_assign (" rt_timestamps" , mvreg<CRDTAttribute> ());
436461 it->second .write (std::move (timestamps));
437462 }
0 commit comments