Backed out changeset 8366e5cc9f57 (bug 125282) because of four windows unit test...
[mozilla-central.git] / layout / tables / SpanningCellSorter.h
blob3928e3505a2daf97023da76f75cca0103e59b5b6
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 // vim:cindent:ts=4:et:sw=4:
3 /* ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
16 * The Original Code is Mozilla's table layout code.
18 * The Initial Developer of the Original Code is the Mozilla Foundation.
19 * Portions created by the Initial Developer are Copyright (C) 2006
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
23 * L. David Baron <dbaron@dbaron.org> (original author)
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
40 * Code to sort cells by their colspan, used by BasicTableLayoutStrategy.
43 #include "nsIPresShell.h"
44 #include "pldhash.h"
46 /**
47 * The SpanningCellSorter is responsible for accumulating lists of cells
48 * with colspans so that those cells can later be enumerated, sorted
49 * from lowest number of columns spanned to highest. It does not use a
50 * stable sort (in fact, it currently reverses).
52 class SpanningCellSorter {
53 public:
54 SpanningCellSorter(nsIPresShell *aPresShell);
55 ~SpanningCellSorter();
57 struct Item {
58 PRInt32 row, col;
59 Item *next;
62 /**
63 * Add a cell to the sorter. Returns false on out of memory.
64 * aColSpan is the number of columns spanned, and aRow/aCol are the
65 * position of the cell in the table (for GetCellInfoAt).
67 PRBool AddCell(PRInt32 aColSpan, PRInt32 aRow, PRInt32 aCol);
69 /**
70 * Get the next *list* of cells. Each list contains all the cells
71 * for a colspan value, and the lists are given in order from lowest
72 * to highest colspan. The colspan value is filled in to *aColSpan.
74 Item* GetNext(PRInt32 *aColSpan);
75 private:
76 nsIPresShell *mPresShell;
78 enum State { ADDING, ENUMERATING_ARRAY, ENUMERATING_HASH, DONE };
79 State mState;
81 // store small colspans in an array for fast sorting and
82 // enumeration, and large colspans in a hash table
84 enum { ARRAY_BASE = 2 };
85 enum { ARRAY_SIZE = 8 };
86 Item *mArray[ARRAY_SIZE];
87 PRInt32 SpanToIndex(PRInt32 aSpan) { return aSpan - ARRAY_BASE; }
88 PRInt32 IndexToSpan(PRInt32 aIndex) { return aIndex + ARRAY_BASE; }
89 PRBool UseArrayForSpan(PRInt32 aSpan) {
90 NS_ASSERTION(SpanToIndex(aSpan) >= 0, "cell without colspan");
91 return SpanToIndex(aSpan) < ARRAY_SIZE;
94 PLDHashTable mHashTable;
95 struct HashTableEntry : public PLDHashEntryHdr {
96 PRInt32 mColSpan;
97 Item *mItems;
100 static PLDHashTableOps HashTableOps;
102 static PLDHashNumber
103 HashTableHashKey(PLDHashTable *table, const void *key);
104 static PRBool
105 HashTableMatchEntry(PLDHashTable *table, const PLDHashEntryHdr *hdr,
106 const void *key);
108 static PLDHashOperator
109 FillSortedArray(PLDHashTable *table, PLDHashEntryHdr *hdr,
110 PRUint32 number, void *arg);
112 static int SortArray(const void *a, const void *b, void *closure);
114 /* state used only during enumeration */
115 PRUint32 mEnumerationIndex; // into mArray or mSortedHashTable
116 HashTableEntry **mSortedHashTable;
119 * operator new is forbidden since we use the pres shell's stack
120 * memory, which much be pushed and popped at points matching a
121 * push/pop on the C++ stack.
123 void* operator new(size_t sz) CPP_THROW_NEW { return nsnull; }