青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

洗塵齋

三懸明鏡垂鴻韻,九撩清泉洗塵心

常用鏈接

統(tǒng)計

最新評論

使用共享內(nèi)存的多級哈希表的一種實現(xiàn)

在一個服務(wù)程序運行的時候,它往往要把數(shù)據(jù)寫入共享內(nèi)存以便在進城需要重新啟動的時候可以直接從共享內(nèi)存中讀取數(shù)據(jù),另一方面,在服務(wù)進程因某種原因掛掉的時候,共享內(nèi)存中的數(shù)據(jù)仍然存在,這樣就可以減少帶來的損失。關(guān)于共享內(nèi)存的內(nèi)容請google之,在這里,實現(xiàn)了一種在共享內(nèi)存中存取數(shù)據(jù)的hash表,它采用了多級存儲求模取余的方法,具體內(nèi)容請看以下代碼:
http://lmlf001.blog.sohu.com/


//hash_shm.h
#ifndef _STORMLI_HASH_SHM_H_
#define _STORMLI_HASH_SHM_H_

#include
<iostream>
#include
<cstdlib>
#include
<cmath>
#include
<sys/shm.h>
using namespace std;

template
<typename valueType,unsigned long maxLine,int lines>
class hash_shm
{
public:    
    
int find(unsigned long _key);    //if _key in the table,return 0,and set lastFound the position,otherwise return -1
    int remove(unsigned long _key);    //if _key not in the table,return-1,else remove the node,set the node key 0 and return 0

    
//insert node into the table,if the _key exists,return 1,if insert success,return 0;and if fail return -1
    int insert(unsigned long _key,const valueType &_value);
    
void clear();        //remove all the data

public:    //some statistic function
    double getFullRate()const;        //the rate of the space used
    
public:
    
//constructor,with the share memory start position and the space size,if the space is not enough,the program will exit
    hash_shm(void *startShm,unsigned long shmSize=sizeof(hash_node)*maxLine*lines);

    
//constructor,with the share memory key,it will get share memory,if fail,exit
    hash_shm(key_t shm_key);
    
~hash_shm(){}    //destroy the class
private:
    
void *mem;        //the start position of the share memory  // the mem+memSize  space used to storage the runtime data:currentSize
    unsigned long memSize;    //the size of the share memory
    unsigned long modTable[lines];    //modtable,the largest primes
    unsigned long maxSize;        //the size of the table
    unsigned long *currentSize;    //current size of the table ,the pointer of the shm mem+memSize
    void *lastFound;        //write by the find function,record the last find place
    
    
struct hash_node{        //the node of the hash table
        unsigned long key;    //when key==0,the node is empty
        valueType value;    //name-value pair
    };
private:
    
bool getShm(key_t shm_key);    //get share memory,used by the constructor
    void getMode();        //get the largest primes blow maxLine,use by the constructor
    void *getPos(unsigned int _row,unsigned long _col);//get the positon with the (row,col)
};

template
<typename vT,unsigned long maxLine,int lines>
hash_shm
<vT,maxLine,lines>::hash_shm(void *startShm,unsigned long shmSize)
{
    
if(startShm!=NULL){
        cerr
<<"Argument error\n Please check the shm address\n";
        exit(
-1);
    }
    getMode();
    maxSize
=0;
    
int i;
    
for(i=0;i<lines;i++)    //count the maxSize
        maxSize+=modTable[i];
    
if(shmSize<sizeof(hash_node)*(maxSize+1)){    //check the share memory size
        cerr<<"Not enough share memory space\n";
        exit(
-1);
    }
    memSize
=shmSize;
    
if(*(currentSize=(unsigned long *)((long)mem+memSize))<0)
        
*currentSize=0;;
}

template
<typename vT,unsigned long maxLine,int lines>
hash_shm
<vT,maxLine,lines>::hash_shm(key_t shm_key)
{    
//constructor with get share memory
    getMode();
    maxSize
=0;
    
for(int i=0;i<lines;i++)
        maxSize
+=modTable[i];
    memSize
=sizeof(hash_node)*maxSize;    
    
if(!getShm(shm_key)){
        exit(
-1);
    }
//    memset(mem,0,memSize);
    if(*(currentSize=(unsigned long *)((long)mem+memSize))<0)
        
*currentSize=0;
}    


