removed Container classes in favor of std::vector

Wed, 27 Jan 2021 13:17:11 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 27 Jan 2021 13:17:11 +0200
changeset 181
e254398fcc7c
parent 180
2e7225dbd9b2
child 182
20ca0a6be175

removed Container classes in favor of std::vector

sources/basics.h file | annotate | diff | comparison | revisions
sources/coloredline.cpp file | annotate | diff | comparison | revisions
sources/interface.cpp file | annotate | diff | comparison | revisions
sources/list.h file | annotate | diff | comparison | revisions
sources/mystring.cpp file | annotate | diff | comparison | revisions
--- a/sources/basics.h	Wed Jan 27 13:08:51 2021 +0200
+++ b/sources/basics.h	Wed Jan 27 13:17:11 2021 +0200
@@ -29,6 +29,7 @@
 */
 
 #pragma once
+#include <cassert>
 #include <type_traits>
 #include <stdlib.h>
 
--- a/sources/coloredline.cpp	Wed Jan 27 13:08:51 2021 +0200
+++ b/sources/coloredline.cpp	Wed Jan 27 13:17:11 2021 +0200
@@ -53,7 +53,7 @@
 		setColor(m_activeColor, false);
 
 	if (m_boldActive)
-		m_data << RLINE_OFF_BOLD;
+		m_data.push_back(RLINE_OFF_BOLD);
 
 	m_final = true;
 }
@@ -103,7 +103,7 @@
 			setColor(m_activeColor, false);
 
 		if (m_boldActive)
-			m_data << RLINE_OFF_BOLD;
+			m_data.push_back(RLINE_OFF_BOLD);
 
 		m_boldActive = false;
 
@@ -151,7 +151,7 @@
 	if (isprint(ch))
 	{
 		m_string += ch;
-		m_data << int(ch);
+		m_data.push_back(static_cast<int>(ch));
 		++m_length;
 	}
 }
@@ -161,15 +161,17 @@
 void ColoredLine::activateColor(Color color, bool bold)
 {
 	if (m_boldActive)
-		m_data << RLINE_OFF_BOLD;
-
+	{
+		m_data.push_back(RLINE_OFF_BOLD);
+	}
 	m_activeColor = color;
 	m_boldActive = bold;
 	assert(m_activeColor < 8);
 	setColor(m_activeColor, true);
-
 	if (m_boldActive)
-		m_data << RLINE_ON_BOLD;
+	{
+		m_data.push_back(RLINE_ON_BOLD);
+	}
 }
 
 // -------------------------------------------------------------------------------------------------
@@ -185,7 +187,7 @@
 void ColoredLine::setColor(Color a, bool on)
 {
 	assert(a < 8);
-	m_data << (a +(on ? RLINE_ON_COLOR : RLINE_OFF_COLOR));
+	m_data.push_back(a + (on ? RLINE_ON_COLOR : RLINE_OFF_COLOR));
 }
 
 // -------------------------------------------------------------------------------------------------
--- a/sources/interface.cpp	Wed Jan 27 13:08:51 2021 +0200
+++ b/sources/interface.cpp	Wed Jan 27 13:17:11 2021 +0200
@@ -167,7 +167,7 @@
 	m_inputHistory.clear();
 	m_inputHistory.push_back("");
 	m_outputLines.clear();
-	m_outputLines << ColoredLine();
+	m_outputLines.push_back(ColoredLine());
 	m_session.setInterface(this);
 	resetTitle();
 
@@ -304,7 +304,7 @@
 	if (m_outputLines.size() == 1)
 		return;
 
-	m_outputScroll = clamp(m_outputScroll, 0, m_outputLines.size() - 1);
+	m_outputScroll = clamp(m_outputScroll, 0, static_cast<signed>(m_outputLines.size() - 1));
 
 	int height = LINES - 3;
 	int width = COLS - nicklistWidth();
