Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(392)

Unified Diff: Source/wtf/TerminatedArrayBuilder.h

Issue 181403005: Introduce TerminatedArray<T> type to make data-structure used by RuleSet visible at the type level. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: rebase Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « Source/wtf/TerminatedArray.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: Source/wtf/TerminatedArrayBuilder.h
diff --git a/Source/wtf/TerminatedArrayBuilder.h b/Source/wtf/TerminatedArrayBuilder.h
new file mode 100644
index 0000000000000000000000000000000000000000..c71985e7686d1bd50e42b763c6a9f390a8f4ca24
--- /dev/null
+++ b/Source/wtf/TerminatedArrayBuilder.h
@@ -0,0 +1,79 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+#ifndef TerminatedArrayBuilder_h
+#define TerminatedArrayBuilder_h
+
+#include "wtf/OwnPtr.h"
+
+namespace WTF {
+
+template<typename T>
+class TerminatedArrayBuilder {
+ DISALLOW_ALLOCATION();
+ WTF_MAKE_NONCOPYABLE(TerminatedArrayBuilder);
+public:
+ explicit TerminatedArrayBuilder(PassOwnPtr<TerminatedArray<T> > array)
+ : m_array(array)
+ , m_count(0)
+ , m_capacity(0)
+ {
+ if (!m_array)
+ return;
+ m_capacity = m_count = m_array->size();
+ }
+
+ void grow(size_t count)
+ {
+ ASSERT(count);
+ if (!m_array) {
+ ASSERT(!m_count);
+ ASSERT(!m_capacity);
+ m_capacity = count;
+ m_array = TerminatedArray<T>::create(m_capacity);
+ return;
+ }
+ m_capacity += count;
+ m_array = TerminatedArray<T>::resize(m_array.release(), m_capacity);
+ m_array->at(m_count - 1).setLastInArray(false);
+ }
+
+ void append(const T& item)
+ {
+ RELEASE_ASSERT(m_count < m_capacity);
+ ASSERT(!item.isLastInArray());
+ m_array->at(m_count++) = item;
+ }
+
+ PassOwnPtr<TerminatedArray<T> > release()
+ {
+ RELEASE_ASSERT(m_count == m_capacity);
+ if (m_array)
+ m_array->at(m_count - 1).setLastInArray(true);
+ assertValid();
+ return m_array.release();
+ }
+
+private:
+#ifndef NDEBUG
+ void assertValid()
+ {
+ for (size_t i = 0; i < m_count; ++i) {
+ bool isLastInArray = (i + 1 == m_count);
+ ASSERT(m_array->at(i).isLastInArray() == isLastInArray);
+ }
+ }
+#else
+ void assertValid() { }
+#endif
+
+ OwnPtr<TerminatedArray<T> > m_array;
+ size_t m_count;
+ size_t m_capacity;
+};
+
+} // namespace WTF
+
+using WTF::TerminatedArrayBuilder;
+
+#endif // TerminatedArrayBuilder_h
« no previous file with comments | « Source/wtf/TerminatedArray.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698