简体   繁体   English

如何为文档中的每个对象提供唯一的ID?

[英]How to give each object in a document a unique 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. 与文档中当前存在的所有其他图层相比,每个图层必须具有唯一的ID。 I also need to take into account that I need to save and load documents along with the layer IDs. 我还需要考虑到我需要保存和加载文档以及图层ID。

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. 我正在使用命令模式来存储对文档执行的操作,并且使用唯一ID来跟踪应该对哪个层执行操作。

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. 目前,我只保留一个名为X的计数器,当创建一个新图层时,其ID设置为X,然后X递增。 When loading, I need to make sure X is set to an appropriate number so that new layers are given unique IDs ie I could save the value of X and restore that, or set X based on the biggest layer ID loaded. 加载时,我需要确保将X设置为适当的数字,以便为新图层指定唯一ID,即我可以保存X的值并恢复它,或者根据加载的最大图层ID设置X.

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. 如果X是32位数,则在ID开始重用之前,用户需要创建4,294,967,296个层在同一文件上工作,这将导致奇怪的行为。 Should I implement a better unique ID system or is this generally good enough? 我应该实施一个更好的独特ID系统还是这个通常足够好?

I'm in Java so I could use the UUID library which creates 128 bit unique IDs according to a standard algorithm. 我是Java,所以我可以使用UUID库,根据标准算法创建128位唯一ID。 This seems overkill though. 这似乎有点矫枉过正了。

Is there some general approach to this kind of problem? 这种问题有一些通用的方法吗?

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. 以每秒十个新的速度24/365,这是愚蠢的,它将运行良好大约三年。

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. 如果您认为可以以编程方式操作图层,因此可能在图像的生命周期中有2 ^ 32个图层,则在读取文件时将所有图层ID抛出到HashSet中,并在添加/时更新该图层集删除图层。 (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". (您不需要显式存储该集合;与每个掩码关联的ID就足够了。)然后,不要使用“下一个数字”,而是“不在集合中的下一个数字”。 Having a counter is still useful, but whether you set it to 0 or (max+1) when you read a file is immaterial; 拥有一个计数器仍然有用,但是当你读取文件时将它设置为0或(max + 1)是无关紧要的; 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. 但是既然你正在使用Java,你可以使用long而不是int,然后你就不会(实际上说)能够溢出数字,即使你所做的只是创建一个像素的掩码并且销毁它一遍又一遍。

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

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