Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #ifndef WW_SORTEDARRAY_HXX
30 : : #define WW_SORTEDARRAY_HXX
31 : :
32 : : #include <algorithm>
33 : :
34 : : //simple template that manages a static [] array by sorting at construction
35 : :
36 : : namespace ww
37 : : {
38 : : /** simple template that manages a static array
39 : :
40 : : The template sorts the array at construction in place.
41 : :
42 : : @author
43 : : <a href="mailto:cmc@openoffice.org">Caolán McNamara</a>
44 : : */
45 : : template<class C> class SortedArray
46 : : {
47 : : private:
48 : : //The array e.g. of sprms.
49 : : C *mpWwSprmTab;
50 : : size_t mnNoElems;
51 : :
52 : : //No copying
53 : : SortedArray(const SortedArray&);
54 : : SortedArray& operator=(const SortedArray&);
55 : : public:
56 : : //Find an entry, return its address if found and 0 if not
57 : 51663 : const C *search(C aSrch) const
58 : : {
59 : : std::pair<C *, C *> aPair =
60 [ + - ]: 51663 : std::equal_range(mpWwSprmTab, mpWwSprmTab + mnNoElems, aSrch);
61 [ + + ]: 51663 : if (aPair.first != aPair.second)
62 : 48693 : return aPair.first;
63 : : else
64 : 51663 : return 0;
65 : : }
66 : :
67 : 12 : SortedArray(C *pWwSprmTab, size_t nNoElems)
68 : 12 : : mpWwSprmTab(pWwSprmTab), mnNoElems(nNoElems)
69 : : {
70 : : OSL_ENSURE(mnNoElems && pWwSprmTab, "WW8: empty Array: Don't do that");
71 : 12 : std::sort(mpWwSprmTab, mpWwSprmTab + mnNoElems);
72 : : #if OSL_DEBUG_LEVEL > 1
73 : : bool bBroken=false;
74 : : rtl::OUString sError;
75 : : const C *pIter = mpWwSprmTab;
76 : : const C *pBeforeEnd = mpWwSprmTab + mnNoElems - 1;
77 : : while (pIter < pBeforeEnd)
78 : : {
79 : : if (*pIter == *(pIter+1))
80 : : {
81 : : if (!bBroken)
82 : : {
83 : : sError =
84 : : "WW8: Duplicate in list, almost certainly don't "
85 : : "want that!\n"
86 : : "(You will not see this message again unless you "
87 : : "restart)\n"
88 : : "Extra entries are...\n";
89 : : bBroken=true;
90 : : }
91 : :
92 : : size_t nSize = sizeof(C);
93 : : const sal_uInt8 *pHack =
94 : : reinterpret_cast<const sal_uInt8 *>(&(*pIter));
95 : : for (size_t i=0; i < nSize; ++i)
96 : : {
97 : : sError += rtl::OUString::valueOf(
98 : : static_cast<sal_Int32>(pHack[i]), 16);
99 : : sError += rtl::OUString::valueOf(sal_Unicode(' '));
100 : : }
101 : : sError += rtl::OUString::valueOf(sal_Unicode('\n'));
102 : : while (*pIter == *(pIter+1) && pIter < pBeforeEnd)
103 : : ++pIter;
104 : : }
105 : : else
106 : : ++pIter;
107 : : }
108 : : if (bBroken)
109 : : {
110 : : OSL_FAIL( rtl::OUStringToOString( sError, RTL_TEXTENCODING_ASCII_US ).getStr() );
111 : : }
112 : : #endif
113 : 12 : }
114 : : };
115 : : }
116 : : #endif
117 : :
118 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|