[personal profile] aklepatc
Code a data structure with 2 operations: push() and popMostFrequent().

Most of the meat is in the comments/discussion below.

Date: 2024-05-09 07:35 am (UTC)
From: [personal profile] sassa_nf
Yes, almost. The difference really isn't the memory retention, but when reallocation happens and what the footprint requirement is.

I.e if you pop from the back, you retain just as much memory as when you pop from the front. The difference is that if you pop from the front, this will trim to size upon the next push, but if you pop from the back, you will retain the historically biggest array.

But we can't really tell how that works, because it is a slice, an abstraction. So it may well be a ring buffer underneath, why not?

Of course, the experiment shows that the implementation isn't a ring buffer:

https://tio.run/##bY7NCsIwEITv@xTLnhIoUvFW6Dt4Lz3E/qXUbENNQSM@e4ypCIqngfm@gRnmEKxqJjV0aNTIAKOx8@IEUm8coQToV24SExLvgIgGizIWUyeq@uKWkYcM8wwPcoMlKms7boXJkNpZUwx1aihhTttqX9R/5OvNv60qryMi7z1tq4/H0dNaf3tudQSvIn7eHeMjd2Zh5G/DEh4hPAE

Append behaviour was surprising, though. I.e. that appending to n modified m.

Date: 2024-05-09 04:34 pm (UTC)
From: [personal profile] sassa_nf
Well, it is surprising to those familiar with java byte buffers. Slicing a buffer there creates a slice of the underlying buffer, but does not share its limit - can't go beyond the last byte available in the buffer at the time of slicing, even if the capacity allows for that.

So to speak, cap(n)==len(n) immediately after slicing.

Profile

aklepatc

May 2024

S M T W T F S
   1234
567 891011
12131415161718
19202122232425
262728293031 

Most Popular Tags

Page Summary

Style Credit

Expand Cut Tags

No cut tags
Page generated Jun. 16th, 2025 03:55 pm
Powered by Dreamwidth Studios