xref: /haiku/src/apps/cortex/support/set_tools.h (revision e1c4049fed1047bdb957b0529e1921e97ef94770)
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 {
72 public:
73 	bool operator()(const std::pair<key,value>& p, const value& v) const {
74 		return p.second == v;
75 	}
76 };
77 
78 //// a predicate functor adaptor for maps
79 //// e.moon 28jul99
80 //template<class key, class value>
81 //class map_value_predicate_t :
82 //	public unary_function<pair<key,value>, bool> {
83 //
84 //	const unary_function<const value, bool>& fn;
85 //
86 //public:
87 //	map_value_predicate_t(const unary_function<const value, bool>& _fn) : fn(_fn) {}
88 //	bool operator()(const std::pair<key,value>& p) const {
89 //		return fn(p.second);
90 //	}
91 //};
92 //
93 //template<class key, class value>
94 //inline map_value_predicate_t<key,value> map_value_predicate(
95 //	const unary_function<const value, bool>& fn) {
96 //	return map_value_predicate_t<key,value>(fn);
97 //}
98 
99 // copy values from a map subset
100 template<class input_iter, class output_iter>
101 void map_value_copy(input_iter begin, input_iter end, output_iter to) {
102 	while(begin != end) {
103 		*to = (*begin).second;
104 		++to;
105 		++begin;
106 	}
107 }
108 
109 // adapt a unary functor to a map (eek)
110 template <class pairT, class opT>
111 class unary_map_function_t
112 {
113 
114 	opT f;
115 
116 public:
117 	unary_map_function_t(const opT& _f) : f(_f) {}
118 
119 	typename opT::result_type
120 	operator()(pairT& p) const {
121 		return f(p.second);
122 	}
123 };
124 
125 template <class mapT, class opT>
126 inline unary_map_function_t<typename mapT::value_type, opT>
127 unary_map_function(
128 	const mapT& map,
129 	const opT& f) {
130 	return unary_map_function_t<typename mapT::value_type, opT>(f);
131 }
132 
133 
134 __END_CORTEX_NAMESPACE
135 #endif /* __SET_TOOLS_H__ */
136