1*ab595ff6SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*ab595ff6SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*ab595ff6SAndrew Rist * or more contributor license agreements. See the NOTICE file
5*ab595ff6SAndrew Rist * distributed with this work for additional information
6*ab595ff6SAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*ab595ff6SAndrew Rist * to you under the Apache License, Version 2.0 (the
8*ab595ff6SAndrew Rist * "License"); you may not use this file except in compliance
9*ab595ff6SAndrew Rist * with the License. You may obtain a copy of the License at
10*ab595ff6SAndrew Rist *
11*ab595ff6SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12*ab595ff6SAndrew Rist *
13*ab595ff6SAndrew Rist * Unless required by applicable law or agreed to in writing,
14*ab595ff6SAndrew Rist * software distributed under the License is distributed on an
15*ab595ff6SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ab595ff6SAndrew Rist * KIND, either express or implied. See the License for the
17*ab595ff6SAndrew Rist * specific language governing permissions and limitations
18*ab595ff6SAndrew Rist * under the License.
19*ab595ff6SAndrew Rist *
20*ab595ff6SAndrew Rist *************************************************************/
21*ab595ff6SAndrew Rist
22*ab595ff6SAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir #include <string.h>
25cdf0e10cSrcweir #include "heap.hxx"
26cdf0e10cSrcweir
27cdf0e10cSrcweir
28cdf0e10cSrcweir #include <iostream>
29cdf0e10cSrcweir #include <stdlib.h>
30cdf0e10cSrcweir #define AssertionOf(x) {if (!(x)) {std::cerr << "Assertion failed: " << #x << __FILE__ << __LINE__ << std::endl; exit(3); }}
31cdf0e10cSrcweir
32cdf0e10cSrcweir #ifdef UNX
33cdf0e10cSrcweir #define stricmp strcasecmp
34cdf0e10cSrcweir #endif
35cdf0e10cSrcweir
36cdf0e10cSrcweir
37cdf0e10cSrcweir
Heap(unsigned i_nWidth)38cdf0e10cSrcweir Heap::Heap(unsigned i_nWidth)
39cdf0e10cSrcweir : dpColumnsArray(new Column[i_nWidth]),
40cdf0e10cSrcweir nColumnsArraySize(i_nWidth),
41cdf0e10cSrcweir nActiveColumn(nColumnsArraySize-1)
42cdf0e10cSrcweir {
43cdf0e10cSrcweir for ( unsigned i = 0; i < nColumnsArraySize; i++)
44cdf0e10cSrcweir {
45cdf0e10cSrcweir dpColumnsArray[i] = 0;
46cdf0e10cSrcweir } // end for
47cdf0e10cSrcweir }
48cdf0e10cSrcweir
~Heap()49cdf0e10cSrcweir Heap::~Heap()
50cdf0e10cSrcweir {
51cdf0e10cSrcweir for ( unsigned i = 0; i < nColumnsArraySize; i++)
52cdf0e10cSrcweir {
53cdf0e10cSrcweir HeapItem * & rColumn = dpColumnsArray[i];
54cdf0e10cSrcweir for ( HeapItem * pValue = rColumn; pValue != 0; pValue = rColumn )
55cdf0e10cSrcweir {
56cdf0e10cSrcweir rColumn = rColumn->Next();
57cdf0e10cSrcweir delete pValue;
58cdf0e10cSrcweir }
59cdf0e10cSrcweir } // end for
60cdf0e10cSrcweir
61cdf0e10cSrcweir delete [] dpColumnsArray;
62cdf0e10cSrcweir }
63cdf0e10cSrcweir
64cdf0e10cSrcweir void
InsertValue(const char * i_sKey,const char * i_sValue)65cdf0e10cSrcweir Heap::InsertValue( const char * i_sKey,
66cdf0e10cSrcweir const char * i_sValue )
67cdf0e10cSrcweir {
68cdf0e10cSrcweir HeapItem * pSearch1 = 0;
69cdf0e10cSrcweir HeapItem * pSearch2 = 0;
70cdf0e10cSrcweir HeapItem * pNew = new HeapItem(i_sKey, i_sValue);
71cdf0e10cSrcweir
72cdf0e10cSrcweir IncColumn();
73cdf0e10cSrcweir pSearch1 = ActiveColumn();
74cdf0e10cSrcweir
75cdf0e10cSrcweir if ( pSearch1 != 0 ? *pNew < *pSearch1 : true )
76cdf0e10cSrcweir {
77cdf0e10cSrcweir pNew->SetNext( pSearch1 );
78cdf0e10cSrcweir ActiveColumn() = pNew;
79cdf0e10cSrcweir
80cdf0e10cSrcweir if ( pNew->Next() != 0)
81cdf0e10cSrcweir {
82cdf0e10cSrcweir AssertionOf( *pNew <= *pNew->Next() );
83cdf0e10cSrcweir }
84cdf0e10cSrcweir
85cdf0e10cSrcweir return;
86cdf0e10cSrcweir }
87cdf0e10cSrcweir
88cdf0e10cSrcweir do
89cdf0e10cSrcweir {
90cdf0e10cSrcweir pSearch2 = pSearch1;
91cdf0e10cSrcweir pSearch1 = pSearch1->Next();
92cdf0e10cSrcweir
93cdf0e10cSrcweir if ( pSearch1 != 0 ? *pNew < *pSearch1 : true )
94cdf0e10cSrcweir {
95cdf0e10cSrcweir pNew->SetNext( pSearch1 );
96cdf0e10cSrcweir pSearch2->SetNext(pNew);
97cdf0e10cSrcweir
98cdf0e10cSrcweir
99cdf0e10cSrcweir AssertionOf( *pSearch2 <= *pNew );
100cdf0e10cSrcweir if ( pNew->Next() != 0)
101cdf0e10cSrcweir {
102cdf0e10cSrcweir AssertionOf( *pNew <= *pNew->Next() );
103cdf0e10cSrcweir }
104cdf0e10cSrcweir
105cdf0e10cSrcweir }
106cdf0e10cSrcweir } while (pSearch2->Next() != pNew);
107cdf0e10cSrcweir }
108cdf0e10cSrcweir
109cdf0e10cSrcweir
110cdf0e10cSrcweir Simstr sKey1;
111cdf0e10cSrcweir Simstr sValue1;
112cdf0e10cSrcweir Simstr sKey2;
113cdf0e10cSrcweir Simstr sValue2;
114cdf0e10cSrcweir int nCol1 = 0;
115cdf0e10cSrcweir int nCol2 = 0;
116cdf0e10cSrcweir
117cdf0e10cSrcweir
118cdf0e10cSrcweir HeapItem *
ReleaseTop()119cdf0e10cSrcweir Heap::ReleaseTop()
120cdf0e10cSrcweir {
121cdf0e10cSrcweir unsigned nRetColumn = 0;
122cdf0e10cSrcweir HeapItem * ret = dpColumnsArray[0];
123cdf0e10cSrcweir HeapItem * pSearch = 0;
124cdf0e10cSrcweir
125cdf0e10cSrcweir for ( unsigned i = 1; i < nColumnsArraySize; ++i )
126cdf0e10cSrcweir {
127cdf0e10cSrcweir pSearch = dpColumnsArray[i];
128cdf0e10cSrcweir if (pSearch != 0)
129cdf0e10cSrcweir {
130cdf0e10cSrcweir if ( ret == 0 ? true : *pSearch < *ret)
131cdf0e10cSrcweir {
132cdf0e10cSrcweir ret = pSearch;
133cdf0e10cSrcweir nRetColumn = i;
134cdf0e10cSrcweir }
135cdf0e10cSrcweir }
136cdf0e10cSrcweir } // for
137cdf0e10cSrcweir
138cdf0e10cSrcweir if (ret != 0)
139cdf0e10cSrcweir {
140cdf0e10cSrcweir dpColumnsArray[nRetColumn] = ret->Next();
141cdf0e10cSrcweir }
142cdf0e10cSrcweir return ret;
143cdf0e10cSrcweir }
144cdf0e10cSrcweir
145cdf0e10cSrcweir void
IncColumn()146cdf0e10cSrcweir Heap::IncColumn()
147cdf0e10cSrcweir {
148cdf0e10cSrcweir if (++nActiveColumn >= nColumnsArraySize)
149cdf0e10cSrcweir nActiveColumn = 0;
150cdf0e10cSrcweir }
151cdf0e10cSrcweir
152cdf0e10cSrcweir
153cdf0e10cSrcweir
HeapItem(const char * i_sKey,const char * i_sValue)154cdf0e10cSrcweir HeapItem::HeapItem( const char * i_sKey,
155cdf0e10cSrcweir const char * i_sValue )
156cdf0e10cSrcweir : sValue(i_sValue),
157cdf0e10cSrcweir sKey(i_sKey),
158cdf0e10cSrcweir pNext(0)
159cdf0e10cSrcweir {
160cdf0e10cSrcweir }
161cdf0e10cSrcweir
~HeapItem()162cdf0e10cSrcweir HeapItem::~HeapItem()
163cdf0e10cSrcweir {
164cdf0e10cSrcweir }
165cdf0e10cSrcweir
166cdf0e10cSrcweir bool
operator <(const HeapItem & i_rOther) const167cdf0e10cSrcweir HeapItem::operator<( const HeapItem & i_rOther ) const
168cdf0e10cSrcweir {
169cdf0e10cSrcweir int ret = stricmp(sKey.str(), i_rOther.sKey.str());
170cdf0e10cSrcweir if (ret == 0)
171cdf0e10cSrcweir ret = strcmp(sKey.str(), i_rOther.sKey.str());
172cdf0e10cSrcweir if (ret == 0)
173cdf0e10cSrcweir ret = stricmp(sValue.str(), i_rOther.sValue.str());
174cdf0e10cSrcweir if (ret == 0)
175cdf0e10cSrcweir ret = strcmp(sValue.str(), i_rOther.sValue.str());
176cdf0e10cSrcweir return ret < 0;
177cdf0e10cSrcweir }
178cdf0e10cSrcweir
179cdf0e10cSrcweir const Simstr &
Value() const180cdf0e10cSrcweir HeapItem::Value() const
181cdf0e10cSrcweir {
182cdf0e10cSrcweir return sValue;
183cdf0e10cSrcweir }
184cdf0e10cSrcweir
185cdf0e10cSrcweir const Simstr &
Key() const186cdf0e10cSrcweir HeapItem::Key() const
187cdf0e10cSrcweir {
188cdf0e10cSrcweir return sKey;
189cdf0e10cSrcweir }
190cdf0e10cSrcweir
191cdf0e10cSrcweir HeapItem *
Next() const192cdf0e10cSrcweir HeapItem::Next() const
193cdf0e10cSrcweir {
194cdf0e10cSrcweir return pNext;
195cdf0e10cSrcweir }
196cdf0e10cSrcweir
197cdf0e10cSrcweir void
SetNext(HeapItem * i_pNext)198cdf0e10cSrcweir HeapItem::SetNext( HeapItem * i_pNext )
199cdf0e10cSrcweir {
200cdf0e10cSrcweir pNext = i_pNext;
201cdf0e10cSrcweir }
202cdf0e10cSrcweir
203cdf0e10cSrcweir
204