2 * Copyright (C) 2009 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include "WebCommon.h"
40 // A simple vector class.
44 // void Foo(WebVector<int>& result)
46 // WebVector<int> data(10);
47 // for (size_t i = 0; i < data.size(); ++i)
52 // It is also possible to assign from other types of random access
55 // void Foo(const std::vector<std::string>& input)
57 // WebVector<WebCString> cstrings = input;
71 explicit WebVector(size_t size = 0)
77 WebVector(const U* values, size_t size)
79 initializeFrom(values, size);
82 WebVector(const WebVector<T>& other)
84 initializeFrom(other.m_ptr, other.m_size);
88 WebVector(const C& other)
90 initializeFrom(other.size() ? &other[0] : 0, other.size());
93 WebVector& operator=(const WebVector& other)
100 template <typename C>
101 WebVector<T>& operator=(const C& other)
103 if (this != reinterpret_cast<const WebVector<T>*>(&other))
108 template <typename C>
109 void assign(const C& other)
111 assign(other.size() ? &other[0] : 0, other.size());
114 template <typename U>
115 void assign(const U* values, size_t size)
118 initializeFrom(values, size);
121 size_t size() const { return m_size; }
122 bool isEmpty() const { return !m_size; }
124 T& operator[](size_t i)
126 BLINK_ASSERT(i < m_size);
129 const T& operator[](size_t i) const
131 BLINK_ASSERT(i < m_size);
135 bool contains(const T& value) const
137 for (size_t i = 0; i < m_size; i++) {
138 if (m_ptr[i] == value)
144 T* data() { return m_ptr; }
145 const T* data() const { return m_ptr; }
147 void swap(WebVector<T>& other)
149 std::swap(m_ptr, other.m_ptr);
150 std::swap(m_size, other.m_size);
154 void initialize(size_t size)
160 m_ptr = static_cast<T*>(::operator new(sizeof(T) * m_size));
161 for (size_t i = 0; i < m_size; ++i)
166 template <typename U>
167 void initializeFrom(const U* values, size_t size)
173 m_ptr = static_cast<T*>(::operator new(sizeof(T) * m_size));
174 for (size_t i = 0; i < m_size; ++i)
175 new (&m_ptr[i]) T(values[i]);
181 for (size_t i = 0; i < m_size; ++i)
183 ::operator delete(m_ptr);