简体   繁体   中英

How to generate a unique identifier for a range of Int32 numbers

Is there a way to calculate and generate a unique identifier for a given range of Int32 numbers? eg see the example please:

var range1 = [a, b, c, d];
var unique1 = GenerateUnique(range1);

var range2 = [b, a, d, c];
var unique2 = GenerateUnique(range2);

// unique1 and unique2 should be equal. I mean:
var areEqual = unique1 == unique2; // should be true


var range3 = [a, b, c, d];
var unique3 = GenerateUnique(range3);

// unique1 and unique3 should be equal. I mean:
areEqual = unique1 == unique3; // should be true


var range4 = [a, b, c, e]; // it has not d, but has e. different array.
var unique4 = GenerateUnique(range4);

// unique1 and unique4 should NOT be equal. I mean:
areEqual = unique1 == unique4; // should NOT be true / should be false


private int /* or even string */ GenerateUnique(int[] range) {
    // what would be the implementation of this method?
} 

UPDATE:

1- The ranges doesn't have any duplicate members. So there wouldn't be a [1, 2, 3, 4, 4] set. So, there is always [1, 2, 3, 4]

2- Array can be sorted. No problem.

3- The purpose : I have a huge number of items in DB. Sometimes, I need to generate a Word document of them. And, I don't want to regenerate a previously generated document. That's it. Ranges are modifiable, so I want to regenerate a document for a certain range, if and only if an item got added/removed to/from range. NOTE: Changing database and saving a range's last-change is not an option.

我认为对整数列表进行排序,然后使用任何标准的哈希函数(如果需要,可以使用MD5,SHA1甚至CRC32)都可以正常工作。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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