xref: /AOO42X/main/sw/source/filter/ww8/hash_wrap.hxx (revision 9bce9b0d387299c68bd81d539e1478357a103de5)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 
25 //this is a shameless rip from sortedarray.hxx but changed to hash_set
26 
27 #ifndef WW_HASH_WRAP_HXX
28 #define WW_HASH_WRAP_HXX
29 
30 #include <hash_set>
31 #include <tools/debug.hxx>
32 #include <errhdl.hxx>       // ASSERT()
33 
34 //simple wrapper around hash_set to behave like sorted array
35 namespace ww
36 {
37     /** simple template that manages a hash
38 
39 
40         @author
41         <a href="mailto:mikeleib@openoffice.org">Michael Leibowitz</a>
42     */
43     template<class C, class HashFcn = std::hash<C> > class WrappedHash
44     {
45     private:
46         std::hash_set<C, HashFcn> mHashSet;
47 
48         //No copying
49         WrappedHash(const WrappedHash&);
50         WrappedHash& operator=(const WrappedHash&);
51     public:
52         //Find an entry, return its address if found and 0 if not
search(C aSrch) const53         const C* search(C aSrch) const
54         {
55             typename std::hash_set<C, HashFcn>::const_iterator it;
56             it= mHashSet.find(aSrch);
57             if (it != mHashSet.end())
58                 return &(*it);
59             else
60                 return 0;
61         }
62 
WrappedHash(const C * pWwSprmTab,const size_t nNoElems)63         WrappedHash(const C *pWwSprmTab, const size_t nNoElems)
64         {
65             ASSERT(nNoElems && pWwSprmTab, "WW8: empty Array: Don't do that");
66             const C *pIter = pWwSprmTab;
67             const C *pEnd  = pWwSprmTab + nNoElems;
68             while (pIter < pEnd)
69             {
70                 mHashSet.insert(*pIter);
71                 pIter++;
72             }
73 #if OSL_DEBUG_LEVEL > 1
74             bool bBroken=false;
75             rtl::OUString sError;
76             pIter = pWwSprmTab;
77             const C *pBeforeEnd = pWwSprmTab + nNoElems - 1;
78             while (pIter < pBeforeEnd)
79             {
80                 if (*pIter == *(pIter+1))
81                 {
82                     if (!bBroken)
83                     {
84                         sError = rtl::OUString::createFromAscii(
85                             "WW8: Duplicate in list, almost certainly don't "
86                             "want that!\n"
87                             "(You will not see this message again unless you "
88                             "restart)\n"
89                             "Extra entries are...\n");
90                         bBroken=true;
91                     }
92 
93                     size_t nSize = sizeof(C);
94                     const sal_uInt8 *pHack =
95                         reinterpret_cast<const sal_uInt8 *>(&(*pIter));
96                     for (size_t i=0; i < nSize; ++i)
97                     {
98                         sError += rtl::OUString::valueOf(
99                             static_cast<sal_Int32>(pHack[i]), 16);
100                         sError += rtl::OUString::valueOf(sal_Unicode(' '));
101                     }
102                     sError += rtl::OUString::valueOf(sal_Unicode('\n'));
103                     while (*pIter == *(pIter+1) && pIter < pBeforeEnd)
104                         ++pIter;
105                 }
106                 else
107                     ++pIter;
108             }
109             if (bBroken)
110             {
111                DBG_ERROR(rtl::OUStringToOString(sError, RTL_TEXTENCODING_ASCII_US));
112             }
113 #endif
114         }
115     };
116 }
117 #endif
118 
119 /* vi:set tabstop=4 shiftwidth=4 expandtab: */
120