template
<typename vT,unsigned long maxLine,int lines>
int hash_shm<vT,maxLine,lines>::find(unsigned long _key)
{
    unsigned 
long hash;
    hash_node 
*pH=NULL;
    
for(int i=0;i<lines;i++)
    {
        hash
=(_key+maxLine)%modTable[i];    //calculate the col position
        pH=(hash_node *)getPos(i,hash);
//        if(pH==NULL)return -2;    //almost not need
        if(pH->key==_key){
            lastFound
=pH;
            
return 0;
        }
    }
    
return -1;
}

template
<typename vT,unsigned long maxLine,int lines>
int hash_shm<vT,maxLine,lines>::remove(unsigned long _key)
{
    
if(find(_key)==-1)return -1;    //not found
    hash_node *pH=(hash_node *)lastFound;
    pH
->key=0;        //only set the key 0
    (*currentSize)--;
    
return 0;
}

template
<typename vT,unsigned long maxLine,int lines>
int hash_shm<vT,maxLine,lines>::insert(unsigned long _key,const vT &_value)
{
    
if(find(_key)==0)return 1;    //if the key exists
    unsigned long hash;
    hash_node 
*pH=NULL;
    
for(int i=0;i<lines;i++){    
        hash
=(_key+maxLine)%modTable[i];
        pH
=(hash_node *)getPos(i,hash);
        
if(pH->key==0){        //find the insert position,insert the value
            pH->key=_key;
            pH
->value=_value;
            (
*currentSize)++;
            
return 0;
        }
    }
    
return -1;    //all the appropriate position filled
}


template
<typename vT,unsigned long maxLine,int lines>
void hash_shm<vT,maxLine,lines>::clear()
{
    memset(mem,
0,memSize);
    
*currentSize=0;
}


template
<typename vT,unsigned long maxLine,int lines>
bool hash_shm<vT,maxLine,lines>::getShm(key_t shm_key)
{
    
int shm_id=shmget(shm_key,memSize,0666);
    
if(shm_id==-1)    //check if the shm exists
    {
        shm_id
=shmget(shm_key,memSize,0666|IPC_CREAT);//create the shm
        if(shm_id==-1){
            cerr
<<"Share memory get failed\n";
            
return false;
        }
    }
    mem
=shmat(shm_id,NULL,0);    //mount the shm
    if(int(mem)==-1){
        cerr
<<"shmat system call failed\n";
        
return false;
    }
    
return true;
}

template
<typename vT,unsigned long maxLine,int lines>
void hash_shm<vT,maxLine,lines>::getMode()
{        
//采用 6n+1 6n-1 素數(shù)集中原理
    if(maxLine<5){exit(-1);}
    
    unsigned 
long t,m,n,p;
    
int i,j,a,b,k;
    
int z=0;
    
    
for(t=maxLine/6;t>=0,z<lines;t--)
    {
        i
=1;j=1; k=t%10;
        m
=6*t;                                        /**i,j的值 是是否進行驗證的標(biāo)志也是對應(yīng)的6t-1和6t+1的素性標(biāo)志**/
        
if(((k-4)==0)||((k-9)==0)||((m+1)%3==0))j=0;/*此處是簡單驗證6*t-1,6*t+1 是不是素數(shù),借以提高素數(shù)純度**/
        
if(((k-6)==0)||((m-1)%3==0))i=0;            /***先通過初步判斷去除末尾是5,及被3整除的數(shù)***/
        
for(p=1;p*6<=sqrt(m+1)+2;p++ )
        {
            n
=p*6;                                    /**將6*p-1和6*p+1看作偽素數(shù)來試除*****/
            k
=p%10;
            a
=1;b=1;                                /**同樣此處a,b的值也是用來判斷除數(shù)是否為素數(shù)提高除數(shù)的素數(shù)純度**/
            
if(((k-4)==0)||((k-9)==0))a=0;
            
if(((k-6)==0))b=0;
            
if(i){                            /*如果i非零就對m-1即所謂6*t-1進行驗證,當(dāng)然還要看除數(shù)n+1,n-1,素性純度*/
                
if(a){if((m-1)%(n+1)==0)i=0;}        /***一旦被整除就說明不是素數(shù)故素性為零即將i 賦值為零***/
                
if(b){if((m-1)%(n-1)==0)i=0;}
            }
            
if(j){                           /**如果j非零就對m+1即所謂6*t+1進行驗證,當(dāng)然還要看除數(shù)n+1,n-1,素性純度*/
                
if(a){if((m+1)%(n+1)==0)j=0;}         /***一旦被整除就說明不是素數(shù)故素性為零即將j 賦值為零***/
                
if(b){if((m+1)%(n-1)==0)j=0;}
            }
            
if((i+j)==0)break;                     /**如果已經(jīng)知道6*t-1,6*t+1都不是素數(shù)了那就結(jié)束試除循環(huán)***/
        }
        
if(j){modTable[z++]=m+1;if(z>= lines)return;}
        
if(i){modTable[z++]=m-1;if(z>= lines)return;}
    }
}

