src/misc/smallvec.h
author glx
Mon, 16 Jun 2008 20:16:43 +0000
changeset 10985 ae131ef2cc8f
parent 10791 0f3e94733e65
child 11018 830493d2f499
permissions -rw-r--r--
(svn r13539) -Fix (r13537): signed/unsigned warnings
10205
d37e906c7070 (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$ */
d37e906c7070 (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
10683
d9c3f54fe72f (svn r13227) -Codechange: Apply code style
peter1138
parents: 10429
diff changeset
     3
/** @file smallvec.h Simple vector class that allows allocating an item without the need to copy this->data needlessly. */
10205
d37e906c7070 (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
d37e906c7070 (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
d37e906c7070 (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
d37e906c7070 (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
10791
0f3e94733e65 (svn r13342) -Fix: smallvec.h/sortlist_type.h didn't include everything they needed.
rubidium
parents: 10697
diff changeset
     8
#include "../core/alloc_func.hpp"
0f3e94733e65 (svn r13342) -Fix: smallvec.h/sortlist_type.h didn't include everything they needed.
rubidium
parents: 10697
diff changeset
     9
#include "../core/math_func.hpp"
0f3e94733e65 (svn r13342) -Fix: smallvec.h/sortlist_type.h didn't include everything they needed.
rubidium
parents: 10697
diff changeset
    10
10683
d9c3f54fe72f (svn r13227) -Codechange: Apply code style
peter1138
parents: 10429
diff changeset
    11
template <typename T, uint S>
d9c3f54fe72f (svn r13227) -Codechange: Apply code style
peter1138
parents: 10429
diff changeset
    12
struct SmallVector {
10205
d37e906c7070 (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;
d37e906c7070 (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;
d37e906c7070 (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;
d37e906c7070 (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
d37e906c7070 (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) { }
d37e906c7070 (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
d37e906c7070 (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()
d37e906c7070 (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
	{
10683
d9c3f54fe72f (svn r13227) -Codechange: Apply code style
peter1138
parents: 10429
diff changeset
    21
		free(this->data);
10205
d37e906c7070 (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
	}
d37e906c7070 (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
d37e906c7070 (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
	/**
10697
9ec651be998d (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 10683
diff changeset
    25
	 * Remove all items from the list.
9ec651be998d (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 10683
diff changeset
    26
	 */
9ec651be998d (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 10683
diff changeset
    27
	void Clear()
9ec651be998d (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 10683
diff changeset
    28
	{
9ec651be998d (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 10683
diff changeset
    29
		/* In fact we just reset the item counter avoiding the need to
9ec651be998d (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 10683
diff changeset
    30
		 * probably reallocate the same amount of memory the list was
9ec651be998d (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 10683
diff changeset
    31
		 * previously using. */
9ec651be998d (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 10683
diff changeset
    32
		this->items = 0;
9ec651be998d (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 10683
diff changeset
    33
	}
9ec651be998d (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 10683
diff changeset
    34
9ec651be998d (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 10683
diff changeset
    35
	/**
9ec651be998d (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 10683
diff changeset
    36
	 * Compact the list down to the smallest block size boundary.
9ec651be998d (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 10683
diff changeset
    37
	 */
9ec651be998d (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 10683
diff changeset
    38
	void Compact()
9ec651be998d (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 10683
diff changeset
    39
	{
9ec651be998d (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 10683
diff changeset
    40
		uint capacity = Align(this->items, S);
9ec651be998d (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 10683
diff changeset
    41
		if (capacity >= this->capacity) return;
9ec651be998d (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 10683
diff changeset
    42
9ec651be998d (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 10683
diff changeset
    43
		this->capacity = capacity;
9ec651be998d (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 10683
diff changeset
    44
		this->data = ReallocT(this->data, this->capacity);
9ec651be998d (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 10683
diff changeset
    45
	}
9ec651be998d (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 10683
diff changeset
    46
9ec651be998d (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 10683
diff changeset
    47
	/**
10205
d37e906c7070 (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.
d37e906c7070 (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
	 */
d37e906c7070 (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()
d37e906c7070 (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
	{
10683
d9c3f54fe72f (svn r13227) -Codechange: Apply code style
peter1138
parents: 10429
diff changeset
    52
		if (this->items == this->capacity) {
d9c3f54fe72f (svn r13227) -Codechange: Apply code style
peter1138
parents: 10429
diff changeset
    53
			this->capacity += S;
d9c3f54fe72f (svn r13227) -Codechange: Apply code style
peter1138
parents: 10429
diff changeset
    54
			this->data = ReallocT(this->data, this->capacity);
10205
d37e906c7070 (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
		}
d37e906c7070 (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
10683
d9c3f54fe72f (svn r13227) -Codechange: Apply code style
peter1138
parents: 10429
diff changeset
    57
		return &this->data[this->items++];
10205
d37e906c7070 (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
	}
d37e906c7070 (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
10697
9ec651be998d (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 10683
diff changeset
    60
	/**
9ec651be998d (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 10683
diff changeset
    61
	 * Get the number of items in the list.
9ec651be998d (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 10683
diff changeset
    62
	 */
9ec651be998d (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 10683
diff changeset
    63
	uint Length() const
9ec651be998d (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 10683
diff changeset
    64
	{
9ec651be998d (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 10683
diff changeset
    65
		return this->items;
9ec651be998d (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 10683
diff changeset
    66
	}
9ec651be998d (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 10683
diff changeset
    67
10205
d37e906c7070 (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
d37e906c7070 (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
	{
10683
d9c3f54fe72f (svn r13227) -Codechange: Apply code style
peter1138
parents: 10429
diff changeset
    70
		return this->data;
10205
d37e906c7070 (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
	}
d37e906c7070 (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
10207
0bdc24d88198 (svn r12739) -Codechange: use a vector instead of allocating memory in a byte array for ParentSpriteToDraw.
rubidium
parents: 10205
diff changeset
    73
	T *Begin()
0bdc24d88198 (svn r12739) -Codechange: use a vector instead of allocating memory in a byte array for ParentSpriteToDraw.
rubidium
parents: 10205
diff changeset
    74
	{
10683
d9c3f54fe72f (svn r13227) -Codechange: Apply code style
peter1138
parents: 10429
diff changeset
    75
		return this->data;
10207
0bdc24d88198 (svn r12739) -Codechange: use a vector instead of allocating memory in a byte array for ParentSpriteToDraw.
rubidium
parents: 10205
diff changeset
    76
	}
0bdc24d88198 (svn r12739) -Codechange: use a vector instead of allocating memory in a byte array for ParentSpriteToDraw.
rubidium
parents: 10205
diff changeset
    77
10205
d37e906c7070 (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
d37e906c7070 (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
	{
10683
d9c3f54fe72f (svn r13227) -Codechange: Apply code style
peter1138
parents: 10429
diff changeset
    80
		return &this->data[this->items];
10205
d37e906c7070 (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
	}
10207
0bdc24d88198 (svn r12739) -Codechange: use a vector instead of allocating memory in a byte array for ParentSpriteToDraw.
rubidium
parents: 10205
diff changeset
    82
0bdc24d88198 (svn r12739) -Codechange: use a vector instead of allocating memory in a byte array for ParentSpriteToDraw.
rubidium
parents: 10205
diff changeset
    83
	T *End()
0bdc24d88198 (svn r12739) -Codechange: use a vector instead of allocating memory in a byte array for ParentSpriteToDraw.
rubidium
parents: 10205
diff changeset
    84
	{
10683
d9c3f54fe72f (svn r13227) -Codechange: Apply code style
peter1138
parents: 10429
diff changeset
    85
		return &this->data[this->items];
10207
0bdc24d88198 (svn r12739) -Codechange: use a vector instead of allocating memory in a byte array for ParentSpriteToDraw.
rubidium
parents: 10205
diff changeset
    86
	}
10208
98dc9f65629e (svn r12740) -Codechange: use a vector instead of allocating memory in a byte array for ChildScreenSpriteToDraw.
rubidium
parents: 10207
diff changeset
    87
10683
d9c3f54fe72f (svn r13227) -Codechange: Apply code style
peter1138
parents: 10429
diff changeset
    88
	const T *Get(uint index) const
10208
98dc9f65629e (svn r12740) -Codechange: use a vector instead of allocating memory in a byte array for ChildScreenSpriteToDraw.
rubidium
parents: 10207
diff changeset
    89
	{
10683
d9c3f54fe72f (svn r13227) -Codechange: Apply code style
peter1138
parents: 10429
diff changeset
    90
		return &this->data[index];
10208
98dc9f65629e (svn r12740) -Codechange: use a vector instead of allocating memory in a byte array for ChildScreenSpriteToDraw.
rubidium
parents: 10207
diff changeset
    91
	}
98dc9f65629e (svn r12740) -Codechange: use a vector instead of allocating memory in a byte array for ChildScreenSpriteToDraw.
rubidium
parents: 10207
diff changeset
    92
10683
d9c3f54fe72f (svn r13227) -Codechange: Apply code style
peter1138
parents: 10429
diff changeset
    93
	T *Get(uint index)
10208
98dc9f65629e (svn r12740) -Codechange: use a vector instead of allocating memory in a byte array for ChildScreenSpriteToDraw.
rubidium
parents: 10207
diff changeset
    94
	{
10683
d9c3f54fe72f (svn r13227) -Codechange: Apply code style
peter1138
parents: 10429
diff changeset
    95
		return &this->data[index];
10208
98dc9f65629e (svn r12740) -Codechange: use a vector instead of allocating memory in a byte array for ChildScreenSpriteToDraw.
rubidium
parents: 10207
diff changeset
    96
	}
10697
9ec651be998d (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 10683
diff changeset
    97
9ec651be998d (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 10683
diff changeset
    98
	const T &operator[](uint index) const
9ec651be998d (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 10683
diff changeset
    99
	{
9ec651be998d (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 10683
diff changeset
   100
		return this->data[index];
9ec651be998d (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 10683
diff changeset
   101
	}
9ec651be998d (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 10683
diff changeset
   102
9ec651be998d (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 10683
diff changeset
   103
	T &operator[](uint index)
9ec651be998d (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 10683
diff changeset
   104
	{
9ec651be998d (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 10683
diff changeset
   105
		return this->data[index];
9ec651be998d (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 10683
diff changeset
   106
	}
10205
d37e906c7070 (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
};
d37e906c7070 (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
d37e906c7070 (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 */