Algorithm 将三个32位标识符合并为一个32位标识符?

Algorithm 将三个32位标识符合并为一个32位标识符?,algorithm,data-structures,identity,Algorithm,Data Structures,Identity,给定三个标识符,将它们组合成单个32位值 众所周知,第一标识符可能具有(2^8)-1个不同的值。类似地,第二个(2^8)-1和第三个(2^10)-1。因此,所有类型的标识符的总数不会超过(2^32)-1 示例解决方案可能是拥有一张地图: 键:32位 值:8(或10)位 该值将从0开始,并在每次提供新标识符时递增 能做得更好吗?(而不是3张地图)您认为此解决方案有问题吗 为了澄清,标识符可以保存范围中的任何值,您可以通过位移位和不安全代码来实现这一点 有一篇关于这方面的文章: 然后,您可以将

给定三个标识符,将它们组合成单个32位值

众所周知,第一标识符可能具有(2^8)-1个不同的值。类似地,第二个(2^8)-1和第三个(2^10)-1。因此,所有类型的标识符的总数不会超过(2^32)-1

示例解决方案可能是拥有一张地图:

  • 键:32位
  • 值:8(或10)位
该值将从0开始,并在每次提供新标识符时递增

能做得更好吗?(而不是3张地图)您认为此解决方案有问题吗



为了澄清,标识符可以保存范围中的任何值,您可以通过位移位和不安全代码来实现这一点

有一篇关于这方面的文章:

然后,您可以将整个32位范围用于三个值

----8位-----8位-----10位-----6位未使用----


int result=firstValue您可以通过位移位和不安全代码来实现这一点

有一篇关于这方面的文章:

然后,您可以将整个32位范围用于三个值

----8位-----8位-----10位-----6位未使用----


int result=firstValue您可以通过位移位和不安全代码来实现这一点

有一篇关于这方面的文章:

然后,您可以将整个32位范围用于三个值

----8位-----8位-----10位-----6位未使用----


int result=firstValue您可以通过位移位和不安全代码来实现这一点

有一篇关于这方面的文章:

然后,您可以将整个32位范围用于三个值

----8位-----8位-----10位-----6位未使用----


int result=firstValue您可以尝试以下方法:-

#include <map>
#include <iostream>

class CombinedIdentifier
{
public:
    CombinedIdentifier (unsigned id1, unsigned id2, unsigned id3)
    {
        m_id [0] = id1;
        m_id [1] = id2;
        m_id [2] = id3;
    }

    // version to throw exception on ID not found
    static CombinedIdentifier GetIdentifier (unsigned int id)
    {
        // search m_store for a value = id
        // if found, get key and return it
        // else....throw an exception->id not found
    }

    // version to return found/not found instead of throwing an exception
    static bool GetIdentifier (unsigned int id, CombinedIdentifier &out)
    {
        // search m_store for a value = id
        // if found, get key and save it to 'out' and return true
        // else....return false
    }

    int operator [] (int index) { return m_id [index]; }

    bool operator < (const CombinedIdentifier &rhs) const
    {
        return m_id [0] < rhs.m_id [0] ? true : 
               m_id [1] < rhs.m_id [1] ? true : 
               m_id [2] < rhs.m_id [2];
    }

    bool operator == (const CombinedIdentifier &rhs) const
    {
        return m_id [0] == rhs.m_id [0] &&
               m_id [1] == rhs.m_id [1] &&
               m_id [2] == rhs.m_id [2];
    }

    bool operator != (const CombinedIdentifier &rhs) const
    {
        return !operator == (rhs);
    }

    int GetID ()
    {
        int 
            id;

        std::map <CombinedIdentifier, int>::iterator
            item = m_store.find (*this);

        if (item == m_store.end ())
        {
            id = m_store.size () + 1;
            m_store [*this] = id;
        }
        else
        {
            id = item->second;
        }        

        return id;
    }

private:
    int 
        m_id [3];

