Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

QueryKeys.cpp

Go to the documentation of this file.
00001 //
00002 // QueryKeys.cpp
00003 //
00004 // Copyright (c) Shareaza Development Team, 2002-2005.
00005 // This file is part of SHAREAZA (www.shareaza.com)
00006 //
00007 // Shareaza is free software; you can redistribute it
00008 // and/or modify it under the terms of the GNU General Public License
00009 // as published by the Free Software Foundation; either version 2 of
00010 // the License, or (at your option) any later version.
00011 //
00012 // Shareaza is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 //
00017 // You should have received a copy of the GNU General Public License
00018 // along with Shareaza; if not, write to the Free Software
00019 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 //
00021 
00022 #include "StdAfx.h"
00023 #include "shareaza.h"
00024 #include "QueryKeys.h"
00025 
00026 #ifdef _DEBUG
00027 #undef THIS_FILE
00028 static char THIS_FILE[]=__FILE__;
00029 #define new DEBUG_NEW
00030 #endif
00031 
00032 
00034 // CQueryKeys construction
00035 
00036 CQueryKeys::CQueryKeys()
00037 {
00038         m_nBits         = 12;
00039         m_nTable        = (DWORD)pow( 2, m_nBits );
00040         m_pTable        = new DWORD[ m_nTable ];
00041         m_pMap          = new DWORD[ m_nBits * 2 ];
00042 
00043         DWORD* pMap = m_pMap;
00044 
00045         srand( GetTickCount() );
00046 
00047         for ( DWORD nCount = m_nBits ; nCount ; nCount-- )
00048         {
00049                 *pMap++ = 1 << ( rand() % 32 );
00050                 *pMap++ = 1 << ( rand() % 32 );
00051         }
00052 
00053         BYTE* pFill = (BYTE*)m_pTable;
00054 
00055         for ( DWORD nCount = m_nTable ; nCount ; nCount-- )
00056         {
00057                 *pFill++ = (BYTE)( rand() & 0xFF );
00058                 *pFill++ = (BYTE)( rand() & 0xFF );
00059                 *pFill++ = (BYTE)( rand() & 0xFF );
00060                 *pFill++ = (BYTE)( rand() & 0xFF );
00061         }
00062 }
00063 
00064 CQueryKeys::~CQueryKeys()
00065 {
00066         delete [] m_pMap;
00067         delete [] m_pTable;
00068 }
00069 
00071 // CQueryKeys create or lookup
00072 
00073 DWORD CQueryKeys::Create(DWORD nAddress)
00074 {
00075         DWORD* pMap = m_pMap;
00076         DWORD nHash = 0;
00077 
00078         for ( DWORD nCount = m_nBits, nBit = 1 ; nCount ; nCount--, nBit <<= 1 )
00079         {
00080                 BOOL bOne = ( nAddress & (*pMap++) ) != 0;
00081                 BOOL bTwo = ( nAddress & (*pMap++) ) != 0;
00082                 if ( bOne ^ bTwo ) nHash |= nBit;
00083         }
00084 
00085         return m_pTable[ nHash & ( m_nTable - 1 ) ];
00086 }
00087 
00089 // CQueryKeys check
00090 
00091 BOOL CQueryKeys::Check(DWORD nAddress, DWORD nKey)
00092 {
00093         DWORD* pMap = m_pMap;
00094         DWORD nHash = 0;
00095 
00096         for ( DWORD nCount = m_nBits, nBit = 1 ; nCount ; nCount--, nBit <<= 1 )
00097         {
00098                 BOOL bOne = ( nAddress & (*pMap++) ) != 0;
00099                 BOOL bTwo = ( nAddress & (*pMap++) ) != 0;
00100                 if ( bOne ^ bTwo ) nHash |= nBit;
00101         }
00102 
00103         return nKey == m_pTable[ nHash & ( m_nTable - 1 ) ];
00104 }
00105 

Generated on Thu Dec 15 10:39:46 2005 for Shareaza 2.2.1.0 by  doxygen 1.4.2