template
<typename vT,unsigned long maxLine,int lines>
void *hash_shm<vT,maxLine,lines>::getPos(unsigned int _row,unsigned long _col)
{
    unsigned 
long pos=0UL;
    
for(int i=0;i<_row;i++)    //calculate the positon from the start
        pos+=modTable[i];
    pos
+=_col;        
    
if(pos>=maxSize)return NULL;
    
return (void *)((long)mem+pos*sizeof(hash_node));
}

template
<typename vT,unsigned long maxLine,int lines>
double hash_shm<vT,maxLine,lines>::getFullRate()const
{
    
return double(*currentSize)/maxSize;
}


#endif



//test.cpp

#include
"hash_shm.h"
#include
<cstdlib>
using namespace std;
int main()
{
    hash_shm
<int,1000,100> ht(key_t(999));
    
double rate=0.0;
//    ht.clear();
    for(int i=0;i<100;i++){
        srand(time(NULL)
+i);
        
while(true){
            
if(ht.insert(rand(),0)==-1)break;
        }
        cout
<<ht.getFullRate()<<endl;
        rate
+=ht.getFullRate();
        ht.clear();
    }
    cout
<<"\n\n\n";
    cout
<<rate/100<<endl;
}

這段代碼作測試的時候發(fā)現(xiàn)了一些問題,用gprof查看函數(shù)時間的時候發(fā)現(xiàn),getPos函數(shù)占用了大部分的執(zhí)行時間,始主要的性能瓶頸,后來又新設(shè)立了一個數(shù)組,用來記錄每行開始時的位置,性能提高了很多,改動部分的代碼如下:
template<typename valueType,unsigned long maxLine,int lines>
class hash_shm
{

private:
    
void *mem;        //the start position of the share memory  // the mem+memSize  space used to storage the runtime data:currentSize
    unsigned long memSize;    //the size of the share memory
    unsigned long modTable[lines];    //modtable,the largest primes
    unsigned long modTotal[lines];    //modTotal[i] is the summary of the modTable when x<=i  
                    
//used by getPos to improve the performance
    ...
};

template
<typename vT,unsigned long maxLine,int lines>
hash_shm
<vT,maxLine,lines>::hash_shm(void *startShm,unsigned long shmSize)
{
    ...

    
    
int i;
    
for(i=0;i<lines;i++){    //count the maxSize
        maxSize+=modTable[i];
        
if(i!=0)modTotal[i]=modTotal[i-1]+modTable[i-1];
        
else modTotal[i]=0;    //caculate the modTotal
    }
    ...
}

template
<typename vT,unsigned long maxLine,int lines>
hash_shm
<vT,maxLine,lines>::hash_shm(key_t shm_key)
{    
//constructor with get share memory 
    getMode();
    maxSize
=0;
    
for(int i=0;i<lines;i++){
        maxSize
+=modTable[i];
        
if(i!=0)modTotal[i]=modTotal[i-1]+modTable[i-1];
        
else modTotal[i]=0;
    }
    ...
}    




template
<typename vT,unsigned long maxLine,int lines>
void *hash_shm<vT,maxLine,lines>::getPos(unsigned int _row,unsigned long _col)
{
    unsigned 
long pos=_col+modTotal[_row];
    
//for(int i=0;i<_row;i++)    //calculate the positon from the start
    
//    pos+=modTable[i];
    if(pos<maxSize)
        
return (void *)((long)mem+pos*sizeof(hash_node));
    
return NULL;    
}


