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