From 6e35f47a3ea064b78f038c69065a3b0781a8d5d4 Mon Sep 17 00:00:00 2001 From: Klaus Schmidinger Date: Mon, 12 Jan 2015 12:10:15 +0100 Subject: [PATCH] Added the functions IndexOf(), InsertUnique(), AppendUnique() and RemoveElement() to the cVector class --- CONTRIBUTORS | 4 ++++ HISTORY | 2 ++ tools.h | 26 +++++++++++++++++++++++++- 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/CONTRIBUTORS b/CONTRIBUTORS index 2a33837a..cf961d25 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -3298,3 +3298,7 @@ Dietmar Spingler a CAM needs to receive the TS for reporting a problem that led to a fix with EMM pids not being properly reset for CAMs that need to receive the TS + +Stefan Schallenberg + for adding the functions IndexOf(), InsertUnique(), AppendUnique() and RemoveElement() + to the cVector class diff --git a/HISTORY b/HISTORY index c6368dce..0c5b41bf 100644 --- a/HISTORY +++ b/HISTORY @@ -8346,3 +8346,5 @@ Video Disk Recorder Revision History Lars Hanisch). - Added subsystem id support for DVB devices connected via USB (thanks to Jose Alberto Reguero). +- Added the functions IndexOf(), InsertUnique(), AppendUnique() and RemoveElement() + to the cVector class (thanks to Stefan Schallenberg). diff --git a/tools.h b/tools.h index 358f75e3..ea063232 100644 --- a/tools.h +++ b/tools.h @@ -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++)