新增了一個用于遍歷的函數(shù)foreach

template<typename vT,unsigned long maxLine,int lines>
void hash_shm<vT,maxLine,lines>::foreach(void (*fn)(unsigned long _key,vT &_value))
{
    typedef  unsigned 
long u_long;
    u_long beg
=(u_long)mem;
    u_long end
=(u_long)mem+sizeof(hash_node)*(modTable[lines-1]+modTotal[lines-1]);
    hash_node 
*p=NULL;
    
for(u_long pos=beg;pos<end;pos+=sizeof(hash_node))
    {
        p
=(hash_node *)pos;
        
if(p->key!=0)fn(p->key,p->value);
    }
}

為了利于使用新增一個用于查找的函數(shù)find,該函數(shù)同find(_key)類似,如果找到_key節(jié)點,把它賦給_value以返回
int find(unsigned long _key,vT &_value);

posted on 2007-09-08 21:17 芥之舟 閱讀(7717) 評論(3)  編輯 收藏 引用 所屬分類: 數(shù)據(jù)結(jié)構(gòu)和算法

評論

# re: 使用共享內(nèi)存的多級哈希表的一種實現(xiàn) 2012-06-16 16:44 ydsec

不錯,能發(fā)進一步改進,value如果是一個結(jié)構(gòu)體,如何實現(xiàn)呢?內(nèi)存用格式表示數(shù)據(jù)  回復(fù)  更多評論   

# re: 使用共享內(nèi)存的多級哈希表的一種實現(xiàn) 2014-08-28 13:14 null

if(*(currentSize=(unsigned long *)((long)mem+memSize))<0)
這一句不是很明白,  回復(fù)  更多評論   

# re: 使用共享內(nèi)存的多級哈希表的一種實現(xiàn) 2014-10-13 10:51 abc

