xref: /trunk/main/connectivity/source/inc/TSortIndex.hxx (revision 67f7bfb15893aaa2f3b1ee7ec6b966aaaad422fc)
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 #ifndef CONNECTIVITY_TSORTINDEX_HXX
23 #define CONNECTIVITY_TSORTINDEX_HXX
24 
25 #include "connectivity/dbtoolsdllapi.hxx"
26 #include "TKeyValue.hxx"
27 
28 namespace connectivity
29 {
30     typedef enum
31     {
32         SQL_ORDERBYKEY_NONE,        // do not sort
33         SQL_ORDERBYKEY_DOUBLE,      // numeric key
34         SQL_ORDERBYKEY_STRING       // String Key
35     } OKeyType;
36 
37     typedef enum
38     {
39         SQL_ASC     = 1,            // ascending
40         SQL_DESC    = -1            // otherwise
41     } TAscendingOrder;
42 
43     class OKeySet;
44     class OKeyValue;                // simple class which holds a sal_Int32 and a ::std::vector<ORowSetValueDecoratorRef>
45 
46     /**
47         The class OSortIndex can be used to implement a sorted index.
48         This can depend on the fields which should be sorted.
49     */
50     class OOO_DLLPUBLIC_DBTOOLS OSortIndex
51     {
52     public:
53         typedef ::std::vector< ::std::pair<sal_Int32,OKeyValue*> >  TIntValuePairVector;
54         typedef ::std::vector<OKeyType>                             TKeyTypeVector;
55 
56     private:
57         TIntValuePairVector             m_aKeyValues;
58         TKeyTypeVector                  m_aKeyType;
59         ::std::vector<TAscendingOrder>  m_aAscending;
60         sal_Bool                        m_bFrozen;
61 
62     public:
63 
64         OSortIndex( const ::std::vector<OKeyType>& _aKeyType,
65                     const ::std::vector<TAscendingOrder>& _aAscending);
66 
67         ~OSortIndex();
68 
69         inline static void * SAL_CALL operator new( size_t nSize ) SAL_THROW( () )
70             { return ::rtl_allocateMemory( nSize ); }
71         inline static void * SAL_CALL operator new( size_t,void* _pHint ) SAL_THROW( () )
72             { return _pHint; }
73         inline static void SAL_CALL operator delete( void * pMem ) SAL_THROW( () )
74             { ::rtl_freeMemory( pMem ); }
75         inline static void SAL_CALL operator delete( void *,void* ) SAL_THROW( () )
76             {  }
77 
78 
79         /**
80             AddKeyValue appends a new value.
81             @param
82                 pKeyValue   the keyvalue to be appended
83             ATTENTION: when the sortindex is already frozen the parameter will be deleted
84         */
85         void AddKeyValue(OKeyValue * pKeyValue);
86 
87         /**
88             Freeze freezes the sortindex so that new values could only be appended by their value
89         */
90         void Freeze();
91 
92         /**
93             CreateKeySet creates the keyset which values could be used to travel in your table/result
94             The returned keyset is frozen.
95         */
96         ::vos::ORef<OKeySet> CreateKeySet();
97 
98 
99 
100         // look at the name
101         sal_Bool IsFrozen() const { return m_bFrozen; }
102         // returns the current size of the keyvalues
103         sal_Int32 Count()   const { return m_aKeyValues.size(); }
104         /** GetValue returns the value at position nPos (1..n) [sorted access].
105             It only allowed to call this method after the sortindex has been frozen.
106         */
107 
108         sal_Int32 GetValue(sal_Int32 nPos) const;
109 
110         inline const ::std::vector<OKeyType>& getKeyType() const { return m_aKeyType; }
111         inline TAscendingOrder getAscending(::std::vector<TAscendingOrder>::size_type _nPos) const { return m_aAscending[_nPos]; }
112 
113     };
114 
115     /**
116         The class OKeySet is a refcountable vector which also has a state.
117         This state gives information about if the keyset is fixed.
118     */
119     class OOO_DLLPUBLIC_DBTOOLS OKeySet : public ORefVector<sal_Int32>
120     {
121         sal_Bool m_bFrozen;
122     public:
123         OKeySet()
124             : ORefVector<sal_Int32>()
125             , m_bFrozen(sal_False){}
126         OKeySet(Vector::size_type _nSize)
127             : ORefVector<sal_Int32>(_nSize)
128             , m_bFrozen(sal_False){}
129 
130         sal_Bool    isFrozen() const                        { return m_bFrozen; }
131         void        setFrozen(sal_Bool _bFrozen=sal_True)   { m_bFrozen = _bFrozen; }
132     };
133 }
134 #endif // CONNECTIVITY_TSORTINDEX_HXX
135 
136 /* vim: set noet sw=4 ts=4: */
137