1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 // vim:cindent:ts=4:et:sw=4:
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 * Code to sort cells by their colspan, used by BasicTableLayoutStrategy.
11 #include "SpanningCellSorter.h"
12 #include "nsQuickSort.h"
13 #include "mozilla/HashFunctions.h"
15 using namespace mozilla
;
17 // #define DEBUG_SPANNING_CELL_SORTER
19 SpanningCellSorter::SpanningCellSorter()
21 mHashTable(&HashTableOps
, sizeof(HashTableEntry
)),
22 mSortedHashTable(nullptr) {
23 memset(mArray
, 0, sizeof(mArray
));
26 SpanningCellSorter::~SpanningCellSorter() { delete[] mSortedHashTable
; }
28 /* static */ const PLDHashTableOps
SpanningCellSorter::HashTableOps
= {
29 HashTableHashKey
, HashTableMatchEntry
, PLDHashTable::MoveEntryStub
,
30 PLDHashTable::ClearEntryStub
, nullptr};
33 PLDHashNumber
SpanningCellSorter::HashTableHashKey(const void* key
) {
34 return HashGeneric(key
);
38 bool SpanningCellSorter::HashTableMatchEntry(const PLDHashEntryHdr
* hdr
,
40 const HashTableEntry
* entry
= static_cast<const HashTableEntry
*>(hdr
);
41 return NS_PTR_TO_INT32(key
) == entry
->mColSpan
;
44 bool SpanningCellSorter::AddCell(int32_t aColSpan
, int32_t aRow
, int32_t aCol
) {
45 NS_ASSERTION(mState
== ADDING
, "cannot call AddCell after GetNext");
46 NS_ASSERTION(aColSpan
>= ARRAY_BASE
, "cannot add cells with colspan<2");
48 Item
* i
= (Item
*)mozilla::AutoStackArena::Allocate(sizeof(Item
));
49 NS_ENSURE_TRUE(i
!= nullptr, false);
54 if (UseArrayForSpan(aColSpan
)) {
55 int32_t index
= SpanToIndex(aColSpan
);
56 i
->next
= mArray
[index
];
59 auto entry
= static_cast<HashTableEntry
*>(
60 mHashTable
.Add(NS_INT32_TO_PTR(aColSpan
), fallible
));
61 NS_ENSURE_TRUE(entry
, false);
63 NS_ASSERTION(entry
->mColSpan
== 0 || entry
->mColSpan
== aColSpan
,
65 NS_ASSERTION((entry
->mColSpan
== 0) == (entry
->mItems
== nullptr),
66 "entry should be either new or properly initialized");
67 entry
->mColSpan
= aColSpan
;
69 i
->next
= entry
->mItems
;
77 int SpanningCellSorter::SortArray(const void* a
, const void* b
, void* closure
) {
78 int32_t spanA
= (*static_cast<HashTableEntry
* const*>(a
))->mColSpan
;
79 int32_t spanB
= (*static_cast<HashTableEntry
* const*>(b
))->mColSpan
;
81 if (spanA
< spanB
) return -1;
82 if (spanA
== spanB
) return 0;
86 SpanningCellSorter::Item
* SpanningCellSorter::GetNext(int32_t* aColSpan
) {
87 NS_ASSERTION(mState
!= DONE
, "done enumerating, stop calling");
91 /* prepare to enumerate the array */
92 mState
= ENUMERATING_ARRAY
;
93 mEnumerationIndex
= 0;
95 case ENUMERATING_ARRAY
:
96 while (mEnumerationIndex
< ARRAY_SIZE
&& !mArray
[mEnumerationIndex
])
98 if (mEnumerationIndex
< ARRAY_SIZE
) {
99 Item
* result
= mArray
[mEnumerationIndex
];
100 *aColSpan
= IndexToSpan(mEnumerationIndex
);
101 NS_ASSERTION(result
, "logic error");
102 #ifdef DEBUG_SPANNING_CELL_SORTER
104 "SpanningCellSorter[%p]:"
105 " returning list for colspan=%d from array\n",
106 static_cast<void*>(this), *aColSpan
);
111 /* prepare to enumerate the hash */
112 mState
= ENUMERATING_HASH
;
113 mEnumerationIndex
= 0;
114 if (mHashTable
.EntryCount() > 0) {
115 HashTableEntry
** sh
= new HashTableEntry
*[mHashTable
.EntryCount()];
117 for (auto iter
= mHashTable
.ConstIter(); !iter
.Done(); iter
.Next()) {
118 sh
[j
++] = static_cast<HashTableEntry
*>(iter
.Get());
120 NS_QuickSort(sh
, mHashTable
.EntryCount(), sizeof(sh
[0]), SortArray
,
122 mSortedHashTable
= sh
;
125 case ENUMERATING_HASH
:
126 if (mEnumerationIndex
< mHashTable
.EntryCount()) {
127 Item
* result
= mSortedHashTable
[mEnumerationIndex
]->mItems
;
128 *aColSpan
= mSortedHashTable
[mEnumerationIndex
]->mColSpan
;
129 NS_ASSERTION(result
, "holes in hash table");
130 #ifdef DEBUG_SPANNING_CELL_SORTER
132 "SpanningCellSorter[%p]:"
133 " returning list for colspan=%d from hash\n",
134 static_cast<void*>(this), *aColSpan
);