p*6<=sqrt(m+1)+2 這個是什么原理?  回復(fù)  更多評論   

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            国产亚洲欧美日韩在线一区| 欧美一区二视频| 亚洲视频在线看| 亚洲一二三级电影| 欧美一进一出视频| 久久精品国产综合精品| 精品91免费| 欧美激情aaaa| 亚洲精品资源美女情侣酒店| 亚洲一区制服诱惑| 国产亚洲一区二区三区在线观看 | 久久在线精品| 欧美本精品男人aⅴ天堂| 亚洲国产精品999| 欧美裸体一区二区三区| 亚洲无毛电影| 可以免费看不卡的av网站| 亚洲第一精品影视| 欧美区一区二区三区| 一片黄亚洲嫩模| 欧美专区日韩专区| 日韩视频在线观看免费| 国产精品一香蕉国产线看观看 | 欧美日韩不卡合集视频| 亚洲一区二区三区四区视频| 久久久久久穴| 亚洲精品在线一区二区| 国产精品夜夜夜一区二区三区尤| 欧美亚洲综合在线| 亚洲国产精品高清久久久| 夜夜嗨av一区二区三区四季av| 国产精品久久久亚洲一区| 久久精品国产久精国产爱| 日韩午夜黄色| 免费观看30秒视频久久| 亚洲自拍啪啪| 亚洲毛片在线| 黑人巨大精品欧美一区二区| 欧美日韩免费观看一区二区三区| 久久九九99视频| 亚洲女人天堂成人av在线| 免费观看成人| 久久综合狠狠| 久久成人免费| 欧美一区二区三区免费大片| 亚洲神马久久| 99精品久久久| 亚洲日本aⅴ片在线观看香蕉| 欧美三级韩国三级日本三斤| 欧美va天堂| 六月丁香综合| 久久久久国产精品一区三寸| 亚洲曰本av电影| 亚洲一区二区网站| 中文国产亚洲喷潮| 日韩一级视频免费观看在线| 91久久亚洲| 亚洲精品乱码久久久久久黑人| 亚洲国产高清视频| 亚洲电影毛片| 亚洲韩国青草视频| 亚洲三级免费| 亚洲另类一区二区| 亚洲美女在线国产| 一区二区精品在线| 亚洲小说春色综合另类电影| 亚洲一区二区三区在线观看视频| 亚洲天堂久久| 性色av一区二区三区在线观看 | 亚洲精品一区二区三| 亚洲国产另类 国产精品国产免费| 亚洲高清久久网| 亚洲国产精品尤物yw在线观看| 亚洲欧洲精品成人久久奇米网| 亚洲精品乱码| 午夜视频在线观看一区二区三区| 欧美在线一级视频| 欧美日韩免费| 夜夜狂射影院欧美极品| 亚洲一级网站| 久久精品中文| 欧美日韩成人一区| 国产亚洲激情| 亚洲精品久久7777| 亚洲在线1234| 久久亚洲一区| 最新国产乱人伦偷精品免费网站| 亚洲精品中文字| 亚洲欧美日韩国产| 久久久国产精品一区二区三区| 欧美成人一品| 国产日韩欧美一区| 亚洲看片一区| 欧美一区激情| 91久久在线视频| 亚洲欧美中文日韩在线| 午夜精品久久久久久99热| 美女日韩欧美| 国产美女精品| 日韩视频在线永久播放| 久久久久国产精品www| 亚洲精品一区久久久久久| 久久九九热re6这里有精品| 欧美电影在线| 狠狠狠色丁香婷婷综合久久五月 | 国内外成人在线视频| 亚洲性xxxx| 欧美不卡福利| 欧美一区二区私人影院日本| 欧美国产精品一区| 国语精品中文字幕| 亚洲欧美日韩国产一区二区| 欧美韩国在线| 午夜在线不卡| 国产精品久久久久77777| 亚洲国产精品免费| 久久久噜噜噜久久中文字幕色伊伊 | 国产一区91| 亚洲欧美美女| 99精品国产一区二区青青牛奶| 久久免费黄色| 国内伊人久久久久久网站视频| 亚洲自拍偷拍福利| 亚洲伦伦在线| 欧美日韩第一区| 亚洲美女av在线播放| 欧美成ee人免费视频| 久久国产精品72免费观看| 国产日产欧美精品| 欧美亚洲专区| 亚洲欧美激情在线视频| 国产精品v一区二区三区| 欧美二区乱c少妇| 欧美福利一区二区| 亚洲欧洲一区二区在线播放| 免费在线亚洲欧美| 免费国产一区二区| 亚洲日本国产| 亚洲国产成人不卡| 欧美高清不卡在线| 99riav久久精品riav| 亚洲高清在线观看一区| 免费看黄裸体一级大秀欧美| 最新国产成人在线观看| 亚洲国产婷婷香蕉久久久久久99| 免费亚洲一区| 一区二区三区你懂的| 一区二区三区导航| 欧美91大片| 亚洲精选一区二区| 日韩亚洲欧美中文三级| 国产精品视频yy9099| 久久久久久综合| 久久免费高清| 日韩一区二区高清| 亚洲永久在线观看| 精品成人一区二区三区| 老司机精品久久| 欧美电影免费| 午夜精品久久久久久久久久久久 | 在线一区亚洲| 宅男精品导航| 伊人精品成人久久综合软件| 亚洲成人自拍视频| 99综合精品| 一区二区三区高清在线观看| 国产精品一区二区在线观看不卡| 久久久人成影片一区二区三区观看| 久久精品一区二区三区中文字幕| 亚洲国产精品一区二区第四页av| 99精品国产高清一区二区| 国产欧美精品一区| 亚洲高清免费在线| 国产精品日韩一区二区三区| 免费看黄裸体一级大秀欧美| 国产精品福利av| 欧美国产日韩一区| 国产日韩欧美综合精品| 亚洲精品国产精品乱码不99按摩| 国产伦一区二区三区色一情| 亚洲国产精品传媒在线观看| 国产亚洲精品久久久久动| 99re6这里只有精品| 在线视频国内自拍亚洲视频| 亚洲综合三区| 亚洲私人影院| 美女性感视频久久久| 午夜在线视频观看日韩17c| 美日韩丰满少妇在线观看| 性欧美暴力猛交69hd| 欧美日韩不卡合集视频| 亚洲成色最大综合在线| 你懂的网址国产 欧美| 国产日韩精品一区二区三区在线| 欧美α欧美αv大片| 国产日韩精品视频一区| 一区二区三区成人| 一本不卡影院| 男人的天堂亚洲| 欧美高清一区|