src/misc/smallvec.h
author rubidium
Fri, 30 May 2008 09:32:24 +0000
changeset 9427 af652de004a0
parent 9348 dc680f675138
child 9554 d677e63894db
permissions -rw-r--r--
(svn r13342) -Fix: smallvec.h/sortlist_type.h didn't include everything they needed.
8949
4c9fbf5ec359 (svn r12737) -Codechange: Replace vector with a cut down class to allocate space as necessary. This avoids copying data around for vector's push_back() function.
peter1138
parents:
diff changeset
     1
/* $Id$ */
4c9fbf5ec359 (svn r12737) -Codechange: Replace vector with a cut down class to allocate space as necessary. This avoids copying data around for vector's push_back() function.
peter1138
parents:
diff changeset
     2
9335
4f1e59a9aed4 (svn r13227) -Codechange: Apply code style
peter1138
parents: 9111
diff changeset
     3
/** @file smallvec.h Simple vector class that allows allocating an item without the need to copy this->data needlessly. */
8949
4c9fbf5ec359 (svn r12737) -Codechange: Replace vector with a cut down class to allocate space as necessary. This avoids copying data around for vector's push_back() function.
peter1138
parents:
diff changeset
     4
4c9fbf5ec359 (svn r12737) -Codechange: Replace vector with a cut down class to allocate space as necessary. This avoids copying data around for vector's push_back() function.
peter1138
parents:
diff changeset
     5
#ifndef SMALLVEC_H
4c9fbf5ec359 (svn r12737) -Codechange: Replace vector with a cut down class to allocate space as necessary. This avoids copying data around for vector's push_back() function.
peter1138
parents:
diff changeset
     6
#define SMALLVEC_H
4c9fbf5ec359 (svn r12737) -Codechange: Replace vector with a cut down class to allocate space as necessary. This avoids copying data around for vector's push_back() function.
peter1138
parents:
diff changeset
     7
9427
af652de004a0 (svn r13342) -Fix: smallvec.h/sortlist_type.h didn't include everything they needed.
rubidium
parents: 9348
diff changeset
     8
#include "../core/alloc_func.hpp"
af652de004a0 (svn r13342) -Fix: smallvec.h/sortlist_type.h didn't include everything they needed.
rubidium
parents: 9348
diff changeset
     9
#include "../core/math_func.hpp"
af652de004a0 (svn r13342) -Fix: smallvec.h/sortlist_type.h didn't include everything they needed.
rubidium
parents: 9348
diff changeset
    10
9335
4f1e59a9aed4 (svn r13227) -Codechange: Apply code style
peter1138
parents: 9111
diff changeset
    11
template <typename T, uint S>
4f1e59a9aed4 (svn r13227) -Codechange: Apply code style
peter1138
parents: 9111
diff changeset
    12
