BOSS 8.0.0
BESIII Offline Software System
Loading...
Searching...
No Matches
IfdStrKey.cxx
Go to the documentation of this file.
1//--------------------------------------------------------------------------
2// File and Version Information:
3// $Id: IfdStrKey.cxx,v 1.1.1.1 2005/04/21 01:18:05 zhangy Exp $
4//
5// Description:
6// Implementation of IfdStrKey. See .hh for details.
7//
8// Author List:
9// Ed Frank University of Pennsylvania
10//
11// History:
12// Ed Frank 18 Sep 97 Creation of first version
13//
14// Copyright Information:
15// Copyright (C) 1997
16//
17// Bugs:
18//
19//------------------------------------------------------------------------
20
21// #include "BaBar/BaBar.hh"
22#include "ProxyDict/IfdStrKey.h"
23#if !( defined( __GNUC__ ) && ( __GNUC__ < 3 ) && \
24 ( __GNUC_MINOR__ < 95 ) ) // BABAR_IOSTREAMS_MIGRATION
25using std::ostream;
26#endif // BABAR_IOSTREAMS_MIGRATION
27
28//****************************************************************************
29IfdStrKey::IfdStrKey( const char* s )
30 //****************************************************************************
31
32 : IfdKey( strKey )
33
34{
35
36 // NB: strncpy would be nice below, but we've already died in strlen
37 // if thats an issue.
38
39 register size_t strSize = strlen( s ) + 1;
40
41 // If the space allocated in this instance of IfdStrKey is big enough
42 // to hold the string, then put it there; otherwise do the slower operation
43 // of allocating it in the heap. Either way, make strVal point at it
44 // to avoid "if allocated" sort of logic later.
45
46 if ( strSize < IFDKEY_BUFSIZE )
47 {
48 strcpy( _stringBuf, s );
49 strVal = &_stringBuf[0];
50 _onHeap = false;
51 }
52 else
53 {
54 strVal = new char[strSize];
55 strcpy( strVal, s );
56 _onHeap = true;
57 }
58
59 enum {
60 MaxChar = 64 // Limit computation. Don't need strlen()
61 };
62
63 _hashVal = 0;
64
65 size_t n = 0;
66 while ( ( s[n] != '\0' ) && ( n < MaxChar ) )
67 {
68 _hashVal = ( ( _hashVal << 8 ) + s[n] ) % _nHashBuckets;
69 n++;
70 }
71}
72
73#ifndef VXWORKS
74//****************************************************************************
75IfdStrKey::IfdStrKey( const std::string& str )
76 //****************************************************************************
77
78 : IfdKey( strKey )
79
80{
81 using std::string;
82 const char* s = str.c_str();
83 register size_t strSize = str.size() + 1;
84
85 // register size_t strSize = strlen(s)+1;
86
87 // If the space allocated in this instance of IfdStrKey is big enough
88 // to hold the string, then put it there; otherwise do the slower operation
89 // of allocating it in the heap. Either way, make strVal point at it
90 // to avoid "if allocated" sort of logic later.
91
92 if ( strSize < IFDKEY_BUFSIZE )
93 {
94 strcpy( _stringBuf, s );
95 strVal = &_stringBuf[0];
96 _onHeap = false;
97 }
98 else
99 {
100 strVal = new char[strSize];
101 strcpy( strVal, s );
102 _onHeap = true;
103 }
104
105 enum {
106 MaxChar = 64 // Limit computation. Don't need strlen()
107 };
108
109 _hashVal = 0;
110
111 size_t n = 0;
112 while ( ( s[n] != '\0' ) && ( n < MaxChar ) )
113 {
114 _hashVal = ( ( _hashVal << 8 ) + s[n] ) % _nHashBuckets;
115 n++;
116 }
117}
118#endif
119
120//****************************************************************************
121IfdStrKey::IfdStrKey( const char* s, unsigned int hashVal )
122 //****************************************************************************
123 // Used for the clone function. We keep this private because we don't trust
124 // anyone else to set the hash value correctly/consistently. The aim here
125 // is to save the cost of calling the hash function during a clone.
126
127 : IfdKey( strKey ) {
128
129 // This code is cut/paste from the other constructor. This is harder
130 // to maintain but in this case we very much need the speed gained by
131 // saving a call.
132
133 register size_t strSize = strlen( s ) + 1;
134
135 if ( strSize < IFDKEY_BUFSIZE )
136 {
137 strcpy( _stringBuf, s );
138 strVal = &_stringBuf[0];
139 _onHeap = false;
140 }
141 else
142 {
143 strVal = new char[strlen( s ) + 1];
144 strcpy( strVal, s );
145 _onHeap = true;
146 }
147
148 _hashVal = hashVal; // Override the IfdKey base class init of this.
149}
150
151//****************************************************************************
153 //****************************************************************************
154 if ( _onHeap ) delete[] strVal;
155 strVal = 0;
156}
157
158//****************************************************************************
159IfdKey* IfdStrKey::clone( void ) const {
160 //****************************************************************************
161 return new IfdStrKey( strVal, _hashVal );
162}
163
164//****************************************************************************
165int IfdStrKey::operator==( const IfdKey& k ) const {
166 //****************************************************************************
167
168 if ( ( strKey == k.getKeyKind() ) && ( _hashVal == k._hashVal ) )
169 {
170
171 // Then they might actually match! Check the strings.
172 // We used to use strcmp, but this is faster by x2.2.
173 char* s1 = strVal;
174 char* s2 = k.strVal;
175 while ( *s1 == *s2++ )
176 {
177 if ( *s1++ == '\0' ) return 1; // NB: == in while => *s2 == \0 too.
178 }
179 }
180
181 return 0;
182}
183
184//****************************************************************************
185int IfdStrKey::operator<( const IfdKey& k ) const {
186 //****************************************************************************
187 return ( strKey == k.getKeyKind() ) && ( strcmp( strVal, k.strVal ) < 0 );
188}
189
190//****************************************************************************
191void IfdStrKey::print( ostream& o ) const {
192 //****************************************************************************
193 o << "IfdStrKey(" << strVal << ")";
194}
const Int_t n
XmlRpcServer s
IfdKey(keyKind kind)
Definition IfdKey.cxx:23
virtual void print(std::ostream &o) const
virtual int operator==(const IfdKey &k) const
virtual ~IfdStrKey()
virtual int operator<(const IfdKey &k) const
virtual IfdKey * clone(void) const