src/misc/smallvec.h
author peter1138
Sat, 24 May 2008 10:02:49 +0000
changeset 10683 d9c3f54fe72f
parent 10429 1b99254f9607
child 10697 9ec651be998d
permissions -rw-r--r--
(svn r13227) -Codechange: Apply code style
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
10683
d9c3f54fe72f (svn r13227) -Codechange: Apply code style
peter1138
parents: 10429
diff changeset
     8
template <typename T, uint S>
d9c3f54fe72f (svn r13227) -Codechange: Apply code style
peter1138
parents: 10429
diff changeset
     9
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
    10
	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
    11
	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
    12
	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
    13
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
	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
    15
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
	~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
    17
	{
10683
d9c3f54fe72f (svn r13227) -Codechange: Apply code style
peter1138
parents: 10429
diff changeset
    18
		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
    19
	}
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
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
    21
	/**
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
	 * 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
    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
	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
    25
	{
10683
d9c3f54fe72f (svn r13227) -Codechange: Apply code style
peter1138
parents: 10429
diff changeset
    26
		if (this->items == this->capacity) {
d9c3f54fe72f (svn r13227) -Codechange: Apply code style
peter1138
parents: 10429
diff changeset
    27
			this->capacity += S;
d9c3f54fe72f (svn r13227) -Codechange: Apply code style
peter1138
parents: 10429
diff changeset
    28
			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
    29
		}
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
    30
10683
d9c3f54fe72f (svn r13227) -Codechange: Apply code style
peter1138
parents: 10429
diff changeset
    31
		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
    32
	}
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
    33
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
    34
	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
    35
	{
10683
d9c3f54fe72f (svn r13227) -Codechange: Apply code style
peter1138
parents: 10429
diff changeset
    36
		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
    37
	}
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
    38
10207
0bdc24d88198 (svn r12739) -Codechange: use a vector instead of allocating memory in a byte array for ParentSpriteToDraw.
rubidium
parents: 10205
diff changeset
    39
	T *Begin()
0bdc24d88198 (svn r12739) -Codechange: use a vector instead of allocating memory in a byte array for ParentSpriteToDraw.
rubidium
parents: 10205
diff changeset
    40
	{
10683
d9c3f54fe72f (svn r13227) -Codechange: Apply code style
peter1138
parents: 10429
diff changeset
    41
		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
    42
	}
0bdc24d88198 (svn r12739) -Codechange: use a vector instead of allocating memory in a byte array for ParentSpriteToDraw.
rubidium
parents: 10205
diff changeset
    43
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
    44
	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
    45
	{
10683
d9c3f54fe72f (svn r13227) -Codechange: Apply code style
peter1138
parents: 10429
diff changeset
    46
		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
    47
	}
10207
0bdc24d88198 (svn r12739) -Codechange: use a vector instead of allocating memory in a byte array for ParentSpriteToDraw.
rubidium
parents: 10205
diff changeset
    48
0bdc24d88198 (svn r12739) -Codechange: use a vector instead of allocating memory in a byte array for ParentSpriteToDraw.
rubidium
parents: 10205
diff changeset
    49
	T *End()
0bdc24d88198 (svn r12739) -Codechange: use a vector instead of allocating memory in a byte array for ParentSpriteToDraw.
rubidium
parents: 10205
diff changeset
    50
	{
10683
d9c3f54fe72f (svn r13227) -Codechange: Apply code style
peter1138
parents: 10429
diff changeset
    51
		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
    52
	}
10208
98dc9f65629e (svn r12740) -Codechange: use a vector instead of allocating memory in a byte array for ChildScreenSpriteToDraw.
rubidium
parents: 10207
diff changeset
    53
10683
d9c3f54fe72f (svn r13227) -Codechange: Apply code style
peter1138
parents: 10429
diff changeset
    54
	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
    55
	{
10683
d9c3f54fe72f (svn r13227) -Codechange: Apply code style
peter1138
parents: 10429
diff changeset
    56
		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
    57
	}
98dc9f65629e (svn r12740) -Codechange: use a vector instead of allocating memory in a byte array for ChildScreenSpriteToDraw.
rubidium
parents: 10207
diff changeset
    58
10683
d9c3f54fe72f (svn r13227) -Codechange: Apply code style
peter1138
parents: 10429
diff changeset
    59
	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
    60
	{
10683
d9c3f54fe72f (svn r13227) -Codechange: Apply code style
peter1138
parents: 10429
diff changeset
    61
		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
    62
	}
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
    63
};
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
    64
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
    65
#endif /* SMALLVEC_H */