xref: /trunk/main/sw/source/filter/ww8/hash_wrap.hxx (revision 1d2dbeb0)
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 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil -*- */
25 
26 //this is a shameless rip from sortedarray.hxx but changed to hash_set
27 
28 #ifndef WW_HASH_WRAP_HXX
29 #define WW_HASH_WRAP_HXX
30 
31 #include <hash_set>
32 #include <tools/debug.hxx>
33 #include <errhdl.hxx>       // ASSERT()
34 
35 //simple wrapper around hash_set to behave like sorted array
36 namespace ww
37 {
38     /** simple template that manages a hash
39 
40 
41         @author
42         <a href="mailto:mikeleib@openoffice.org">Michael Leibowitz</a>
43     */
44     template<class C, class HashFcn = std::hash<C> > class WrappedHash
45     {
46     private:
47         std::hash_set<C, HashFcn> mHashSet;
48 
49         //No copying
50         WrappedHash(const WrappedHash&);
51         WrappedHash& operator=(const WrappedHash&);
52     public:
53         //Find an entry, return its address if found and 0 if not
search(C aSrch) const54         const C* search(C aSrch) const
55         {
56             typename std::hash_set<C, HashFcn>::const_iterator it;
57             it= mHashSet.find(aSrch);
58             if (it != mHashSet.end())
59                 return &(*it);
60             else
61                 return 0;
62         }
63 
WrappedHash(const C * pWwSprmTab,const size_t nNoElems)64         WrappedHash(const C *pWwSprmTab, const size_t nNoElems)
65         {
66             ASSERT(nNoElems && pWwSprmTab, "WW8: empty Array: Don't do that");
67             const C *pIter = pWwSprmTab;
68             const C *pEnd  = pWwSprmTab + nNoElems;
69             while (pIter < pEnd)
70             {
71                 mHashSet.insert(*pIter);
72                 pIter++;
73             }
74 #if OSL_DEBUG_LEVEL > 1
75             bool bBroken=false;
76             rtl::OUString sError;
77             pIter = pWwSprmTab;
78             const C *pBeforeEnd = pWwSprmTab + nNoElems - 1;
79             while (pIter < pBeforeEnd)
80             {
81                 if (*pIter == *(pIter+1))
82                 {
83                     if (!bBroken)
84                     {
85                         sError = rtl::OUString::createFromAscii(
86                             "WW8: Duplicate in list, almost certainly don't "
87                             "want that!\n"
88                             "(You will not see this message again unless you "
89                             "restart)\n"
90                             "Extra entries are...\n");
91                         bBroken=true;
92                     }
93 
94                     size_t nSize = sizeof(C);
95                     const sal_uInt8 *pHack =
96                         reinterpret_cast<const sal_uInt8 *>(&(*pIter));
97                     for (size_t i=0; i < nSize; ++i)
98                     {
99                         sError += rtl::OUString::valueOf(
100                             static_cast<sal_Int32>(pHack[i]), 16);
101                         sError += rtl::OUString::valueOf(sal_Unicode(' '));
102                     }
103                     sError += rtl::OUString::valueOf(sal_Unicode('\n'));
104                     while (*pIter == *(pIter+1) && pIter < pBeforeEnd)
105                         ++pIter;
106                 }
107                 else
108                     ++pIter;
109             }
110             if (bBroken)
111             {
112                DBG_ERROR(rtl::OUStringToOString(sError, RTL_TEXTENCODING_ASCII_US));
113             }
114 #endif
115         }
116     };
117 }
118 #endif
119 
120 /* vi:set tabstop=4 shiftwidth=4 expandtab: */
121