简体   繁体   English

C ++ Viewport缓存目录中的文件

[英]C++ Viewport caching files in a directory

In my C++ app, I have a table widget used to display a list of files in a directory. 在我的C ++应用程序中,我有一个表小部件用于显示目录中的文件列表。 There is enough screen real estate to display ~20 files at a time. 有足够的屏幕空间可以一次显示~20个文件。 It is wrapped around a viewport so the user can scroll up and down to navigate to other files in the list. 它包裹在视口中,因此用户可以向上和向下滚动以导航到列表中的其他文件。

Each file in the list contains metadata of arbitrary size which is cached in a background thread. 列表中的每个文件都包含任意大小的元数据,这些元数据缓存在后台线程中。 Attempting to cache all files in a directory containing lots of metadata rich files quickly hoovers up available memory on the heap, with severe consequences. 尝试将所有文​​件缓存在包含大量元数据丰富文件的目录中会迅速将堆上的可用内存丢失,从而造成严重后果。

Of course, I don't need to cache all the files at once; 当然,我不需要一次缓存所有文件; only those that are visible. 只有那些可见的。 It would also be nice if those files containing metadata immediately above/below the viewport bounds were cached, so that scrolling up and down wouldn't incur too much of a delay. 如果那些包含紧靠视口边界上方/下方的元数据的文件被缓存,那么上下滚动不会产生太多延迟也是很好的。

I guess this is related to a problem that many UI applications containing viewports (web browsers, text/ hex editors) have to solve. 我想这与包含视口(Web浏览器,文本/十六进制编辑器)的许多UI应用程序必须解决的问题有关。 Are there any patterns, algorithms or containers ( deque springs to mind) that would help in solving this problem? 是否有任何模式,算法或容器( deque spring to mind)有助于解决这个问题?

Thanks! 谢谢!

If your data is in eg a std::vector , then you can have a first_visible and last_visible variables that are indexes into the vector. 如果您的数据位于例如std::vector ,那么您可以使用first_visiblelast_visible变量作为向量的索引。 The items in the vector between those two (inclusive) will be the ones you need to cache (plus X extra on each side). 这两个(包括)之间的向量中的项将是您需要缓存的项(每侧加上X额外)。

Whenever the user changes the UI element to display new items, those indexes are updated, and new data is loaded if needed. 每当用户更改UI元素以显示新项时,都会更新这些索引,并在需要时加载新数据。

您可以查看FlyweightProxy设计模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM