src/misc/smallvec.h
author frosch
Sat, 24 May 2008 11:19:30 +0000
changeset 9338 3d6f9ddd431d
parent 9335 4f1e59a9aed4
child 9348 dc680f675138
permissions -rw-r--r--
(svn r13230) -Fix [FS#2030](r13171): When closing toolbars, also close their PickerWindows.
/* $Id$ */

/** @file smallvec.h Simple vector class that allows allocating an item without the need to copy this->data needlessly. */

#ifndef SMALLVEC_H
#define SMALLVEC_H

template <typename T, uint S>
struct SmallVector {
	T *data;
	uint items;
	uint capacity;

	SmallVector() : data(NULL), items(0), capacity(0) { }

	~SmallVector()
	{
		free(this->data);
	}

	/**
	 * Append an item and return it.
	 */
	T *Append()
	{
		if (this->items == this->capacity) {
			this->capacity += S;
			this->data = ReallocT(this->data, this->capacity);
		}

		return &this->data[this->items++];
	}

	const T *Begin() const
	{
		return this->data;
	}

	T *Begin()
	{
		return this->data;
	}

	const T *End() const
	{
		return &this->data[this->items];
	}

	T *End()
	{
		return &this->data[this->items];
	}

	const T *Get(uint index) const
	{
		return &this->data[index];
	}

	T *Get(uint index)
	{
		return &this->data[index];
	}
};

#endif /* SMALLVEC_H */