nsTreeRows.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "nsString.h"
  6. #include "nsTreeRows.h"
  7. #include <algorithm>
  8. nsTreeRows::Subtree*
  9. nsTreeRows::EnsureSubtreeFor(Subtree* aParent,
  10. int32_t aChildIndex)
  11. {
  12. Subtree* subtree = GetSubtreeFor(aParent, aChildIndex);
  13. if (! subtree) {
  14. subtree = aParent->mRows[aChildIndex].mSubtree = new Subtree(aParent);
  15. InvalidateCachedRow();
  16. }
  17. return subtree;
  18. }
  19. nsTreeRows::Subtree*
  20. nsTreeRows::GetSubtreeFor(const Subtree* aParent,
  21. int32_t aChildIndex,
  22. int32_t* aSubtreeSize)
  23. {
  24. NS_PRECONDITION(aParent, "no parent");
  25. NS_PRECONDITION(aChildIndex >= 0, "bad child index");
  26. Subtree* result = nullptr;
  27. if (aChildIndex < aParent->mCount)
  28. result = aParent->mRows[aChildIndex].mSubtree;
  29. if (aSubtreeSize)
  30. *aSubtreeSize = result ? result->mSubtreeSize : 0;
  31. return result;
  32. }
  33. void
  34. nsTreeRows::RemoveSubtreeFor(Subtree* aParent, int32_t aChildIndex)
  35. {
  36. NS_PRECONDITION(aParent, "no parent");
  37. NS_PRECONDITION(aChildIndex >= 0 && aChildIndex < aParent->mCount, "bad child index");
  38. Row& row = aParent->mRows[aChildIndex];
  39. if (row.mSubtree) {
  40. int32_t subtreeSize = row.mSubtree->GetSubtreeSize();
  41. delete row.mSubtree;
  42. row.mSubtree = nullptr;
  43. for (Subtree* subtree = aParent; subtree != nullptr; subtree = subtree->mParent)
  44. subtree->mSubtreeSize -= subtreeSize;
  45. }
  46. InvalidateCachedRow();
  47. }
  48. nsTreeRows::iterator
  49. nsTreeRows::First()
  50. {
  51. iterator result;
  52. result.Append(&mRoot, 0);
  53. result.SetRowIndex(0);
  54. return result;
  55. }
  56. nsTreeRows::iterator
  57. nsTreeRows::Last()
  58. {
  59. iterator result;
  60. // Build up a path along the rightmost edge of the tree
  61. Subtree* current = &mRoot;
  62. int32_t count = current->Count();
  63. do {
  64. int32_t last = count - 1;
  65. result.Append(current, last);
  66. current = count ? GetSubtreeFor(current, last) : nullptr;
  67. } while (current && ((count = current->Count()) != 0));
  68. // Now, at the bottom rightmost leaf, advance us one off the end.
  69. result.GetTop().mChildIndex++;
  70. // Our row index will be the size of the root subree, plus one.
  71. result.SetRowIndex(mRoot.GetSubtreeSize() + 1);
  72. return result;
  73. }
  74. nsTreeRows::iterator
  75. nsTreeRows::operator[](int32_t aRow)
  76. {
  77. // See if we're just lucky, and end up with something
  78. // nearby. (This tends to happen a lot due to the way that we get
  79. // asked for rows n' stuff.)
  80. int32_t last = mLastRow.GetRowIndex();
  81. if (last != -1) {
  82. if (aRow == last)
  83. return mLastRow;
  84. else if (last + 1 == aRow)
  85. return ++mLastRow;
  86. else if (last - 1 == aRow)
  87. return --mLastRow;
  88. }
  89. // Nope. Construct a path to the specified index. This is a little
  90. // bit better than O(n), because we can skip over subtrees. (So it
  91. // ends up being approximately linear in the subtree size, instead
  92. // of the entire view size. But, most of the time, big views are
  93. // flat. Oh well.)
  94. iterator result;
  95. Subtree* current = &mRoot;
  96. int32_t index = 0;
  97. result.SetRowIndex(aRow);
  98. do {
  99. int32_t subtreeSize;
  100. Subtree* subtree = GetSubtreeFor(current, index, &subtreeSize);
  101. if (subtreeSize >= aRow) {
  102. result.Append(current, index);
  103. current = subtree;
  104. index = 0;
  105. --aRow;
  106. }
  107. else {
  108. ++index;
  109. aRow -= subtreeSize + 1;
  110. }
  111. } while (aRow >= 0);
  112. mLastRow = result;
  113. return result;
  114. }
  115. nsTreeRows::iterator
  116. nsTreeRows::FindByResource(nsIRDFResource* aResource)
  117. {
  118. // XXX Mmm, scan through the rows one-by-one...
  119. iterator last = Last();
  120. iterator iter;
  121. nsresult rv;
  122. nsAutoString resourceid;
  123. bool stringmode = false;
  124. for (iter = First(); iter != last; ++iter) {
  125. if (!stringmode) {
  126. nsCOMPtr<nsIRDFResource> findres;
  127. rv = iter->mMatch->mResult->GetResource(getter_AddRefs(findres));
  128. if (NS_FAILED(rv)) return last;
  129. if (findres == aResource)
  130. break;
  131. if (! findres) {
  132. const char *uri;
  133. aResource->GetValueConst(&uri);
  134. CopyUTF8toUTF16(uri, resourceid);
  135. // set stringmode and fall through
  136. stringmode = true;
  137. }
  138. }
  139. // additional check because previous block could change stringmode
  140. if (stringmode) {
  141. nsAutoString findid;
  142. rv = iter->mMatch->mResult->GetId(findid);
  143. if (NS_FAILED(rv)) return last;
  144. if (resourceid.Equals(findid))
  145. break;
  146. }
  147. }
  148. return iter;
  149. }
  150. nsTreeRows::iterator
  151. nsTreeRows::Find(nsIXULTemplateResult *aResult)
  152. {
  153. // XXX Mmm, scan through the rows one-by-one...
  154. iterator last = Last();
  155. iterator iter;
  156. for (iter = First(); iter != last; ++iter) {
  157. if (aResult == iter->mMatch->mResult)
  158. break;
  159. }
  160. return iter;
  161. }
  162. void
  163. nsTreeRows::Clear()
  164. {
  165. mRoot.Clear();
  166. InvalidateCachedRow();
  167. }
  168. //----------------------------------------------------------------------
  169. //
  170. // nsTreeRows::Subtree
  171. //
  172. nsTreeRows::Subtree::~Subtree()
  173. {
  174. Clear();
  175. }
  176. void
  177. nsTreeRows::Subtree::Clear()
  178. {
  179. for (int32_t i = mCount - 1; i >= 0; --i)
  180. delete mRows[i].mSubtree;
  181. delete[] mRows;
  182. mRows = nullptr;
  183. mCount = mCapacity = mSubtreeSize = 0;
  184. }
  185. nsTreeRows::iterator
  186. nsTreeRows::Subtree::InsertRowAt(nsTemplateMatch* aMatch, int32_t aIndex)
  187. {
  188. if (mCount >= mCapacity || aIndex >= mCapacity) {
  189. int32_t newCapacity = std::max(mCapacity * 2, aIndex + 1);
  190. Row* newRows = new Row[newCapacity];
  191. if (! newRows)
  192. return iterator();
  193. for (int32_t i = mCount - 1; i >= 0; --i)
  194. newRows[i] = mRows[i];
  195. delete[] mRows;
  196. mRows = newRows;
  197. mCapacity = newCapacity;
  198. }
  199. for (int32_t i = mCount - 1; i >= aIndex; --i)
  200. mRows[i + 1] = mRows[i];
  201. mRows[aIndex].mMatch = aMatch;
  202. mRows[aIndex].mContainerType = eContainerType_Unknown;
  203. mRows[aIndex].mContainerState = eContainerState_Unknown;
  204. mRows[aIndex].mContainerFill = eContainerFill_Unknown;
  205. mRows[aIndex].mSubtree = nullptr;
  206. ++mCount;
  207. // Now build an iterator that points to the newly inserted element.
  208. int32_t rowIndex = 0;
  209. iterator result;
  210. result.Push(this, aIndex);
  211. for ( ; --aIndex >= 0; ++rowIndex) {
  212. // Account for open subtrees in the absolute row index.
  213. const Subtree *subtree = mRows[aIndex].mSubtree;
  214. if (subtree)
  215. rowIndex += subtree->mSubtreeSize;
  216. }
  217. Subtree *subtree = this;
  218. do {
  219. // Note that the subtree's size has expanded.
  220. ++subtree->mSubtreeSize;
  221. Subtree *parent = subtree->mParent;
  222. if (! parent)
  223. break;
  224. // Account for open subtrees in the absolute row index.
  225. int32_t count = parent->Count();
  226. for (aIndex = 0; aIndex < count; ++aIndex, ++rowIndex) {
  227. const Subtree *child = (*parent)[aIndex].mSubtree;
  228. if (subtree == child)
  229. break;
  230. if (child)
  231. rowIndex += child->mSubtreeSize;
  232. }
  233. NS_ASSERTION(aIndex < count, "couldn't find subtree in parent");
  234. result.Push(parent, aIndex);
  235. subtree = parent;
  236. ++rowIndex; // One for the parent row.
  237. } while (1);
  238. result.SetRowIndex(rowIndex);
  239. return result;
  240. }
  241. void
  242. nsTreeRows::Subtree::RemoveRowAt(int32_t aIndex)
  243. {
  244. NS_PRECONDITION(aIndex >= 0 && aIndex < Count(), "bad index");
  245. if (aIndex < 0 || aIndex >= Count())
  246. return;
  247. // How big is the subtree we're going to be removing?
  248. int32_t subtreeSize = mRows[aIndex].mSubtree
  249. ? mRows[aIndex].mSubtree->GetSubtreeSize()
  250. : 0;
  251. ++subtreeSize;
  252. delete mRows[aIndex].mSubtree;
  253. for (int32_t i = aIndex + 1; i < mCount; ++i)
  254. mRows[i - 1] = mRows[i];
  255. --mCount;
  256. for (Subtree* subtree = this; subtree != nullptr; subtree = subtree->mParent)
  257. subtree->mSubtreeSize -= subtreeSize;
  258. }
  259. //----------------------------------------------------------------------
  260. //
  261. // nsTreeRows::iterator
  262. //
  263. nsTreeRows::iterator::iterator(const iterator& aIterator)
  264. : mRowIndex(aIterator.mRowIndex),
  265. mLink(aIterator.mLink)
  266. {
  267. }
  268. nsTreeRows::iterator&
  269. nsTreeRows::iterator::operator=(const iterator& aIterator)
  270. {
  271. mRowIndex = aIterator.mRowIndex;
  272. mLink = aIterator.mLink;
  273. return *this;
  274. }
  275. void
  276. nsTreeRows::iterator::Append(Subtree* aParent, int32_t aChildIndex)
  277. {
  278. Link *link = mLink.AppendElement();
  279. if (link) {
  280. link->mParent = aParent;
  281. link->mChildIndex = aChildIndex;
  282. }
  283. else
  284. NS_ERROR("out of memory");
  285. }
  286. void
  287. nsTreeRows::iterator::Push(Subtree *aParent, int32_t aChildIndex)
  288. {
  289. Link *link = mLink.InsertElementAt(0);
  290. if (link) {
  291. link->mParent = aParent;
  292. link->mChildIndex = aChildIndex;
  293. }
  294. else
  295. NS_ERROR("out of memory");
  296. }
  297. bool
  298. nsTreeRows::iterator::operator==(const iterator& aIterator) const
  299. {
  300. if (GetDepth() != aIterator.GetDepth())
  301. return false;
  302. if (GetDepth() == 0)
  303. return true;
  304. return GetTop() == aIterator.GetTop();
  305. }
  306. void
  307. nsTreeRows::iterator::Next()
  308. {
  309. NS_PRECONDITION(GetDepth() > 0, "cannot increment an uninitialized iterator");
  310. // Increment the absolute row index
  311. ++mRowIndex;
  312. Link& top = GetTop();
  313. // Is there a child subtree? If so, descend into the child
  314. // subtree.
  315. Subtree* subtree = top.GetRow().mSubtree;
  316. if (subtree && subtree->Count()) {
  317. Append(subtree, 0);
  318. return;
  319. }
  320. // Have we exhausted the current subtree?
  321. if (top.mChildIndex >= top.mParent->Count() - 1) {
  322. // Yep. See if we've just iterated path the last element in
  323. // the tree, period. Walk back up the stack, looking for any
  324. // unfinished subtrees.
  325. int32_t unfinished;
  326. for (unfinished = GetDepth() - 2; unfinished >= 0; --unfinished) {
  327. const Link& link = mLink[unfinished];
  328. if (link.mChildIndex < link.mParent->Count() - 1)
  329. break;
  330. }
  331. // If there are no unfinished subtrees in the stack, then this
  332. // iterator is exhausted. Leave it in the same state that
  333. // Last() does.
  334. if (unfinished < 0) {
  335. top.mChildIndex++;
  336. return;
  337. }
  338. // Otherwise, we ran off the end of one of the inner
  339. // subtrees. Pop up to the next unfinished level in the stack.
  340. mLink.SetLength(unfinished + 1);
  341. }
  342. // Advance to the next child in this subtree
  343. ++(GetTop().mChildIndex);
  344. }
  345. void
  346. nsTreeRows::iterator::Prev()
  347. {
  348. NS_PRECONDITION(GetDepth() > 0, "cannot increment an uninitialized iterator");
  349. // Decrement the absolute row index
  350. --mRowIndex;
  351. // Move to the previous child in this subtree
  352. --(GetTop().mChildIndex);
  353. // Have we exhausted the current subtree?
  354. if (GetTop().mChildIndex < 0) {
  355. // Yep. See if we've just iterated back to the first element
  356. // in the tree, period. Walk back up the stack, looking for
  357. // any unfinished subtrees.
  358. int32_t unfinished;
  359. for (unfinished = GetDepth() - 2; unfinished >= 0; --unfinished) {
  360. const Link& link = mLink[unfinished];
  361. if (link.mChildIndex >= 0)
  362. break;
  363. }
  364. // If there are no unfinished subtrees in the stack, then this
  365. // iterator is exhausted. Leave it in the same state that
  366. // First() does.
  367. if (unfinished < 0)
  368. return;
  369. // Otherwise, we ran off the end of one of the inner
  370. // subtrees. Pop up to the next unfinished level in the stack.
  371. mLink.SetLength(unfinished + 1);
  372. return;
  373. }
  374. // Is there a child subtree immediately prior to our current
  375. // position? If so, descend into it, grovelling down to the
  376. // deepest, rightmost left edge.
  377. Subtree* parent = GetTop().GetParent();
  378. int32_t index = GetTop().GetChildIndex();
  379. Subtree* subtree = (*parent)[index].mSubtree;
  380. if (subtree && subtree->Count()) {
  381. do {
  382. index = subtree->Count() - 1;
  383. Append(subtree, index);
  384. parent = subtree;
  385. subtree = (*parent)[index].mSubtree;
  386. } while (subtree && subtree->Count());
  387. }
  388. }