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
5 : *
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/
10 : *
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.
15 : *
16 : * The Original Code is Mozilla's table layout code.
17 : *
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.
21 : *
22 : * Contributor(s):
23 : * L. David Baron <dbaron@dbaron.org> (original author)
24 : *
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.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : /*
40 : * Code to sort cells by their colspan, used by BasicTableLayoutStrategy.
41 : */
42 :
43 : #include "pldhash.h"
44 : #include "nsDebug.h"
45 :
46 : class nsIPresShell;
47 :
48 : /**
49 : * The SpanningCellSorter is responsible for accumulating lists of cells
50 : * with colspans so that those cells can later be enumerated, sorted
51 : * from lowest number of columns spanned to highest. It does not use a
52 : * stable sort (in fact, it currently reverses).
53 : */
54 : class SpanningCellSorter {
55 : public:
56 : SpanningCellSorter(nsIPresShell *aPresShell);
57 : ~SpanningCellSorter();
58 :
59 : struct Item {
60 : PRInt32 row, col;
61 : Item *next;
62 : };
63 :
64 : /**
65 : * Add a cell to the sorter. Returns false on out of memory.
66 : * aColSpan is the number of columns spanned, and aRow/aCol are the
67 : * position of the cell in the table (for GetCellInfoAt).
68 : */
69 : bool AddCell(PRInt32 aColSpan, PRInt32 aRow, PRInt32 aCol);
70 :
71 : /**
72 : * Get the next *list* of cells. Each list contains all the cells
73 : * for a colspan value, and the lists are given in order from lowest
74 : * to highest colspan. The colspan value is filled in to *aColSpan.
75 : */
76 : Item* GetNext(PRInt32 *aColSpan);
77 : private:
78 : nsIPresShell *mPresShell;
79 :
80 : enum State { ADDING, ENUMERATING_ARRAY, ENUMERATING_HASH, DONE };
81 : State mState;
82 :
83 : // store small colspans in an array for fast sorting and
84 : // enumeration, and large colspans in a hash table
85 :
86 : enum { ARRAY_BASE = 2 };
87 : enum { ARRAY_SIZE = 8 };
88 : Item *mArray[ARRAY_SIZE];
89 0 : PRInt32 SpanToIndex(PRInt32 aSpan) { return aSpan - ARRAY_BASE; }
90 0 : PRInt32 IndexToSpan(PRInt32 aIndex) { return aIndex + ARRAY_BASE; }
91 0 : bool UseArrayForSpan(PRInt32 aSpan) {
92 0 : NS_ASSERTION(SpanToIndex(aSpan) >= 0, "cell without colspan");
93 0 : return SpanToIndex(aSpan) < ARRAY_SIZE;
94 : }
95 :
96 : PLDHashTable mHashTable;
97 : struct HashTableEntry : public PLDHashEntryHdr {
98 : PRInt32 mColSpan;
99 : Item *mItems;
100 : };
101 :
102 : static PLDHashTableOps HashTableOps;
103 :
104 : static PLDHashNumber
105 : HashTableHashKey(PLDHashTable *table, const void *key);
106 : static bool
107 : HashTableMatchEntry(PLDHashTable *table, const PLDHashEntryHdr *hdr,
108 : const void *key);
109 :
110 : static PLDHashOperator
111 : FillSortedArray(PLDHashTable *table, PLDHashEntryHdr *hdr,
112 : PRUint32 number, void *arg);
113 :
114 : static int SortArray(const void *a, const void *b, void *closure);
115 :
116 : /* state used only during enumeration */
117 : PRUint32 mEnumerationIndex; // into mArray or mSortedHashTable
118 : HashTableEntry **mSortedHashTable;
119 :
120 : /*
121 : * operator new is forbidden since we use the pres shell's stack
122 : * memory, which much be pushed and popped at points matching a
123 : * push/pop on the C++ stack.
124 : */
125 : void* operator new(size_t sz) CPP_THROW_NEW { return nsnull; }
126 : };
127 :
|