1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: heap.cxx,v $
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 ************************************************************************/
37 #define AssertionOf(x) {if (!(x)) {std::cerr << "Assertion failed: " << #x << __FILE__ << __LINE__ << std::endl; exit(3); }}
40 #define stricmp strcasecmp
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;
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();
68 delete [] dpColumnsArray
;
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
);
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() );
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
);
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
];
137 if ( ret
== 0 ? true : *pSearch
< *ret
)
147 dpColumnsArray
[nRetColumn
] = ret
->Next();
155 if (++nActiveColumn
>= nColumnsArraySize
)
161 HeapItem::HeapItem( const char * i_sKey
,
162 const char * i_sValue
)
169 HeapItem::~HeapItem()
174 HeapItem::operator<( const HeapItem
& i_rOther
) const
176 int ret
= stricmp(sKey
.str(), i_rOther
.sKey
.str());
178 ret
= strcmp(sKey
.str(), i_rOther
.sKey
.str());
180 ret
= stricmp(sValue
.str(), i_rOther
.sValue
.str());
182 ret
= strcmp(sValue
.str(), i_rOther
.sValue
.str());
187 HeapItem::Value() const
193 HeapItem::Key() const
199 HeapItem::Next() const
205 HeapItem::SetNext( HeapItem
* i_pNext
)