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