   static std::map <CombinedIdentifier, int>
       m_store;
};

std::map <CombinedIdentifier, int>
    CombinedIdentifier::m_store;

int main ()
{
    CombinedIdentifier
        id1 (2, 4, 10),
        id2 (9, 14, 1230),
        id3 (4, 1, 14560),
        id4 (9, 14, 1230);

    std::cout << "id1 = " << id1.GetID () << std::endl;
    std::cout << "id2 = " << id2.GetID () << std::endl;
    std::cout << "id3 = " << id3.GetID () << std::endl;
    std::cout << "id4 = " << id4.GetID () << std::endl;
 }
#包括
#包括
类组合标识符
{
公众:
组合标识符(无符号id1、无符号id2、无符号id3)
{
m_id[0]=id1;
m_id[1]=id2;
m_id[2]=id3;
}
//找不到要在ID上引发异常的版本
静态组合标识符GetIdentifier(无符号整数id)
{
//在m_商店中搜索值=id
//如果找到,获取密钥并将其返回
//否则…抛出异常->找不到id
}
//返回“已找到/未找到”而不是引发异常的版本
静态bool GetIdentifier(无符号int-id、组合标识符和out)
{
//在m_商店中搜索值=id
//如果找到,获取密钥并将其保存到“out”并返回true
//否则…返回false
}
int运算符[](int索引){return m_id[index];}
布尔运算符<(常量组合标识符和rhs)常量
{
返回m_id[0]秒;
}        
返回id;
}
私人:
int
m_id[3];
静态std::map
m_商店;
};
标准::地图
组合标识:m_商店;
int main()
{
组合识别器
id1(2,4,10),
id2(9、14、1230),
id3(4,114560),
id4(9、14、1230);

std::cout你可以试试这样的东西:-

#include <map>
#include <iostream>

class CombinedIdentifier
{
public:
    CombinedIdentifier (unsigned id1, unsigned id2, unsigned id3)
    {
        m_id [0] = id1;
        m_id [1] = id2;
        m_id [2] = id3;
    }

    // version to throw exception on ID not found
    static CombinedIdentifier GetIdentifier (unsigned int id)
    {
        // search m_store for a value = id
        // if found, get key and return it
        // else....throw an exception->id not found
    }

    // version to return found/not found instead of throwing an exception
    static bool GetIdentifier (unsigned int id, CombinedIdentifier &out)
    {
        // search m_store for a value = id
        // if found, get key and save it to 'out' and return true
        // else....return false
    }

    int operator [] (int index) { return m_id [index]; }

    bool operator < (const CombinedIdentifier &rhs) const
    {
        return m_id [0] < rhs.m_id [0] ? true : 
               m_id [1] < rhs.m_id [1] ? true : 
               m_id [2] < rhs.m_id [2];
    }

    bool operator == (const CombinedIdentifier &rhs) const
    {
        return m_id [0] == rhs.m_id [0] &&
               m_id [1] == rhs.m_id [1] &&
               m_id [2] == rhs.m_id [2];
    }

    bool operator != (const CombinedIdentifier &rhs) const
    {
        return !operator == (rhs);
    }

    int GetID ()
    {
        int 
            id;

        std::map <CombinedIdentifier, int>::iterator
            item = m_store.find (*this);

        if (item == m_store.end ())
        {
            id = m_store.size () + 1;
            m_store [*this] = id;
        }
        else
        {
            id = item->second;
        }        

        return id;
    }

private:
    int 
        m_id [3];

   static std::map <CombinedIdentifier, int>
       m_store;
};

std::map <CombinedIdentifier, int>
    CombinedIdentifier::m_store;

int main ()
{
    CombinedIdentifier
        id1 (2, 4, 10),
        id2 (9, 14, 1230),
        id3 (4, 1, 14560),
        id4 (9, 14, 1230);

    std::cout << "id1 = " << id1.GetID () << std::endl;
    std::cout << "id2 = " << id2.GetID () << std::endl;
    std::cout << "id3 = " << id3.GetID () << std::endl;
    std::cout << "id4 = " << id4.GetID () << std::endl;
 }
#包括
#包括
类组合标识符
{
公众:
组合标识符(无符号id1、无符号id2、无符号id3)
{
m_id[0]=id1;
m_id[1]=id2;
m_id[2]=id3;
}
//找不到要在ID上引发异常的版本
静态组合标识符GetIdentifier(无符号整数id)
{
//在m_商店中搜索值=id
//如果找到,获取密钥并将其返回
//否则…抛出异常->找不到id
}
//返回“已找到/未找到”而不是引发异常的版本
静态bool GetIdentifier(无符号int-id、组合标识符和out)
{
//在m_商店中搜索值=id
//如果找到,获取密钥并将其保存到“out”并返回true
//否则…返回false
}
int运算符[](int索引){return m_id[index];}
布尔运算符<(常量组合标识符和rhs)常量
{
返回m_id[0]秒;
}        
返回id;
}
私人:
int
m_id[3];
静态std::map
m_商店;
};
标准::地图
组合标识:m_商店;
int main()
{
组合识别器
id1(2,4,10),
id2(9、14、1230),
id3(4,114560),
id4(9、14、1230);

std::cout你可以试试这样的东西:-

#include <map>
#include <iostream>

class CombinedIdentifier
{
public:
    CombinedIdentifier (unsigned id1, unsigned id2, unsigned id3)
    {
        m_id [0] = id1;
        m_id [1] = id2;
        m_id [2] = id3;
    }

    // version to throw exception on ID not found
    static CombinedIdentifier GetIdentifier (unsigned int id)
    {
        // search m_store for a value = id
        // if found, get key and return it
        // else....throw an exception->id not found
    }

    // version to return found/not found instead of throwing an exception
    static bool GetIdentifier (unsigned int id, CombinedIdentifier &out)
    {
        // search m_store for a value = id
        // if found, get key and save it to 'out' and return true
        // else....return false
    }

    int operator [] (int index) { return m_id [index]; }

    bool operator < (const CombinedIdentifier &rhs) const
    {
        return m_id [0] < rhs.m_id [0] ? true : 
               m_id [1] < rhs.m_id [1] ? true : 
               m_id [2] < rhs.m_id [2];
    }

    bool operator == (const CombinedIdentifier &rhs) const
    {
        return m_id [0] == rhs.m_id [0] &&
               m_id [1] == rhs.m_id [1] &&
               m_id [2] == rhs.m_id [2];
    }

    bool operator != (const CombinedIdentifier &rhs) const
    {
        return !operator == (rhs);
    }

    int GetID ()
    {
        int 
            id;

        std::map <CombinedIdentifier, int>::iterator
            item = m_store.find (*this);

        if (item == m_store.end ())
        {
            id = m_store.size () + 1;
            m_store [*this] = id;
        }
        else
        {
            id = item->second;
        }        

        return id;
    }

private:
    int 
        m_id [3];

   static std::map <CombinedIdentifier, int>
       m_store;
};

std::map <CombinedIdentifier, int>
    CombinedIdentifier::m_store;

int main ()
{
    CombinedIdentifier
        id1 (2, 4, 10),
        id2 (9, 14, 1230),
        id3 (4, 1, 14560),
        id4 (9, 14, 1230);

    std::cout << "id1 = " << id1.GetID () << std::endl;
    std::cout << "id2 = " << id2.GetID () << std::endl;
    std::cout << "id3 = " << id3.GetID () << std::endl;
    std::cout << "id4 = " << id4.GetID () << std::endl;
 }
#包括
#包括
类组合标识符
{
公众:
组合识别器(un)
for (j=0; j<4; j++) {
     // standard Pearson hash (output is h)