File: STL_Set.h

package info (click to toggle)
spring 88.0%2Bdfsg1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 41,524 kB
  • sloc: cpp: 343,114; ansic: 38,414; python: 12,257; java: 12,203; awk: 5,748; sh: 1,204; xml: 997; perl: 405; objc: 192; makefile: 181; php: 134; sed: 2
file content (95 lines) | stat: -rwxr-xr-x 2,273 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#ifndef CR_SET_TYPE_IMPL_H
#define CR_SET_TYPE_IMPL_H

#include "creg_cond.h"

#ifdef USING_CREG

#ifdef _MSC_VER
	#include <hash_set>
	#define SPRING_HASH_SET stdext::hash_set
#elif __GNUG__
	/* Test for GCC >= 4.3.2 */
	#if __GNUC__ > 4 || \
		(__GNUC__ == 4 && (__GNUC_MINOR__ > 3 || \
						(__GNUC_MINOR__ == 3 && \
							__GNUC_PATCHLEVEL__ >= 2)))
		#include <tr1/unordered_set>
		#define SPRING_HASH_SET std::tr1::unordered_set
	#else
		#define SPRING_HASH_SET __gnu_cxx::hash_set
		#include <ext/hash_set>
	#endif
#else
	#error Unsupported compiler
#endif

#include <set>

namespace creg
{
	// set/multiset - this template assumes the type is copyable
	template<typename T>
	class SetType : public IType
	{
		boost::shared_ptr<IType> elemType;
	public:
		typedef typename T::iterator iterator;

		SetType(boost::shared_ptr<IType> elemType) : elemType(elemType) {}
		~SetType() {}

		void Serialize(ISerializer* s, void* instance)
		{
			T& ct = *(T*)instance;
			if (s->IsWriting()) {
				int size = ct.size();
				s->SerializeInt(&size, sizeof(int));
				for (iterator i = ct.begin(); i != ct.end(); ++i)
					elemType->Serialize(s,(void*) &*i);
			} else {
				int size;
				s->SerializeInt(&size, sizeof(int));
				for (int i = 0; i < size; i++) {
					typename T::value_type v;
					elemType->Serialize(s, &v);
					ct.insert(v);
				}
			}
		}
		std::string GetName() { return "set<" + elemType->GetName() + ">"; }
	};


	// Set type
	template<typename T, typename C>
	struct DeduceType<std::set<T, C> > {
		boost::shared_ptr<IType> Get() {
			DeduceType<T> elemtype;
			return boost::shared_ptr<IType>(new SetType<std::set<T, C> >(elemtype.Get()));
		}
	};
	// Multiset
	template<typename T>
	struct DeduceType<std::multiset<T> > {
		boost::shared_ptr<IType> Get() {
			DeduceType<T> elemtype;
			return boost::shared_ptr<IType>(new SetType<std::multiset<T> >(elemtype.Get()));
		}
	};
	// Hash set
	template<typename T>
	struct DeduceType<SPRING_HASH_SET<T> > {
		boost::shared_ptr<IType> Get() {
			DeduceType<T> elemtype;
			return boost::shared_ptr<IType>(new SetType<SPRING_HASH_SET<T> >(elemtype.Get()));
		}
	};
};

#endif // USING_CREG

#endif // CR_SET_TYPE_IMPL_H