xref: /AOO42X/main/sw/source/filter/ww8/sortedarray.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 #ifndef WW_SORTEDARRAY_HXX
26 #define WW_SORTEDARRAY_HXX
27 
28 #include <algorithm>
29 #include <errhdl.hxx>       // ASSERT()
30 #include <tools/debug.hxx>
31 
32 //simple template that manages a static [] array by sorting at construction
33 
34 namespace ww
35 {
36     /** simple template that manages a static array
37 
38         The template sorts the array at construction in place.
39 
40         @author
41         <a href="mailto:cmc@openoffice.org">Caol&aacute;n McNamara</a>
42     */
43     template<class C> class SortedArray
44     {
45     private:
46         //The array e.g. of sprms.
47         C *mpWwSprmTab;
48         size_t mnNoElems;
49 
50         //No copying
51         SortedArray(const SortedArray&);
52         SortedArray& operator=(const SortedArray&);
53     public:
54         //Find an entry, return its address if found and 0 if not
search(C aSrch) const55         const C *search(C aSrch) const
56         {
57             std::pair<C *, C *> aPair =
58                 std::equal_range(mpWwSprmTab, mpWwSprmTab + mnNoElems, aSrch);
59             if (aPair.first != aPair.second)
60                 return aPair.first;
61             else
62                 return 0;
63         }
64 
SortedArray(C * pWwSprmTab,size_t nNoElems)65         SortedArray(C *pWwSprmTab, size_t nNoElems)
66             : mpWwSprmTab(pWwSprmTab), mnNoElems(nNoElems)
67         {
68             ASSERT(mnNoElems && pWwSprmTab, "WW8: empty Array: Don't do that");
69             std::sort(mpWwSprmTab, mpWwSprmTab + mnNoElems);
70 #if OSL_DEBUG_LEVEL > 1
71             bool bBroken=false;
72             rtl::OUString sError;
73             const C *pIter = mpWwSprmTab;
74             const C *pBeforeEnd = mpWwSprmTab + mnNoElems - 1;
75             while (pIter < pBeforeEnd)
76             {
77                 if (*pIter == *(pIter+1))
78                 {
79                     if (!bBroken)
80                     {
81                         sError = rtl::OUString::createFromAscii(
82                             "WW8: Duplicate in list, almost certainly don't "
83                             "want that!\n"
84                             "(You will not see this message again unless you "
85                             "restart)\n"
86                             "Extra entries are...\n");
87                         bBroken=true;
88                     }
89 
90                     size_t nSize = sizeof(C);
91                     const sal_uInt8 *pHack =
92                         reinterpret_cast<const sal_uInt8 *>(&(*pIter));
93                     for (size_t i=0; i < nSize; ++i)
94                     {
95                         sError += rtl::OUString::valueOf(
96                             static_cast<sal_Int32>(pHack[i]), 16);
97                         sError += rtl::OUString::valueOf(sal_Unicode(' '));
98                     }
99                     sError += rtl::OUString::valueOf(sal_Unicode('\n'));
100                     while (*pIter == *(pIter+1) && pIter < pBeforeEnd)
101                         ++pIter;
102                 }
103                 else
104                     ++pIter;
105             }
106             if (bBroken)
107             {
108                DBG_ERROR(rtl::OUStringToOString(sError, RTL_TEXTENCODING_ASCII_US));
109             }
110 #endif
111         }
112     };
113 }
114 #endif
115 
116 /* vi:set tabstop=4 shiftwidth=4 expandtab: */
117