medfall

A super great game engine
Log | Files | Refs

foreach.hpp (2752B)


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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*  Relacy Race Detector
 *  Copyright (c) 2008-2010, Dmitry S. Vyukov
 *  All rights reserved.
 *  This software is provided AS-IS with no warranty, either express or implied.
 *  This software is distributed under a license and may not be copied,
 *  modified or distributed except as expressly authorized under the
 *  terms of the license contained in the file LICENSE.TXT in this distribution.
 */

#ifndef RL_FOREACH_HPP
#define RL_FOREACH_HPP
#ifdef _MSC_VER
#   pragma once
#endif

#include "base.hpp"

namespace rl
{


template<typename T, thread_id_t i, thread_id_t index>
struct foreach_thread_impl
{
    template<typename F>
    RL_INLINE static void exec(
        T* v1,
        F func)
    {
        (*func)(v1[i]);
        foreach_thread_impl<T, i + 1, index - 1>::exec(v1, func);
    }

    RL_INLINE static void exec(
        T* v1, T* v2,
        void (*func)(T& e1, T& e2))
    {
        (*func)(v1[i], v2[i]);
        foreach_thread_impl<T, i + 1, index - 1>::exec(v1, v2, func);
    }

    RL_INLINE static void exec(
        T* v1, T* v2, T* v3,
        void (*func)(T& e1, T& e2, T& e3))
    {
        (*func)(v1[i], v2[i], v3[i]);
        foreach_thread_impl<T, i + 1, index - 1>::exec(v1, v2, v3, func);
    }
};

template<typename T, thread_id_t i>
struct foreach_thread_impl<T, i, 0>
{
    template<typename F>
    RL_INLINE static void exec(
        T*,
        F)
    {
    }

    RL_INLINE static void exec(
        T*, T*,
        void (*)(T&, T&))
    {
    }

    RL_INLINE static void exec(
        T*, T*, T*,
        void (*)(T&, T&, T&))
    {
    }
};

template<thread_id_t count, typename T, typename F>
RL_INLINE void foreach(
    T* v1,
    F func)
{
    foreach_thread_impl<T, 0, count>::exec(v1, func);
}

template<thread_id_t count, typename T>
RL_INLINE void foreach(
    T* v1, T* v2,
    void (*func)(T& e1, T& e2))
{
    foreach_thread_impl<T, 0, count>::exec(v1, v2, func);
}

template<thread_id_t count, typename T>
RL_INLINE void foreach(
    T* v1, T* v2, T* v3,
    void (*func)(T& e1, T& e2, T& e3))
{
    foreach_thread_impl<T, 0, count>::exec(v1, v2, v3, func);
}

RL_INLINE void assign_zero(timestamp_t& elem)
{
    elem = 0;
}

RL_INLINE void assign_zero_u(unsigned& elem)
{
    elem = 0;
}

template<timestamp_t value>
RL_INLINE void assign(timestamp_t& elem)
{
    elem = value;
}

RL_INLINE void assign(timestamp_t& elem1, timestamp_t& elem2)
{
    elem1 = elem2;
}

RL_INLINE void assign_max(timestamp_t& elem1, timestamp_t& elem2)
{
    if (elem2 > elem1)
        elem1 = elem2;
}

RL_INLINE void plus_one(timestamp_t& elem)
{
    elem += 1;
}

}


#endif