@@ -334,7 +334,7 @@
 	// See if there's any more rows to use(end may be too small)
 	if (not tightFit)
 	{
-		while (end < m_outputLines.size())
+		while (end < static_cast<int>(m_outputLines.size()))
 		{
 			int rows = m_outputLines[end].rows(width);
 
@@ -963,12 +963,12 @@
 	{
 		if (ch == '\n')
 		{
-			m_outputLines.last().finalize();
-			m_outputLines << ColoredLine();
+			zfc::last(m_outputLines).finalize();
+			m_outputLines.push_back({});
 			continue;
 		}
 
-		if (m_outputLines.last().length() == 0)
+		if (zfc::last(m_outputLines).length() == 0)
 		{
 			time_t now;
 			time(&now);
@@ -976,14 +976,14 @@
 			strftime(timestamp, sizeof timestamp, "[%H:%M:%S] ", localtime(&now));
 
 			for (char ch : String(timestamp))
-				m_outputLines.last().addChar(ch);
+				zfc::last(m_outputLines).addChar(ch);
 		}
 
 		// Remove some lines if there's too many of them. 20,000 should be enough, I hope.
 		while (m_outputLines.size() > 20000)
-			m_outputLines.remove_at(0);
+			m_outputLines.erase(m_outputLines.begin());
 
-		m_outputLines.last().addChar(ch);
+		zfc::last(m_outputLines).addChar(ch);
 	}
 
 	m_needOutputRender = true;
--- a/sources/list.h	Wed Jan 27 13:08:51 2021 +0200
+++ b/sources/list.h	Wed Jan 27 13:17:11 2021 +0200
@@ -29,357 +29,19 @@
 */
 
 #pragma once
+#include <vector>
 #include "basics.h"
-#include <algorithm>
-#include <deque>
-#include <functional>
-#include <cassert>
-#include <vector>
 #include "range.h"
 BEGIN_ZFC_NAMESPACE
 
-// -------------------------------------------------------------------------------------------------
-//
-template<typename T, typename C>
-class Container
-{
-public:
-	typedef typename C::iterator Iterator;
-	typedef typename C::const_iterator ConstIterator;
-	typedef typename C::reverse_iterator ReverseIterator;
-	typedef typename C::const_reverse_iterator ConstReverseIterator;
-	typedef Container<T, C> Self;
-
-	Container(){}
-
-	Container (int numvalues) :
-		m_container (numvalues) {}
-
-	Container (const C& other) :
-		m_container (other) {}
-
-	Container(std::initializer_list<T> initializerList) :
-	    m_container(initializerList) {}
-
-	T& append (const T& value)
-	{
-		m_container.push_back (value);
-		return m_container[m_container.size() - 1];
-	}
-
-	void append(const T* values, size_t numValues)
-	{
-		size_t i0 = size();
-		resize(size() + numValues);
-
-		for (size_t i : range(numValues))
-			(*this)[i0 + i] = values[i];
-	}
-
-	Iterator begin()
-	{
-		return m_container.begin();
-	}
-
-	ConstIterator begin() const
-	{
-		return m_container.begin();
-	}
-
-	void clear()
-	{
-		m_container.clear();
-	}
-
-	bool contains (const T& a) const
-	{
-		return std::find (m_container.begin(), m_container.end(), a) != m_container.end();
-	}
-
-	ConstReverseIterator crbegin() const
-	{
-		return m_container.rbegin();
-	}
-
-	ConstReverseIterator crend() const
-	{
-		return m_container.rend();
-	}
-
-	const C& container() const
-	{
-		return m_container;
-	}
-
-	Iterator end()
-	{
-		return m_container.end();
-	}
-
-	ConstIterator end() const
-	{
-		return m_container.end();
-	}
-
-	Iterator find (const T& needle)
-	{
-		auto it = std::find (m_container.begin(), m_container.end(), needle);
-
-		if (it == m_container.end())
-			return end();
-
-		return it;
-	}
-
-	ConstIterator find (const T& needle) const
-	{
-		auto it = std::find (m_container.begin(), m_container.end(), needle);
-
-		if (it == m_container.end())
-			return end();
-
-		return it;
-	}
-
-	Iterator find (bool (*func)(T const&))
-	{
-		for (Iterator it = begin(); it != end(); ++it)
-		{
-			if (func (*it))
-				return it;
-		}
-
-		return end();
-	}
-
-	ConstIterator find (bool (*func)(T const&)) const
-	{
-		for (ConstIterator it = begin(); it != end(); ++it)
-		{
-			if (func (*it))
-				return it;
-		}
-
-		return end();
-	}
-
-	T& first()
-	{
-		return *begin();
-	}
-
-	const T& first() const
-	{
-		return *begin();
-	}
-
-	void insert (int pos, const T& value)
-	{
-		m_container.insert (m_container.begin() + pos, value);
-	}
-
-	bool is_empty() const
-	{
-		return size() == 0;
-	}
-
-	T& last()
-	{
-		return *(end() - 1);
-	}
-
-	const T& last() const
-	{
-		return *(end() - 1);
-	}
-
-	void merge (const Self& other)
-	{
-		int oldsize = size();
-		resize (size() + other.size());
-		std::copy (other.begin(), other.end(), begin() + oldsize);
-	}
-
-	bool pop (T& val)
-	{
-		if (is_empty())
-			return false;
-
-		val = m_container[size() - 1];
-		m_container.erase (m_container.end() - 1);
-		return true;
-	}
-
-	T& prepend (const T& value)
-	{
-		m_container.push_front (value);
-		return m_container[0];
-	}
-
-	ReverseIterator rbegin()
-	{
-		return m_container.rbegin();
-	}
-
-	void remove_at (int pos)
-	{
-		assert (pos < size());
-		m_container.erase (m_container.begin() + pos);
-	}
-
-	void remove_duplicates()
-	{
-		sort();
-		resize (std::distance (begin(), std::unique (begin(), end())));
-	}
-
-	void remove_one (const T& valueToRemove)
-	{
-		auto it = std::find (m_container.begin(), m_container.end(), valueToRemove);
-
-		if (it != m_container.end())
-			m_container.erase (it);
-	}
-
-	ReverseIterator rend()
-	{
-		return m_container.rend();
-	}
-
-	void resize (int size)
-	{
-		m_container.resize (size);
-	}
-
-	Self reverse() const
-	{
-		Self rev;
-		std::copy (rbegin(), rend(), rev.begin());
-		return rev;
-	}
-
-	int size() const
-	{
-		return m_container.size();
-	}
-
-	void sort()
-	{
-		std::sort (begin(), end());
-	}
-
-	Self splice(int start, int end, int step = 1) const
-	{
-		start = clamp(start, 0, size());
-		end = clamp(end, 0, size());
-
-		if (end <= start)
-		{
-			return Self();
-		}
-		else
-		{
-			Self result;
-
-			for (int i : range(start, end, step))
-				result << operator[] (i);
-
-			return result;
-		}
-	}
-
-	Self splice(Range<int>& range) const
-	{
-		return splice(range.min(), range.max(), range.step());
-	}
-
-	Self& operator<< (const T& value)
-	{
-		append (value);
-		return *this;
-	}
-
-	Self& operator<< (const Self& vals)
-	{
-		merge (vals);
-		return *this;
-	}
-
-	T& operator[] (int n)
-	{
-		assert (n < size());
-		return m_container[n];
-	}
-
-	const T& operator[] (int n) const
-	{
-		assert (n < size());
-		return m_container[n];
-	}
-
-	Self operator[] (Range<int> const& n) const
-	{
-		return splice (n);
-	}
-
-	Self operator+ (const Self& other) const
-	{
-		Self out (*this);
-		out.merge (other);
-		return out;
-	}
-
-protected:
-	C m_container;
-};
-
-// -------------------------------------------------------------------------------------------------
-//
-template<typename T, typename C>
-Container<T, C>& operator>> (const T& value, Container<T, C>& haystack)
-{
-	haystack.prepend (value);
-	return haystack;
-}
-
-// -------------------------------------------------------------------------------------------------
-//
+template<typename T>
+using Vector = std::vector<T>;
 
 template<typename T>
-class Vector : public Container<T, std::vector<T> >
+auto& last(T& container)
 {
-public:
-	typedef Container<T, std::vector<T> > Super;
-
-	Vector(){}
-
-	Vector(int numvalues) :
-	    Super(numvalues){}
-
-	Vector (T* data, size_t length) :
-		Super (std::vector<T> (data, data + length)) {}
-
-	Vector(std::initializer_list<T> initializerList) :
-	    Super(initializerList) {}
-
-	Vector(const Super& other) :
-		Super(other) {}
-
-	T* data()
-	{
-		return Super::m_container.data();
-	}
-
-	const T* data() const
-	{
-		return Super::m_container.data();
-	}
-
-	operator const T*() const
-	{
-		return data();
-	}
-};
+	return *(container.begin() + container.size() - 1);
+}
 
 using ByteArray = std::vector<unsigned char>;
 class String quote(const ByteArray& bytes);
--- a/sources/mystring.cpp	Wed Jan 27 13:08:51 2021 +0200
+++ b/sources/mystring.cpp	Wed Jan 27 13:17:11 2021 +0200
@@ -380,7 +380,7 @@
 		// vsnprintf needs more space, so we have to allocate a new buffer and try again.
 		Vector<char> newBuffer(length + 1);
 		vsnprintf(newBuffer.data(), length + 1, formatString, argsCopy);
-		m_string = newBuffer;
+		m_string = newBuffer.data();
 	}
 }
 

mercurial