merge the formfield patch from ooo-build
[ooovba.git] / xml2cmp / source / support / heap.cxx
blobea60759a1e5edde07db9f91381d3d4ad708ccd88
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: heap.cxx,v $
10 * $Revision: 1.7 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include <string.h>
32 #include "heap.hxx"
35 #include <iostream>
36 #include <stdlib.h>
37 #define AssertionOf(x) {if (!(x)) {std::cerr << "Assertion failed: " << #x << __FILE__ << __LINE__ << std::endl; exit(3); }}
39 #ifdef UNX
40 #define stricmp strcasecmp
41 #endif
45 Heap::Heap(unsigned i_nWidth)
46 : dpColumnsArray(new Column[i_nWidth]),
47 nColumnsArraySize(i_nWidth),
48 nActiveColumn(nColumnsArraySize-1)
50 for ( unsigned i = 0; i < nColumnsArraySize; i++)
52 dpColumnsArray[i] = 0;
53 } // end for
56 Heap::~Heap()
58 for ( unsigned i = 0; i < nColumnsArraySize; i++)
60 HeapItem * & rColumn = dpColumnsArray[i];
61 for ( HeapItem * pValue = rColumn; pValue != 0; pValue = rColumn )
63 rColumn = rColumn->Next();
64 delete pValue;
66 } // end for
68 delete [] dpColumnsArray;
71 void
72 Heap::InsertValue( const char * i_sKey,
73 const char * i_sValue )
75 HeapItem * pSearch1 = 0;
76 HeapItem * pSearch2 = 0;
77 HeapItem * pNew = new HeapItem(i_sKey, i_sValue);
79 IncColumn();
80 pSearch1 = ActiveColumn();
82 if ( pSearch1 != 0 ? *pNew < *pSearch1 : true )
84 pNew->SetNext( pSearch1 );
85 ActiveColumn() = pNew;
87 if ( pNew->Next() != 0)
89 AssertionOf( *pNew <= *pNew->Next() );
92 return;
97 pSearch2 = pSearch1;
98 pSearch1 = pSearch1->Next();
100 if ( pSearch1 != 0 ? *pNew < *pSearch1 : true )
102 pNew->SetNext( pSearch1 );
103 pSearch2->SetNext(pNew);
106 AssertionOf( *pSearch2 <= *pNew );
107 if ( pNew->Next() != 0)
109 AssertionOf( *pNew <= *pNew->Next() );
113 } while (pSearch2->Next() != pNew);
117 Simstr sKey1;
118 Simstr sValue1;
119 Simstr sKey2;
120 Simstr sValue2;
121 int nCol1 = 0;
122 int nCol2 = 0;
125 HeapItem *
126 Heap::ReleaseTop()
128 unsigned nRetColumn = 0;
129 HeapItem * ret = dpColumnsArray[0];
130 HeapItem * pSearch = 0;
132 for ( unsigned i = 1; i < nColumnsArraySize; ++i )
134 pSearch = dpColumnsArray[i];
135 if (pSearch != 0)
137 if ( ret == 0 ? true : *pSearch < *ret)
139 ret = pSearch;
140 nRetColumn = i;
143 } // for
145 if (ret != 0)
147 dpColumnsArray[nRetColumn] = ret->Next();
149 return ret;
152 void
153 Heap::IncColumn()
155 if (++nActiveColumn >= nColumnsArraySize)
156 nActiveColumn = 0;
161 HeapItem::HeapItem( const char * i_sKey,
162 const char * i_sValue )
163 : sValue(i_sValue),
164 sKey(i_sKey),
165 pNext(0)
169 HeapItem::~HeapItem()
173 bool
174 HeapItem::operator<( const HeapItem & i_rOther ) const
176 int ret = stricmp(sKey.str(), i_rOther.sKey.str());
177 if (ret == 0)
178 ret = strcmp(sKey.str(), i_rOther.sKey.str());
179 if (ret == 0)
180 ret = stricmp(sValue.str(), i_rOther.sValue.str());
181 if (ret == 0)
182 ret = strcmp(sValue.str(), i_rOther.sValue.str());
183 return ret < 0;
186 const Simstr &
187 HeapItem::Value() const
189 return sValue;
192 const Simstr &
193 HeapItem::Key() const
195 return sKey;
198 HeapItem *
199 HeapItem::Next() const
201 return pNext;
204 void
205 HeapItem::SetNext( HeapItem * i_pNext )
207 pNext = i_pNext;