如何为文档中的每个对象赋予唯一的ID?
我正在制作一个位图编辑器,其中文档由多个层组成,其中每个层代表一个位图。与文档中当前存在的所有其他图层相比,每个图层都必须具有唯一的 ID。我还需要考虑到需要保存和加载文档以及图层 ID。
我使用命令模式来存储对文档执行的操作,并使用唯一 ID 来跟踪应在哪一层上执行操作。
目前,我只保留一个名为 X 的计数器,当创建新层时,其 ID 设置为 X,然后 X 递增。加载时,我需要确保 X 设置为适当的数字,以便为新图层提供唯一的 ID,即我可以保存 X 的值并恢复该值,或者根据加载的最大图层 ID 设置 X。
鉴于 X 是一个 32 位数字,用户需要在 ID 开始重用之前创建 4,294,967,296 个在同一文件上工作的层,这将导致奇怪的行为。我应该实施一个更好的唯一 ID 系统还是这通常足够好?
我使用 Java,因此我可以使用 UUID 库,该库根据标准算法创建 128 位唯一 ID。但这似乎有些过分了。
是否有解决此类问题的通用方法?
I'm making a bitmap editor where a document consists of several layers where each layer represents a bitmap. Each layer must have a unique ID compared to all other layers that currently exist in the document. I also need to take into account that I need to save and load documents along with the layer IDs.
I'm using the command pattern to store actions that are performed on the document and the unique IDs are used to keep track of which layer an action should be performed on.
At the moment, I just keep a counter called X, when a new layer is created its ID is set to X then X is incremented. When loading, I need to make sure X is set to an appropriate number so that new layers are given unique IDs i.e. I could save the value of X and restore that, or set X based on the biggest layer ID loaded.
Given X is a 32-bit number, the user will need to create 4,294,967,296 layers working on the same file before IDs start to be reused which will cause weird behaviour. Should I implement a better unique ID system or is this generally good enough?
I'm in Java so I could use the UUID library which creates 128 bit unique IDs according to a standard algorithm. This seems overkill though.
Is there some general approach to this kind of problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这已经足够好了。以每秒 24/365 十个新层的速度(这很愚蠢),它可以正常运行大约三年。
This is perfectly good enough. At a rate of ten new layers per second 24/365, which is silly, it'll run fine for roughly three years.
如果您认为可以通过编程方式操作图层,因此在图像的生命周期中可能有 2^32 个图层,那么在读取文件时将所有图层 ID 放入 HashSet 中,并在添加/时更新该集合去除图层。 (您不需要显式存储该集合;与每个掩码关联的 ID 就足够了。)然后,不要采用“下一个数字”,而是采用“不在集合中的下一个数字”。拥有一个计数器仍然有用,但是在读取文件时将其设置为 0 还是 (max+1) 并不重要;除非您设想同时存在大量令人困惑的层,否则不需要花费大量时间来找到空白空间。
但是由于您使用的是 Java,因此您可以使用 long 而不是 int,然后您将永远(实际上来说)无法溢出该数字,即使您所做的只是创建一个单像素掩码并销毁一遍又一遍。
If you think you might manipulate layers programmatically, and thus have some possibility of having 2^32 layers in the lifetime of the image, then throw all the layer IDs into a HashSet when you read the file, and update that set when you add/remove layers. (You don't need to explicitly store the set; the ID associated with each mask is enough.) Then instead of taking "the next number", take "the next number not in the set". Having a counter is still useful, but whether you set it to 0 or (max+1) when you read a file is immaterial; it won't take a significant amount of time to find an empty space unless you envision bafflingly large numbers of layers present simultaneously.
But since you're using Java, you could just use a long instead of an int, and then you wouldn't ever (practically speaking) be able to overflow the number even if all you did was create a one-pixel mask and destroy it over and over again.