struct SmallVector {
8949
4c9fbf5ec359 (svn r12737) -Codechange: Replace vector with a cut down class to allocate space as necessary. This avoids copying data around for vector's push_back() function.
peter1138
parents:
diff changeset
    13
	T *data;
4c9fbf5ec359 (svn r12737) -Codechange: Replace vector with a cut down class to allocate space as necessary. This avoids copying data around for vector's push_back() function.
peter1138
parents:
diff changeset
    14
	uint items;
4c9fbf5ec359 (svn r12737) -Codechange: Replace vector with a cut down class to allocate space as necessary. This avoids copying data around for vector's push_back() function.
peter1138
parents:
diff changeset
    15
	uint capacity;
4c9fbf5ec359 (svn r12737) -Codechange: Replace vector with a cut down class to allocate space as necessary. This avoids copying data around for vector's push_back() function.
peter1138
parents:
diff changeset
    16
4c9fbf5ec359 (svn r12737) -Codechange: Replace vector with a cut down class to allocate space as necessary. This avoids copying data around for vector's push_back() function.
peter1138
parents:
diff changeset
    17
	SmallVector() : data(NULL), items(0), capacity(0) { }
4c9fbf5ec359 (svn r12737) -Codechange: Replace vector with a cut down class to allocate space as necessary. This avoids copying data around for vector's push_back() function.
peter1138
parents:
diff changeset
    18
4c9fbf5ec359 (svn r12737) -Codechange: Replace vector with a cut down class to allocate space as necessary. This avoids copying data around for vector's push_back() function.
peter1138
parents:
diff changeset
    19
	~SmallVector()
4c9fbf5ec359 (svn r12737) -Codechange: Replace vector with a cut down class to allocate space as necessary. This avoids copying data around for vector's push_back() function.
peter1138
parents:
diff changeset
    20
	{
9335
4f1e59a9aed4 (svn r13227) -Codechange: Apply code style
peter1138
parents: 9111
diff changeset
    21
		free(this->data);
8949
4c9fbf5ec359 (svn r12737) -Codechange: Replace vector with a cut down class to allocate space as necessary. This avoids copying data around for vector's push_back() function.
peter1138
parents:
diff changeset
    22
	}
4c9fbf5ec359 (svn r12737) -Codechange: Replace vector with a cut down class to allocate space as necessary. This avoids copying data around for vector's push_back() function.
peter1138
parents:
diff changeset
    23
4c9fbf5ec359 (svn r12737) -Codechange: Replace vector with a cut down class to allocate space as necessary. This avoids copying data around for vector's push_back() function.
peter1138
parents:
diff changeset
    24
	/**
9348
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
    25
	 * Remove all items from the list.
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
    26
	 */
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
    27
	void Clear()
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
    28
	{
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
    29
		/* In fact we just reset the item counter avoiding the need to
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
    30
		 * probably reallocate the same amount of memory the list was
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
    31
		 * previously using. */
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
    32
		this->items = 0;
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
    33
	}
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
    34
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
    35
	/**
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
    36
	 * Compact the list down to the smallest block size boundary.
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
    37
	 */
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
    38
	void Compact()
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
    39
	{
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
    40
		uint capacity = Align(this->items, S);
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
    41
		if (capacity >= this->capacity) return;
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
    42
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
    43
		this->capacity = capacity;
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
    44
		this->data = ReallocT(this->data, this->capacity);
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
    45
	}
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
    46
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
    47
	/**
8949
4c9fbf5ec359 (svn r12737) -Codechange: Replace vector with a cut down class to allocate space as necessary. This avoids copying data around for vector's push_back() function.
peter1138
parents:
diff changeset
    48
	 * Append an item and return it.
4c9fbf5ec359 (svn r12737) -Codechange: Replace vector with a cut down class to allocate space as necessary. This avoids copying data around for vector's push_back() function.
peter1138
parents:
diff changeset
    49
	 */
4c9fbf5ec359 (svn r12737) -Codechange: Replace vector with a cut down class to allocate space as necessary. This avoids copying data around for vector's push_back() function.
peter1138
parents:
diff changeset
    50
	T *Append()
4c9fbf5ec359 (svn r12737) -Codechange: Replace vector with a cut down class to allocate space as necessary. This avoids copying data around for vector's push_back() function.
peter1138
parents:
diff changeset
    51
	{
9335
4f1e59a9aed4 (svn r13227) -Codechange: Apply code style
peter1138
parents: 9111
diff changeset
    52
		if (this->items == this->capacity) {
4f1e59a9aed4 (svn r13227) -Codechange: Apply code style
peter1138
parents: 9111
diff changeset
    53
			this->capacity += S;
4f1e59a9aed4 (svn r13227) -Codechange: Apply code style
peter1138
parents: 9111
diff changeset
    54
			this->data = ReallocT(this->data, this->capacity);
8949
4c9fbf5ec359 (svn r12737) -Codechange: Replace vector with a cut down class to allocate space as necessary. This avoids copying data around for vector's push_back() function.
peter1138
parents:
diff changeset
    55
		}
4c9fbf5ec359 (svn r12737) -Codechange: Replace vector with a cut down class to allocate space as necessary. This avoids copying data around for vector's push_back() function.
peter1138
parents:
diff changeset
    56
9335
4f1e59a9aed4 (svn r13227) -Codechange: Apply code style
peter1138
parents: 9111
diff changeset
    57
		return &this->data[this->items++];
8949
4c9fbf5ec359 (svn r12737) -Codechange: Replace vector with a cut down class to allocate space as necessary. This avoids copying data around for vector's push_back() function.
peter1138
parents:
diff changeset
    58
	}
4c9fbf5ec359 (svn r12737) -Codechange: Replace vector with a cut down class to allocate space as necessary. This avoids copying data around for vector's push_back() function.
peter1138
parents:
diff changeset
    59
9348
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
    60
	/**
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
    61
	 * Get the number of items in the list.
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
    62
	 */
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
    63
	uint Length() const
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
    64
	{
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
    65
		return this->items;
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
    66
	}
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
    67
8949
4c9fbf5ec359 (svn r12737) -Codechange: Replace vector with a cut down class to allocate space as necessary. This avoids copying data around for vector's push_back() function.
peter1138
parents:
diff changeset
    68
	const T *Begin() const
4c9fbf5ec359 (svn r12737) -Codechange: Replace vector with a cut down class to allocate space as necessary. This avoids copying data around for vector's push_back() function.
peter1138
parents:
diff changeset
    69
	{
9335
4f1e59a9aed4 (svn r13227) -Codechange: Apply code style
peter1138
parents: 9111
diff changeset
    70
		return this->data;
8949
4c9fbf5ec359 (svn r12737) -Codechange: Replace vector with a cut down class to allocate space as necessary. This avoids copying data around for vector's push_back() function.
peter1138
parents:
diff changeset
    71
	}
4c9fbf5ec359 (svn r12737) -Codechange: Replace vector with a cut down class to allocate space as necessary. This avoids copying data around for vector's push_back() function.
peter1138
parents:
diff changeset
    72
8951
618a0b1a5061 (svn r12739) -Codechange: use a vector instead of allocating memory in a byte array for ParentSpriteToDraw.
rubidium
parents: 8949
diff changeset
    73
	T *Begin()
618a0b1a5061 (svn r12739) -Codechange: use a vector instead of allocating memory in a byte array for ParentSpriteToDraw.
rubidium
parents: 8949
diff changeset
    74
	{
9335
4f1e59a9aed4 (svn r13227) -Codechange: Apply code style
peter1138
parents: 9111
diff changeset
    75
		return this->data;
8951
618a0b1a5061 (svn r12739) -Codechange: use a vector instead of allocating memory in a byte array for ParentSpriteToDraw.
rubidium
parents: 8949
diff changeset
    76
	}
618a0b1a5061 (svn r12739) -Codechange: use a vector instead of allocating memory in a byte array for ParentSpriteToDraw.
rubidium
parents: 8949
diff changeset
    77
8949
4c9fbf5ec359 (svn r12737) -Codechange: Replace vector with a cut down class to allocate space as necessary. This avoids copying data around for vector's push_back() function.
peter1138
parents:
diff changeset
    78
	const T *End() const
4c9fbf5ec359 (svn r12737) -Codechange: Replace vector with a cut down class to allocate space as necessary. This avoids copying data around for vector's push_back() function.
peter1138
parents:
diff changeset
    79
	{
9335
4f1e59a9aed4 (svn r13227) -Codechange: Apply code style
peter1138
parents: 9111
diff changeset
    80
		return &this->data[this->items];
8949
4c9fbf5ec359 (svn r12737) -Codechange: Replace vector with a cut down class to allocate space as necessary. This avoids copying data around for vector's push_back() function.
peter1138
parents:
diff changeset
    81
	}
8951
618a0b1a5061 (svn r12739) -Codechange: use a vector instead of allocating memory in a byte array for ParentSpriteToDraw.
rubidium
parents: 8949
diff changeset
    82
618a0b1a5061 (svn r12739) -Codechange: use a vector instead of allocating memory in a byte array for ParentSpriteToDraw.
rubidium
parents: 8949
diff changeset
    83
	T *End()
618a0b1a5061 (svn r12739) -Codechange: use a vector instead of allocating memory in a byte array for ParentSpriteToDraw.
rubidium
parents: 8949
diff changeset
    84
	{
9335
4f1e59a9aed4 (svn r13227) -Codechange: Apply code style
peter1138
parents: 9111
diff changeset
    85
		return &this->data[this->items];
8951
618a0b1a5061 (svn r12739) -Codechange: use a vector instead of allocating memory in a byte array for ParentSpriteToDraw.
rubidium
parents: 8949
diff changeset
    86
	}
8952
be6bfd75e554 (svn r12740) -Codechange: use a vector instead of allocating memory in a byte array for ChildScreenSpriteToDraw.
rubidium
parents: 8951
diff changeset
    87
9335
4f1e59a9aed4 (svn r13227) -Codechange: Apply code style
peter1138
parents: 9111
diff changeset
    88
	const T *Get(uint index) const
8952
be6bfd75e554 (svn r12740) -Codechange: use a vector instead of allocating memory in a byte array for ChildScreenSpriteToDraw.
rubidium
parents: 8951
diff changeset
    89
	{
9335
4f1e59a9aed4 (svn r13227) -Codechange: Apply code style
peter1138
parents: 9111
diff changeset
    90
		return &this->data[index];
8952
be6bfd75e554 (svn r12740) -Codechange: use a vector instead of allocating memory in a byte array for ChildScreenSpriteToDraw.
rubidium
parents: 8951
diff changeset
    91
	}
be6bfd75e554 (svn r12740) -Codechange: use a vector instead of allocating memory in a byte array for ChildScreenSpriteToDraw.
rubidium
parents: 8951
diff changeset
    92
9335
4f1e59a9aed4 (svn r13227) -Codechange: Apply code style
peter1138
parents: 9111
diff changeset
    93
	T *Get(uint index)
8952
be6bfd75e554 (svn r12740) -Codechange: use a vector instead of allocating memory in a byte array for ChildScreenSpriteToDraw.
rubidium
parents: 8951
diff changeset
    94
	{
9335
4f1e59a9aed4 (svn r13227) -Codechange: Apply code style
peter1138
parents: 9111
diff changeset
    95
		return &this->data[index];
8952
be6bfd75e554 (svn r12740) -Codechange: use a vector instead of allocating memory in a byte array for ChildScreenSpriteToDraw.
rubidium
parents: 8951
diff changeset
    96
	}
9348
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
    97
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
    98
	const T &operator[](uint index) const
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
    99
	{
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
   100
		return this->data[index];
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
   101
	}
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
   102
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
   103
	T &operator[](uint index)
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
   104
	{
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
   105
		return this->data[index];
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
   106
	}
8949
4c9fbf5ec359 (svn r12737) -Codechange: Replace vector with a cut down class to allocate space as necessary. This avoids copying data around for vector's push_back() function.
peter1138
parents:
diff changeset
   107
};
4c9fbf5ec359 (svn r12737) -Codechange: Replace vector with a cut down class to allocate space as necessary. This avoids copying data around for vector's push_back() function.
peter1138
parents:
diff changeset
   108
4c9fbf5ec359 (svn r12737) -Codechange: Replace vector with a cut down class to allocate space as necessary. This avoids copying data around for vector's push_back() function.
peter1138
parents:
diff changeset
   109
#endif /* SMALLVEC_H */