src/misc/smallvec.h
author peter1138
Sun, 25 May 2008 16:12:13 +0000
changeset 9348 dc680f675138
parent 9335 4f1e59a9aed4
child 9427 af652de004a0
permissions -rw-r--r--
(svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
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
9335
4f1e59a9aed4 (svn r13227) -Codechange: Apply code style
peter1138
parents: 9111
diff changeset
     8
template <typename T, uint S>
4f1e59a9aed4 (svn r13227) -Codechange: Apply code style
peter1138
parents: 9111
diff changeset
     9
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
    10
	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
    11
	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
    12
	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
    13
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
	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
    15
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
	~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
    17
	{
9335
4f1e59a9aed4 (svn r13227) -Codechange: Apply code style
peter1138
parents: 9111
diff changeset
    18
		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
    19
	}
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
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
    21
	/**
9348
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
    22
	 * 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
    23
	 */
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
    24
	void Clear()
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
    25
	{
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
    26
		/* 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
    27
		 * 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
    28
		 * previously using. */
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
    29
		this->items = 0;
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
    30
	}
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
    31
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
    32
	/**
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
    33
	 * 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
    34
	 */
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
    35
	void Compact()
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
    36
	{
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
    37
		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
    38
		if (capacity >= this->capacity) return;
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
		this->capacity = capacity;
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
    41
		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
    42
	}
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
    43
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
    44
	/**
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
    45
	 * 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
    46
	 */
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
    47
	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
    48
	{
9335
4f1e59a9aed4 (svn r13227) -Codechange: Apply code style
peter1138
parents: 9111
diff changeset
    49
		if (this->items == this->capacity) {
4f1e59a9aed4 (svn r13227) -Codechange: Apply code style
peter1138
parents: 9111
diff changeset
    50
			this->capacity += S;
4f1e59a9aed4 (svn r13227) -Codechange: Apply code style
peter1138
parents: 9111
diff changeset
    51
			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
    52
		}
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
    53
9335
4f1e59a9aed4 (svn r13227) -Codechange: Apply code style
peter1138
parents: 9111
diff changeset
    54
		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
    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
9348
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
    57
	/**
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
    58
	 * 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
    59
	 */
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
    60
	uint Length() const
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
    61
	{
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
    62
		return this->items;
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
    63
	}
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
    64
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
    65
	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
    66
	{
9335
4f1e59a9aed4 (svn r13227) -Codechange: Apply code style
peter1138
parents: 9111
diff changeset
    67
		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
    68
	}
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
8951
618a0b1a5061 (svn r12739) -Codechange: use a vector instead of allocating memory in a byte array for ParentSpriteToDraw.
rubidium
parents: 8949
diff changeset
    70
	T *Begin()
618a0b1a5061 (svn r12739) -Codechange: use a vector instead of allocating memory in a byte array for ParentSpriteToDraw.
rubidium
parents: 8949
diff changeset
    71
	{
9335
4f1e59a9aed4 (svn r13227) -Codechange: Apply code style
peter1138
parents: 9111
diff changeset
    72
		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
    73
	}
618a0b1a5061 (svn r12739) -Codechange: use a vector instead of allocating memory in a byte array for ParentSpriteToDraw.
rubidium
parents: 8949
diff changeset
    74
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
    75
	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
    76
	{
9335
4f1e59a9aed4 (svn r13227) -Codechange: Apply code style
peter1138
parents: 9111
diff changeset
    77
		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
    78
	}
8951
618a0b1a5061 (svn r12739) -Codechange: use a vector instead of allocating memory in a byte array for ParentSpriteToDraw.
rubidium
parents: 8949
diff changeset
    79
618a0b1a5061 (svn r12739) -Codechange: use a vector instead of allocating memory in a byte array for ParentSpriteToDraw.
rubidium
parents: 8949
diff changeset
    80
	T *End()
618a0b1a5061 (svn r12739) -Codechange: use a vector instead of allocating memory in a byte array for ParentSpriteToDraw.
rubidium
parents: 8949
diff changeset
    81
	{
9335
4f1e59a9aed4 (svn r13227) -Codechange: Apply code style
peter1138
parents: 9111
diff changeset
    82
		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
    83
	}
8952
be6bfd75e554 (svn r12740) -Codechange: use a vector instead of allocating memory in a byte array for ChildScreenSpriteToDraw.
rubidium
parents: 8951
diff changeset
    84
9335
4f1e59a9aed4 (svn r13227) -Codechange: Apply code style
peter1138
parents: 9111
diff changeset
    85
	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
    86
	{
9335
4f1e59a9aed4 (svn r13227) -Codechange: Apply code style
peter1138
parents: 9111
diff changeset
    87
		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
    88
	}
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
	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
    91
	{
9335
4f1e59a9aed4 (svn r13227) -Codechange: Apply code style
peter1138
parents: 9111
diff changeset
    92
		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
    93
	}
9348
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
    94
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
    95
	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
    96
	{
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
    97
		return this->data[index];
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
    98
	}
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
	T &operator[](uint 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
		return this->data[index];
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9335
diff changeset
   103
	}
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
   104
};
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
   105
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
   106
#endif /* SMALLVEC_H */