2007-01-13 04:04:56 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
// vim:cindent:ts=4:et:sw=4:
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-01-13 04:04:56 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Code to sort cells by their colspan, used by BasicTableLayoutStrategy.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "SpanningCellSorter.h"
|
|
|
|
#include "nsQuickSort.h"
|
2010-04-11 00:09:38 +04:00
|
|
|
#include "nsIPresShell.h"
|
2017-07-08 07:48:22 +03:00
|
|
|
#include "mozilla/HashFunctions.h"
|
2007-01-13 04:04:56 +03:00
|
|
|
|
|
|
|
//#define DEBUG_SPANNING_CELL_SORTER
|
|
|
|
|
2012-07-15 08:20:24 +04:00
|
|
|
SpanningCellSorter::SpanningCellSorter()
|
|
|
|
: mState(ADDING)
|
2015-05-06 02:11:43 +03:00
|
|
|
, mHashTable(&HashTableOps, sizeof(HashTableEntry))
|
2012-07-30 18:20:58 +04:00
|
|
|
, mSortedHashTable(nullptr)
|
2007-01-13 04:04:56 +03:00
|
|
|
{
|
|
|
|
memset(mArray, 0, sizeof(mArray));
|
|
|
|
}
|
|
|
|
|
|
|
|
SpanningCellSorter::~SpanningCellSorter()
|
|
|
|
{
|
2007-04-23 22:40:07 +04:00
|
|
|
delete [] mSortedHashTable;
|
2007-01-13 04:04:56 +03:00
|
|
|
}
|
|
|
|
|
2013-11-19 06:51:48 +04:00
|
|
|
/* static */ const PLDHashTableOps
|
2007-01-13 04:04:56 +03:00
|
|
|
SpanningCellSorter::HashTableOps = {
|
|
|
|
HashTableHashKey,
|
|
|
|
HashTableMatchEntry,
|
2015-09-15 00:23:47 +03:00
|
|
|
PLDHashTable::MoveEntryStub,
|
|
|
|
PLDHashTable::ClearEntryStub,
|
2012-07-30 18:20:58 +04:00
|
|
|
nullptr
|
2007-01-13 04:04:56 +03:00
|
|
|
};
|
|
|
|
|
2008-10-10 19:04:34 +04:00
|
|
|
/* static */ PLDHashNumber
|
2016-03-16 07:33:44 +03:00
|
|
|
SpanningCellSorter::HashTableHashKey(const void *key)
|
2007-01-13 04:04:56 +03:00
|
|
|
{
|
2017-07-08 07:48:22 +03:00
|
|
|
return HashGeneric(key);
|
2007-01-13 04:04:56 +03:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
/* static */ bool
|
2016-03-16 07:33:44 +03:00
|
|
|
SpanningCellSorter::HashTableMatchEntry(const PLDHashEntryHdr *hdr,
|
2007-01-13 04:04:56 +03:00
|
|
|
const void *key)
|
|
|
|
{
|
2007-07-08 11:08:04 +04:00
|
|
|
const HashTableEntry *entry = static_cast<const HashTableEntry*>(hdr);
|
2007-01-13 04:04:56 +03:00
|
|
|
return NS_PTR_TO_INT32(key) == entry->mColSpan;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2012-08-22 19:56:38 +04:00
|
|
|
SpanningCellSorter::AddCell(int32_t aColSpan, int32_t aRow, int32_t aCol)
|
2007-01-13 04:04:56 +03:00
|
|
|
{
|
|
|
|
NS_ASSERTION(mState == ADDING, "cannot call AddCell after GetNext");
|
|
|
|
NS_ASSERTION(aColSpan >= ARRAY_BASE, "cannot add cells with colspan<2");
|
|
|
|
|
2012-05-04 04:14:02 +04:00
|
|
|
Item *i = (Item*) mozilla::AutoStackArena::Allocate(sizeof(Item));
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_ENSURE_TRUE(i != nullptr, false);
|
2007-01-13 04:04:56 +03:00
|
|
|
|
|
|
|
i->row = aRow;
|
|
|
|
i->col = aCol;
|
|
|
|
|
|
|
|
if (UseArrayForSpan(aColSpan)) {
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t index = SpanToIndex(aColSpan);
|
2007-01-13 04:04:56 +03:00
|
|
|
i->next = mArray[index];
|
|
|
|
mArray[index] = i;
|
|
|
|
} else {
|
2015-09-15 00:23:12 +03:00
|
|
|
auto entry = static_cast<HashTableEntry*>
|
|
|
|
(mHashTable.Add(NS_INT32_TO_PTR(aColSpan), fallible));
|
2011-10-17 18:59:28 +04:00
|
|
|
NS_ENSURE_TRUE(entry, false);
|
2007-01-13 04:04:56 +03:00
|
|
|
|
|
|
|
NS_ASSERTION(entry->mColSpan == 0 || entry->mColSpan == aColSpan,
|
|
|
|
"wrong entry");
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_ASSERTION((entry->mColSpan == 0) == (entry->mItems == nullptr),
|
2007-01-13 04:04:56 +03:00
|
|
|
"entry should be either new or properly initialized");
|
|
|
|
entry->mColSpan = aColSpan;
|
|
|
|
|
|
|
|
i->next = entry->mItems;
|
|
|
|
entry->mItems = i;
|
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2007-01-13 04:04:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ int
|
|
|
|
SpanningCellSorter::SortArray(const void *a, const void *b, void *closure)
|
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t spanA = (*static_cast<HashTableEntry*const*>(a))->mColSpan;
|
|
|
|
int32_t spanB = (*static_cast<HashTableEntry*const*>(b))->mColSpan;
|
2007-01-13 04:04:56 +03:00
|
|
|
|
|
|
|
if (spanA < spanB)
|
|
|
|
return -1;
|
|
|
|
if (spanA == spanB)
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
SpanningCellSorter::Item*
|
2012-08-22 19:56:38 +04:00
|
|
|
SpanningCellSorter::GetNext(int32_t *aColSpan)
|
2007-01-13 04:04:56 +03:00
|
|
|
{
|
|
|
|
NS_ASSERTION(mState != DONE, "done enumerating, stop calling");
|
|
|
|
|
|
|
|
switch (mState) {
|
|
|
|
case ADDING:
|
|
|
|
/* prepare to enumerate the array */
|
|
|
|
mState = ENUMERATING_ARRAY;
|
|
|
|
mEnumerationIndex = 0;
|
2015-11-23 08:33:47 +03:00
|
|
|
MOZ_FALLTHROUGH;
|
2007-01-13 04:04:56 +03:00
|
|
|
case ENUMERATING_ARRAY:
|
|
|
|
while (mEnumerationIndex < ARRAY_SIZE && !mArray[mEnumerationIndex])
|
|
|
|
++mEnumerationIndex;
|
|
|
|
if (mEnumerationIndex < ARRAY_SIZE) {
|
|
|
|
Item *result = mArray[mEnumerationIndex];
|
|
|
|
*aColSpan = IndexToSpan(mEnumerationIndex);
|
|
|
|
NS_ASSERTION(result, "logic error");
|
|
|
|
#ifdef DEBUG_SPANNING_CELL_SORTER
|
|
|
|
printf("SpanningCellSorter[%p]:"
|
|
|
|
" returning list for colspan=%d from array\n",
|
2007-07-08 11:08:04 +04:00
|
|
|
static_cast<void*>(this), *aColSpan);
|
2007-01-13 04:04:56 +03:00
|
|
|
#endif
|
|
|
|
++mEnumerationIndex;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
/* prepare to enumerate the hash */
|
|
|
|
mState = ENUMERATING_HASH;
|
|
|
|
mEnumerationIndex = 0;
|
2015-05-06 02:11:43 +03:00
|
|
|
if (mHashTable.EntryCount() > 0) {
|
2007-01-13 04:04:56 +03:00
|
|
|
HashTableEntry **sh =
|
2014-08-26 03:56:33 +04:00
|
|
|
new HashTableEntry*[mHashTable.EntryCount()];
|
2015-06-17 07:09:39 +03:00
|
|
|
int32_t j = 0;
|
|
|
|
for (auto iter = mHashTable.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
sh[j++] = static_cast<HashTableEntry*>(iter.Get());
|
|
|
|
}
|
2014-08-26 03:56:33 +04:00
|
|
|
NS_QuickSort(sh, mHashTable.EntryCount(), sizeof(sh[0]),
|
2012-07-30 18:20:58 +04:00
|
|
|
SortArray, nullptr);
|
2007-01-13 04:04:56 +03:00
|
|
|
mSortedHashTable = sh;
|
|
|
|
}
|
2015-11-23 08:33:47 +03:00
|
|
|
MOZ_FALLTHROUGH;
|
2007-01-13 04:04:56 +03:00
|
|
|
case ENUMERATING_HASH:
|
2015-05-06 02:11:43 +03:00
|
|
|
if (mEnumerationIndex < mHashTable.EntryCount()) {
|
2007-01-13 04:04:56 +03:00
|
|
|
Item *result = mSortedHashTable[mEnumerationIndex]->mItems;
|
|
|
|
*aColSpan = mSortedHashTable[mEnumerationIndex]->mColSpan;
|
|
|
|
NS_ASSERTION(result, "holes in hash table");
|
|
|
|
#ifdef DEBUG_SPANNING_CELL_SORTER
|
|
|
|
printf("SpanningCellSorter[%p]:"
|
|
|
|
" returning list for colspan=%d from hash\n",
|
2007-07-08 11:08:04 +04:00
|
|
|
static_cast<void*>(this), *aColSpan);
|
2007-01-13 04:04:56 +03:00
|
|
|
#endif
|
|
|
|
++mEnumerationIndex;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
mState = DONE;
|
2015-11-23 08:33:47 +03:00
|
|
|
MOZ_FALLTHROUGH;
|
2007-01-13 04:04:56 +03:00
|
|
|
case DONE:
|
|
|
|
;
|
|
|
|
}
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2007-01-13 04:04:56 +03:00
|
|
|
}
|