diff options
author | Klaus Schmidinger <vdr@tvdr.de> | 2015-01-12 12:10:15 +0100 |
---|---|---|
committer | Klaus Schmidinger <vdr@tvdr.de> | 2015-01-12 12:10:15 +0100 |
commit | 6e35f47a3ea064b78f038c69065a3b0781a8d5d4 (patch) | |
tree | 11d526afe4500caa917d6e3b34cb95f517b6cd14 /tools.h | |
parent | 6514649439e7a0209945c70b7da10d6f0e4cb0ae (diff) | |
download | vdr-6e35f47a3ea064b78f038c69065a3b0781a8d5d4.tar.gz vdr-6e35f47a3ea064b78f038c69065a3b0781a8d5d4.tar.bz2 |
Added the functions IndexOf(), InsertUnique(), AppendUnique() and RemoveElement() to the cVector class
Diffstat (limited to 'tools.h')
-rw-r--r-- | tools.h | 26 |
1 files changed, 25 insertions, 1 deletions
@@ -4,7 +4,7 @@ * See the main source file 'vdr.c' for copyright information and * how to reach the author. * - * $Id: tools.h 3.3 2013/09/22 13:30:14 kls Exp $ + * $Id: tools.h 3.4 2015/01/12 12:03:59 kls Exp $ */ #ifndef __TOOLS_H @@ -537,6 +537,14 @@ public: { return At(Index); } + int IndexOf(const T &Data) // returns the index of Data, or -1 if not found + { + for (int i = 0; i < size; i++) { + if (data[i] == Data) + return i; + } + return -1; + } int Size(void) const { return size; } virtual void Insert(T Data, int Before = 0) { @@ -549,18 +557,34 @@ public: else Append(Data); } + void InsertUnique(T Data, int Before = 0) + { + if (IndexOf(Data) < 0) + Insert(Data, Before); + } virtual void Append(T Data) { if (size >= allocated) Realloc(allocated * 3 / 2); // increase size by 50% data[size++] = Data; } + void AppendUnique(T Data) + { + if (IndexOf(Data) < 0) + Append(Data); + } virtual void Remove(int Index) { if (Index < size - 1) memmove(&data[Index], &data[Index + 1], (size - Index) * sizeof(T)); size--; } + void RemoveElement(const T &Data) + { + int i = IndexOf(Data); + if (i >= 0) + Remove(i); + } virtual void Clear(void) { for (int i = 0; i < size; i++) |