xiaoxiaole

common.cpp

#include "common.h"

common.h

#ifndef COMMON_H_INCLUDED
#define COMMON_H_INCLUDED
/*--------------------------------------*/
//0
#define NULL  (0)
//1
#define getx(blockPos)  ((blockPos>>4)&0b1111)
#define gety(blockPos)  ((blockPos)&0b1111)
//2
#define getp(stateBlock)    ((stateBlock>>16)&0b11111111)
#define getpu(stateBlock)   ((stateBlock>>8)&0b11111111)
#define getpr(stateBlock)   ((stateBlock)&0b11111111)
/*--------------------------------------*/
//2
typedef unsigned char BlockPos;
//3
typedef int           StateBlock;

/*--------------------------------------*/



/*--------------------------------------*/
#endif // COMMON_H_INCLUDED

hash.cpp

#include "hash.h"
/*--------------------------------------*/
//1
Trie g_hash;
/*--------------------------------------*/
//1
TrieNode::TrieNode()
{
    this->s         = 0;
    this->next      = NULL;
    this->score     = 0;
}

//1
Trie::Trie()
{
    TrieNode a;
    this->layer0.push_back(a);
}
//2
bool Trie::isExited(vector<StateBlock> &v)
{
    if (g_hash.layer0[0].next == NULL) return false;

    vector<TrieNode>    *p = &g_hash.layer0; //point to vector layer 0
    int                 target, tmpL, l, r, mid;
    StateBlock          tmps;
    bool                foundflg;

    int vL = v.size();
    mid = 0;
    for (int i=0; i<vL; ++i) {
        p = (*p)[mid].next; //come to layer (i-1)
        tmpL = (*p).size();
        if (tmpL == 0) return false; //this layer is empty

        l       = 0;
        r       = tmpL-1;
        target  = v[i];
        foundflg = false;
        while (l<=r) {
            mid = ((l+r)>>1);
            tmps = (*p)[mid].s;
            if (tmps == target) { //find the target

                foundflg = true;
                goto NEXT_LOOP;
            }
            else if (tmps < target) { //in second part
                l = mid + 1;
            } else { //in first part
                r = mid - 1;
            }
        }
        return false;
        NEXT_LOOP:;
    }
    if ((*p)[mid].exiFlg == true) {
        return true;
    }
    return false;
}
//3
bool Trie::addOneState(vector<StateBlock> &v)
{

}

hash.h

#ifndef HASH_H_INCLUDED
#define HASH_H_INCLUDED
/*--------------------------------------*/
#include "common.h"
/*--------------------------------------*/
#include "vector"
using namespace std;
/*--------------------------------------*/
//#define getStateExiFlg(StateBlock)  (((StateBlock&0x8000)!=0)?1:0)
/*--------------------------------------*/
//1
class TrieNode {
public:
    StateBlock          s;
    vector<TrieNode>    *next;
    int                 score;

    TrieNode();
};

//2
class Trie {
public:
    vector<TrieNode>    layer0;

    bool isExited(vector<StateBlock> &v);
    bool addOneState(vector<StateBlock> &v);

    Trie();
};
/*--------------------------------------*/
//1
extern Trie g_hash;
/*--------------------------------------*/
//1

/*--------------------------------------*/
#endif // HASH_H_INCLUDED

main.cpp

#include "testfunc.h"
/*--------------------------------------*/
#include <iostream>
using namespace std;
/*--------------------------------------*/



int main()
{
    hash_test1();

    return 0;
}

testfunc.cpp

#include "testfunc.h"
#include "common.h"
#include "hash.h"
/*--------------------------------------*/
#include "stdio.h"
using namespace std;
/*--------------------------------------*/

//1
void hash_test1()
{

    cout << g_hash.layer0
    return;
}

testfunc.h

#ifndef TESTFUNC_H_INCLUDED
#define TESTFUNC_H_INCLUDED
/*--------------------------------------*/
//1
void hash_test1();

/*--------------------------------------*/
#endif // TESTFUNC_H_INCLUDED
原文地址:https://www.cnblogs.com/fish7/p/6939718.html