Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ps_stl.h
Go to the documentation of this file.
1 /* Copyright (c) 2013 Wildfire Games
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining
4  * a copy of this software and associated documentation files (the
5  * "Software"), to deal in the Software without restriction, including
6  * without limitation the rights to use, copy, modify, merge, publish,
7  * distribute, sublicense, and/or sell copies of the Software, and to
8  * permit persons to whom the Software is furnished to do so, subject to
9  * the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22 #ifndef INCLUDED_PS_STL
23 #define INCLUDED_PS_STL
24 
25 /**
26  * @author Jorma Rebane
27  * @note Pyrogenesis STL methods
28  * @note This file contains useful and optimized methods for use with STL
29  */
30 
31 namespace ps
32 {
33  /**
34  * Removes the first occurrence of the specified value from the container.
35  * @param container The STL-compatible container to remove from.
36  * @param value The value to remove.
37  */
38 template<class Container, class T>
39  inline void remove_first_occurrence(Container& container, const T& value)
40  {
41  if (int count = container.size())
42  {
43  T* data = &container[0];
44  for (int i = 0; i < count; ++i)
45  {
46  if (data[i] == value)
47  {
48  container.erase(container.begin() + i);
49  return;
50  }
51  }
52  }
53  }
54 
55  /**
56  * @param container The STL-compatible container to search in.
57  * @param value The value to search for.
58  * @return TRUE if [value] exists in [container].
59  */
60 template<class Container, class T>
61  inline bool exists_in(const Container& container, const T& value)
62  {
63  if (int count = container.size())
64  {
65  for (const T* data = &container[0]; count; ++data, --count)
66  {
67  if (*data == value)
68  {
69  return true;
70  }
71  }
72  }
73  return false;
74  }
75 
76  /**
77  * Finds a value in a container
78  * @param container The STL-compatible container to search in.
79  * @param value The value to search for.
80  * @return Pointer to the value if found, NULL if not found.
81  */
82 template<class Container, class T>
83  inline T* find_in(const Container& container, const T& value)
84  {
85  if (int count = container.size())
86  {
87  for (const T* data = &container[0]; count; ++data, --count)
88  {
89  if (*data == value)
90  {
91  return (T*)data;
92  }
93  }
94  }
95  return NULL;
96  }
97 
98 
99 } // namespace ps
100 
101 #endif // INCLUDED_PS_STL
#define T(string_literal)
Definition: secure_crt.cpp:70
bool exists_in(const Container &container, const T &value)
Definition: ps_stl.h:61
T * find_in(const Container &container, const T &value)
Finds a value in a container.
Definition: ps_stl.h:83
void remove_first_occurrence(Container &container, const T &value)
Removes the first occurrence of the specified value from the container.
Definition: ps_stl.h:39