1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
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/. */
7 #ifndef mozilla_dom_TreeOrderedArrayInlines_h
8 #define mozilla_dom_TreeOrderedArrayInlines_h
10 #include "mozilla/dom/TreeOrderedArray.h"
11 #include "mozilla/BinarySearch.h"
12 #include "nsContentUtils.h"
13 #include <type_traits>
15 namespace mozilla::dom
{
17 template <typename Node
>
18 size_t TreeOrderedArray
<Node
>::Insert(Node
& aNode
, nsINode
* aCommonAncestor
) {
19 static_assert(std::is_base_of_v
<nsINode
, Node
>, "Should be a node");
21 if (mList
.IsEmpty()) {
22 mList
.AppendElement(&aNode
);
26 struct PositionComparator
{
28 nsINode
* mCommonAncestor
;
29 PositionComparator(Node
& aNode
, nsINode
* aCommonAncestor
)
30 : mNode(aNode
), mCommonAncestor(aCommonAncestor
) {}
32 int operator()(void* aNode
) const {
33 auto* curNode
= static_cast<Node
*>(aNode
);
34 MOZ_DIAGNOSTIC_ASSERT(curNode
!= &mNode
,
35 "Tried to insert a node already in the list");
36 return nsContentUtils::CompareTreePosition
<TreeKind::DOM
>(
37 &mNode
, curNode
, mCommonAncestor
);
42 BinarySearchIf(mList
, 0, mList
.Length(),
43 PositionComparator(aNode
, aCommonAncestor
), &idx
);
44 mList
.InsertElementAt(idx
, &aNode
);
48 } // namespace mozilla::dom