1/*
2 * Copyright (C) 2013 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 *     * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *     * Neither the name of Google Inc. nor the names of its
11 * contributors may be used to endorse or promote products derived from
12 * this software without specific prior written permission.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
18 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
20 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "config.h"
28#include "core/dom/shadow/ContentDistribution.h"
29
30#include "core/dom/shadow/InsertionPoint.h"
31
32namespace blink {
33
34void ContentDistribution::swap(ContentDistribution& other)
35{
36    m_nodes.swap(other.m_nodes);
37    m_indices.swap(other.m_indices);
38}
39
40void ContentDistribution::append(PassRefPtrWillBeRawPtr<Node> node)
41{
42    ASSERT(node);
43    ASSERT(!isActiveInsertionPoint(*node));
44    size_t size = m_nodes.size();
45    m_indices.set(node.get(), size);
46    m_nodes.append(node);
47}
48
49size_t ContentDistribution::find(const Node* node) const
50{
51    WillBeHeapHashMap<RawPtrWillBeMember<const Node>, size_t>::const_iterator it = m_indices.find(node);
52    if (it == m_indices.end())
53        return kNotFound;
54
55    return it.get()->value;
56}
57
58Node* ContentDistribution::nextTo(const Node* node) const
59{
60    size_t index = find(node);
61    if (index == kNotFound || index + 1 == size())
62        return 0;
63    return at(index + 1).get();
64}
65
66Node* ContentDistribution::previousTo(const Node* node) const
67{
68    size_t index = find(node);
69    if (index == kNotFound || !index)
70        return 0;
71    return at(index - 1).get();
72}
73
74void ContentDistribution::trace(Visitor* visitor)
75{
76#if ENABLE(OILPAN)
77    visitor->trace(m_nodes);
78    visitor->trace(m_indices);
79#endif
80}
81
82} // namespace blink
83