aboutsummaryrefslogtreecommitdiff
path: root/sp/src/public/vgui/Dar.h
blob: ee7200857710c11d98ba5140136b0c032939a2a0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: Holds the enumerated list of default cursors
//
// $NoKeywords: $
//=============================================================================//

#ifndef DAR_H
#define DAR_H

#ifdef _WIN32
#pragma once
#endif

#include <stdlib.h>
#include <string.h>
#include <vgui/VGUI.h>
#include "tier1/utlvector.h"

#include "tier0/memdbgon.h"

namespace vgui
{

//-----------------------------------------------------------------------------
// Purpose: Simple lightweight dynamic array implementation
//-----------------------------------------------------------------------------
template<class ELEMTYPE> class Dar : public CUtlVector< ELEMTYPE >
{
	typedef CUtlVector< ELEMTYPE > BaseClass;
	
public:
	Dar()
	{
	}
	Dar(int initialCapacity) :
		BaseClass( 0, initialCapacity )
	{
	}

public:
	void SetCount(int count)
	{
		this->EnsureCount( count );
	}
	int GetCount()
	{
		return this->Count();
	}
	int AddElement(ELEMTYPE elem)
	{
		return this->AddToTail( elem );
	}
	void MoveElementToEnd( ELEMTYPE elem )
	{
		if ( this->Count() == 0 )
			return;

		// quick check to see if it's already at the end
		if ( this->Element( this->Count() - 1 ) == elem )
			return;

		int idx = this->Find( elem );
		if ( idx == this->InvalidIndex() )
			return;

		this->Remove( idx );
		this->AddToTail( elem );
	}
	// returns the index of the element in the array, -1 if not found
	int FindElement(ELEMTYPE elem)
	{
		return this->Find( elem );
	}
	bool HasElement(ELEMTYPE elem)
	{
		if ( this->FindElement(elem) != this->InvalidIndex() )
		{
			return true;
		}
		return false;
	}
	int PutElement(ELEMTYPE elem)
	{
		int index = this->FindElement(elem);
		if (index >= 0)
		{
			return index;
		}
		return this->AddElement(elem);
	}
	// insert element at index and move all the others down 1
	void InsertElementAt(ELEMTYPE elem,int index)
	{
		this->InsertBefore( index, elem );
	}
	void SetElementAt(ELEMTYPE elem,int index)
	{
		this->EnsureCount( index + 1 );
		this->Element( index ) = elem;
	}
	void RemoveElementAt(int index)
	{
		this->Remove( index );
	} 

	void RemoveElementsBefore(int index)
	{
		if ( index <= 0 )
			return;
		this->RemoveMultiple( 0, index - 1 );
	}  

	void RemoveElement(ELEMTYPE elem)
	{
		this->FindAndRemove( elem );
	}

	void *GetBaseData()
	{
		return this->Base();
	}

	void CopyFrom(Dar<ELEMTYPE> &dar)
	{
		this->CopyArray( dar.Base(), dar.Count() );
	}
};

}

#include "tier0/memdbgoff.h"

#endif // DAR_H