xref: /haiku/src/apps/cortex/support/set_tools.h (revision 17889a8c70dbb3d59c1412f6431968753c767bab)
1 /*
2  * Copyright (c) 1999-2000, Eric Moon.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions, and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions, and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
23  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
27  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 
32 // set_tools.h
33 // e.moon 7may99
34 //
35 // PURPOSE
36 //   Tools to manipulate STL set types.
37 //
38 // HISTORY
39 //   e.moon 27jul99		moved into cortex namespace
40 //   e.moon	7may99		created
41 
42 #ifndef __SET_TOOLS_H__
43 #define __SET_TOOLS_H__
44 
45 #include "cortex_defs.h"
46 __BEGIN_CORTEX_NAMESPACE
47 
48 // delete range of pointer values from set
49 template<class iter>
50 void ptr_set_delete(iter begin, iter end) {
51 	while(begin != end) {
52 		if(*begin)
53 			delete *begin;
54 		++begin;
55 	}
56 }
57 
58 // delete range of pointer values from map
59 template<class iter>
60 void ptr_map_delete(iter begin, iter end) {
61 	while(begin != end) {
62 		if((*begin).second)
63 			delete (*begin).second;
64 		++begin;
65 	}
66 }
67 
68 // a simple equality-test functor for maps
69 template<class key, class value>
70 class map_value_equal_to :
71 	public std::binary_function<std::pair<key,value>, value, bool> {
72 
73 public:
74 	bool operator()(const std::pair<key,value>& p, const value& v) const {
75 		return p.second == v;
76 	}
77 };
78 
79 //// a predicate functor adaptor for maps
80 //// e.moon 28jul99
81 //template<class key, class value>
82 //class map_value_predicate_t :
83 //	public unary_function<pair<key,value>, bool> {
84 //
85 //	const unary_function<const value, bool>& fn;
86 //
87 //public:
88 //	map_value_predicate_t(const unary_function<const value, bool>& _fn) : fn(_fn) {}
89 //	bool operator()(const std::pair<key,value>& p) const {
90 //		return fn(p.second);
91 //	}
92 //};
93 //
94 //template<class key, class value>
95 //inline map_value_predicate_t<key,value> map_value_predicate(
96 //	const unary_function<const value, bool>& fn) {
97 //	return map_value_predicate_t<key,value>(fn);
98 //}
99 
100 // copy values from a map subset
101 template<class input_iter, class output_iter>
102 void map_value_copy(input_iter begin, input_iter end, output_iter to) {
103 	while(begin != end) {
104 		*to = (*begin).second;
105 		++to;
106 		++begin;
107 	}
108 }
109 
110 // adapt a unary functor to a map (eek)
111 template <class pairT, class opT>
112 class unary_map_function_t :
113 	public std::unary_function<typename opT::argument_type, typename opT::result_type> {
114 
115 	opT f;
116 
117 public:
118 	unary_map_function_t(const opT& _f) : f(_f) {}
119 
120 	typename opT::result_type
121 	operator()(pairT& p) const {
122 		return f(p.second);
123 	}
124 };
125 
126 template <class mapT, class opT>
127 inline unary_map_function_t<typename mapT::value_type, opT>
128 unary_map_function(
129 	const mapT& map,
130 	const opT& f) {
131 	return unary_map_function_t<typename mapT::value_type, opT>(f);
132 }
133 
134 
135 __END_CORTEX_NAMESPACE
136 #endif /* __SET_TOOLS_H__ */
137