1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
|
/* $Id: hashset.cpp,v 1.2 2008/06/16 16:02:40 rotmistr Exp $
* ===========================================================================
*
* PUBLIC DOMAIN NOTICE
* National Center for Biotechnology Information
*
* This software/database is a "United States Government Work" under the
* terms of the United States Copyright Act. It was written as part of
* the author's official duties as a United States Government employee and
* thus cannot be copyrighted. This software/database is freely available
* to the public for use. The National Library of Medicine and the U.S.
* Government have not placed any restriction on its use or reproduction.
*
* Although all reasonable efforts have been taken to ensure the accuracy
* and reliability of the software and data, the NLM and the U.S.
* Government do not and cannot warrant the performance or results that
* may be obtained by using this software or data. The NLM and the U.S.
* Government disclaim all warranties, express or implied, including
* warranties of performance, merchantability or fitness for any particular
* purpose.
*
* Please cite the author in any work or product based on this material.
*
* =========================================================================
*
* Author: Kirill Rotmistrovsky
*
* ========================================================================= */
#include <epcr/hashset.hpp>
#include <string.h>
USING_NCBI_SCOPE;
USING_SCOPE(EPCR_SCOPE);
unsigned char CHashSet::sm_HashId[]=
"********************************"
"********************************"
"*\0*\1***\2************\3***********"
//=Ab=Cdef=Ghijklmnopqrs=Tuvwxyz*****//
"*\0*\1***\2************\3***********"
"********************************"
"********************************"
"********************************"
"********************************";
inline bool s_IsAmbiq(unsigned char a) { return a>3; }
typedef CHashSet::hash_type hash_type;
typedef CHashSet::size_type size_type;
typedef CHashSet::bits_type bits_type;
/*
static char * s_BitsRepr(unsigned u, int w=32)
{
static char ret[65];
if(w>32) w=31;
if(w<1) w=1;
for(unsigned m=(1<<(w-1)), i=0; m; ++i,(m>>=1)) {
ret[i]=(m&u)?'1':'0';
}
ret[w]=0;
return ret;
}
*/
/*
************************************************************************
m_mask[*] 0000111111111
m_word[0] 0000110110110
m_word[1] 0000101101101
m_word[2] 0000011011011
m_ambiq 0000001000000
str(rev) acgtACNGTCATGcgatagat
^ ptr, offset
************************************************************************
*/
bool CHashSet::Begin(const char * ptr)
{
m_Ptr=ptr;
m_Offset=0;
m_AmbMask= ~0U; // all are ambiquos in the very beginning
// for(size_type t=0; t<m_Period; ++t) m_Hash[t]=0;
memset(m_Hash,0,m_Period*sizeof(*m_Hash));
if(ptr==0) return false;
if(m_Period==1) {
for(;m_Offset < m_WdSize && *m_Ptr; ++m_Ptr, ++m_Offset) {
m_AmbMask<<=1;
unsigned char nt=sm_HashId[(int)*m_Ptr];
if(nt>3) { m_AmbMask|=1; nt=0; }
// Circular 'rotation' with update of values
m_Hash[0]=m_Mask[0]&((m_Hash[0]<<2)|nt);
}
}
else {
for(;m_Offset < m_WdSize && *m_Ptr; ++m_Ptr, ++m_Offset) {
m_AmbMask<<=1;
unsigned char nt=sm_HashId[(int)*m_Ptr];
if(nt>3) { m_AmbMask|=1; nt=0; }
hash_type x=m_Hash[m_Period-1];
for(size_type t=m_Period-1; t>0; --t) {
m_Hash[t]=m_Mask[t]&((m_Hash[t-1]<<2)|nt);
}
m_Hash[0]=x&m_Mask[0];
}
}
return !End();
}
bool CHashSet::Good() const
{
for(size_type t=0; t< m_Period; ++t) if(!Good(t)) return false;
return true;
}
CHashSet::~CHashSet() throw ()
{
_intl_free();
}
void CHashSet::_intl_free()
{
delete[] m_Word;
delete[] m_Mask;
delete[] m_Hash;
delete[] m_TbSize;
}
CHashSet::CHashSet(TSize wdsize, TSize period)
:m_Ptr(0),m_Offset(0),m_AmbMask(0)
{
if(period==0 || period>wdsize) period=1;
#if 0
// Assure that hash ranged are equal
if(wdsize%period) throw logic_error("Uneven hash masks");
#endif
m_TbSize = new TSize[period];
m_Word = new TBitMask[period];
m_Mask = new THashValue[period];
m_Hash = new THashValue[period];
m_WdSize = wdsize;
m_Period = period;
if(period<=1) {
m_TbSize[0] = 1<<(2*wdsize);
m_Mask[0]=m_TbSize[0]-1;
m_Word[0]=(1<<wdsize)-1;
m_Hash[0]=0;
}
else {
for(size_type i=0; i<period; ++i) m_Word[i]=m_Mask[i]=0;
for(size_type i=0, bit=1; i<m_WdSize; ++i, (bit<<=1)) {
m_Word[i%period]|=bit;
m_Mask[i%period]++; // count bits off
}
bits_type wdmask=((1<<m_WdSize)-1);
for(size_type i=0; i<period; ++i) {
m_TbSize[i]=1<<((wdsize-m_Mask[i])*2);
m_Mask[i]=m_TbSize[i]-1;
m_Word[i]= (~m_Word[i]) & wdmask;//&m_Mask[i];
m_Hash[i]=0;
}
}
}
CHashSet::CHashSet(const CHashSet& s)
{
_intl_copy(s);
}
void CHashSet::_intl_copy(const CHashSet& s)
{
m_Ptr=s.m_Ptr;
m_Offset=s.m_Offset;
m_AmbMask=s.m_AmbMask;
m_WdSize=s.m_WdSize;
m_Period=s.m_Period;
m_TbSize = new TSize[m_Period];
m_Word = new TBitMask[m_Period];
m_Mask = new THashValue[m_Period];
m_Hash = new THashValue[m_Period];
for(size_type t=0; t<m_Period; ++t) {
m_TbSize[t]=s.m_TbSize[t];
m_Word [t]=s.m_Word [t];
m_Mask [t]=s.m_Mask [t];
m_Hash [t]=s.m_Hash [t];
}
}
CHashSet& CHashSet::operator = (const CHashSet& s)
{
if(&s!=this) {
_intl_free();
_intl_copy(s);
}
return *this;
}
////////////////////////////////////////////////////////////////////////
#if defined TEST_MAIN
#include <string>
#include <iostream>
#include <iomanip>
#include <unistd.h>
using namespace std;
int main(int argc, char ** argv)
{
try {
int optchar;
int period=0;
int wdsize=7;
while((optchar=getopt(argc,argv,"hVp:w:"))!=-1) {
switch(optchar){
case 'V':
case 'h':
cout<< "usage: [-hV] [-p period] [-w wordsize] [string ...]\n";
return 0;
case 'p': period=atoi(optarg); break;
case 'w': wdsize=atoi(optarg); break;
}
}
CHashSet hset(wdsize,period);
cout
<< "WordSize = " << hset.GetWordSize() << endl
<< "TabCount = " << hset.GetWordCount() << endl
<< "Id\t"
<< setw(16) << "Word" << "\t"
<< setw(16) << "Mask" << "\t"
<< "Table size\n";
for(size_type i=0; i<hset.GetWordCount(); ++i) {
cout
<< i << "\t"
<< s_BitsRepr(hset.GetWord(i),16) << "\t"
<< s_BitsRepr(hset.GetMask(i),16) << "\t"
<< hset.GetTableSize(i) << "\n";
}
for(int i=optind; i<argc; ++i) {
const char * ptr=argv[i];
if(strlen(ptr)<wdsize) continue;
hset.Begin(ptr);
for(size_type p=0; p<wdsize; ++p)
cout << "\n" << setw(5) << p << " " << ptr[p];
for(size_type t=0; t<hset.GetWordCount(); ++t) {
cout << " " << (hset.Good(t)?'+':'-')
<< s_BitsRepr(hset.GetValue(t),16);
}
cout << " [" << s_BitsRepr(hset.GetAmbiquityMask(),16) << "]\n";
for(; !hset.End();) {
cout << setw(5) << hset.GetPosition()
<< " " << hset.GetPtr()[0];
if(!hset.Next()) break;
for(size_type t=0; t<hset.GetWordCount(); ++t) {
cout << " " << (hset.Good(t)?'+':'-')
<< s_BitsRepr(hset.GetValue(t),16);
}
cout << " ["
<< s_BitsRepr(hset.GetAmbiquityMask(),16) << "]\n";
}
}
return 0;
}
catch(exception& e) {
cerr << "! Error: " << e.what() << endl;
}
return 100;
}
#endif
/*
* $Log: hashset.cpp,v $
* Revision 1.2 2008/06/16 16:02:40 rotmistr
* *** empty log message ***
*
* Revision 1.1.1.1 2003/12/23 18:17:27 rotmistr
* Package that includes e-PCR, reverse e-PCR, and sequence data preparation
* program for reverse e-PCR looks ready
*
*/
|