summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorali <ali198724@gmail.com>2020-01-23 06:54:44 +0900
committerWooHyun Jung <wh0705.jung@samsung.com>2020-01-23 06:54:44 +0900
commit5137f6d143c681fcf4f53e4e45df8af1a538ae75 (patch)
tree621eebcc5eeb333c6cac1bbdd555e3b9ea3aa4fb
parent93bd97025983e48ae8048d0c63b5d8b314f1de42 (diff)
eina: add new range Data type
Summary: Introduce new data type (Eina.Range) which represent range (part of series) **eina_range_max_get** **eina_range_intersect** **eina_range_union** **eina_range_contains** **eina_range_equal** Reviewers: cedric, woohyun, bu5hm4n, segfaultxavi, zmike Reviewed By: woohyun Subscribers: vtorri, cedric, #committers, #reviewers Tags: #efl Maniphest Tasks: T8570 Differential Revision: https://phab.enlightenment.org/D11133
-rw-r--r--src/lib/eina/Eina.h1
-rw-r--r--src/lib/eina/eina_inline_range.x82
-rw-r--r--src/lib/eina/eina_range.h116
-rw-r--r--src/lib/eina/meson.build2
-rw-r--r--src/lib/eo/eina_types.eot6
-rw-r--r--src/tests/eina/eina_suite.c1
-rw-r--r--src/tests/eina/eina_suite.h1
-rw-r--r--src/tests/eina/eina_test_range.c72
-rw-r--r--src/tests/eina/meson.build1
9 files changed, 282 insertions, 0 deletions
diff --git a/src/lib/eina/Eina.h b/src/lib/eina/Eina.h
index bbf3161929..9e3345e432 100644
--- a/src/lib/eina/Eina.h
+++ b/src/lib/eina/Eina.h
@@ -212,6 +212,7 @@ extern "C" {
212#include <eina_main.h> 212#include <eina_main.h>
213#include <eina_fp.h> 213#include <eina_fp.h>
214#include <eina_rectangle.h> 214#include <eina_rectangle.h>
215#include <eina_range.h>
215#include <eina_clist.h> 216#include <eina_clist.h>
216#include <eina_inlist.h> 217#include <eina_inlist.h>
217#include <eina_file.h> 218#include <eina_file.h>
diff --git a/src/lib/eina/eina_inline_range.x b/src/lib/eina/eina_inline_range.x
new file mode 100644
index 0000000000..b5af9d7226
--- /dev/null
+++ b/src/lib/eina/eina_inline_range.x
@@ -0,0 +1,82 @@
1/* EINA - EFL data type library
2 * Copyright (C) 2020 Ali Alzyod
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library;
16 * if not, see <http://www.gnu.org/licenses/>.
17 */
18#ifndef EINA_INLINE_RANGE_H_
19#define EINA_INLINE_RANGE_H_
20
21/*============================================================================*
22 * API *
23 *============================================================================*/
24
25static inline size_t eina_range_end(const Eina_Range *range)
26{
27 if (range) return range->start + range->length;
28 return 0;
29}
30
31static inline Eina_Range eina_range_intersect(const Eina_Range *range, const Eina_Range *range2)
32{
33 Eina_Range ret_range = EINA_RANGE_EMPTY();
34 size_t min, max;
35
36 if (!range || !range2)
37 return ret_range;
38
39 min = MAX(range->start, range2->start);
40 max = MIN(range->start + range->length, range2->start + range2->length);
41
42 if (max > min)
43 {
44 ret_range = eina_range_from_to(min, max);
45 }
46
47 return ret_range;
48}
49
50static inline Eina_Range eina_range_union(const Eina_Range *range, const Eina_Range *range2)
51{
52 size_t min, max;
53
54 if (!range || !range2)
55 return EINA_RANGE_EMPTY();
56
57 min = MIN(range->start, range2->start);
58 max = MAX(range->start + range->length, range2->start + range2->length);
59
60 return eina_range_from_to(min, max);
61}
62
63static inline Eina_Bool eina_range_contains(const Eina_Range *range, size_t value)
64{
65 if (!range) return EINA_FALSE;
66
67 return value >= range->start && value < (range->start + range->length);
68}
69
70static inline Eina_Bool eina_range_equal(const Eina_Range *range, const Eina_Range *range2)
71{
72 if (!range || !range2) return EINA_FALSE;
73 return (range->start == range2->start && range->length == range2->length);
74}
75
76static inline Eina_Range eina_range_from_to(size_t min, size_t max)
77{
78 if (min < max) return EINA_RANGE(min, max - min);
79 return EINA_RANGE(max, min - max);;
80}
81
82#endif // EINA_INLINE_RANGE_H_ \ No newline at end of file
diff --git a/src/lib/eina/eina_range.h b/src/lib/eina/eina_range.h
new file mode 100644
index 0000000000..b5ffac92f1
--- /dev/null
+++ b/src/lib/eina/eina_range.h
@@ -0,0 +1,116 @@
1/* EINA - EFL data type library
2 * Copyright (C) 2020 Ali Alzyod
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library;
16 * if not, see <http://www.gnu.org/licenses/>.
17 */
18
19#ifndef EINA_RANGE_H_
20#define EINA_RANGE_H_
21
22#include "eina_types.h"
23
24#include "eina_cpu.h"
25
26#define EINA_RANGE(start, length) ((Eina_Range) { (start), (length) })
27#define EINA_RANGE_FROM_TO(from, to) ((Eina_Range) { MIN((from),(to)), ABS((to) - (from)) })
28#define EINA_RANGE_ZERO() EINA_RANGE(0, 0)
29#define EINA_RANGE_EMPTY() ((Eina_Range) EINA_RANGE_ZERO())
30
31/** @brief A Int range */
32typedef struct _Eina_Range
33{
34 size_t start;
35 size_t length;
36} Eina_Range;
37
38
39/**
40 * @brief convenience macro for comparing two Eina_Range structs
41 * @param[in] a An Eina_Range
42 * @param[in] b An Eina_Range
43 * @return 1 if the structs are equal, 0 if they are not
44 * @since 1.24
45 */
46#define EINA_RANGE_EQ(a, b) \
47 (((a).start == (b).start) && ((a).length == (b).length))
48
49/**
50 * @brief Get end value in range (not included in range).
51 *
52 * @param[in] range The Range.
53 * @return The sum of end and length of the range.
54 *
55 * @since 1.24
56 * */
57static inline size_t eina_range_end(const Eina_Range *range);
58
59/**
60 * @brief Intersection between two ranges.
61 *
62 * @param[in] range The first range.
63 * @param[in] range2 The second range.
64 * @return The intersection between two ranges, If ranges do not intersect return length will be 0.
65 *
66 * @since 1.24
67 * */
68static inline Eina_Range eina_range_intersect(const Eina_Range *range, const Eina_Range *range2);
69
70
71/**
72 * @brief Union between two ranges.
73 *
74 * @param[in] range The first range.
75 * @param[in] range2 The second range.
76 * @return The union between two ranges.
77 *
78 * @since 1.24
79 * */
80static inline Eina_Range eina_range_union(const Eina_Range *range, const Eina_Range *range2);
81
82/**
83 * @brief Check if value is set in a range.
84 *
85 * @param[in] range The range.
86 * @return Wither value set within range.
87 *
88 * @since 1.24
89 * */
90static inline Eina_Bool eina_range_contains(const Eina_Range *range, size_t value);
91
92/**
93 * @brief Check if two ranges are equal.
94 *
95 * @param[in] range The first range.
96 * @param[in] range2 The second range.
97 * @return Wither two ranges are equal.
98 *
99 * @since 1.24
100 * */
101static inline Eina_Bool eina_range_equal(const Eina_Range *range, const Eina_Range *range2);
102
103/**
104 * @brief Check if two ranges are equal.
105 *
106 * @param[in] min The min value in range.
107 * @param[in] max The max value in range.
108 * @return range.
109 *
110 * @since 1.24
111 * */
112static inline Eina_Range eina_range_from_to(size_t min, size_t max);
113
114#include "eina_inline_range.x"
115
116#endif /*EINA_RANGE_H_*/
diff --git a/src/lib/eina/meson.build b/src/lib/eina/meson.build
index 7866323742..97e1669c4f 100644
--- a/src/lib/eina/meson.build
+++ b/src/lib/eina/meson.build
@@ -32,6 +32,7 @@ public_sub_headers = [
32'eina_mempool.h', 32'eina_mempool.h',
33'eina_module.h', 33'eina_module.h',
34'eina_rectangle.h', 34'eina_rectangle.h',
35'eina_range.h',
35'eina_types.h', 36'eina_types.h',
36'eina_array.h', 37'eina_array.h',
37'eina_counter.h', 38'eina_counter.h',
@@ -51,6 +52,7 @@ public_sub_headers = [
51'eina_inline_rbtree.x', 52'eina_inline_rbtree.x',
52'eina_inline_mempool.x', 53'eina_inline_mempool.x',
53'eina_inline_rectangle.x', 54'eina_inline_rectangle.x',
55'eina_inline_range.x',
54'eina_inline_trash.x', 56'eina_inline_trash.x',
55'eina_thread.h', 57'eina_thread.h',
56'eina_trash.h', 58'eina_trash.h',
diff --git a/src/lib/eo/eina_types.eot b/src/lib/eo/eina_types.eot
index 4cb8e9347d..d4033e26aa 100644
--- a/src/lib/eo/eina_types.eot
+++ b/src/lib/eo/eina_types.eot
@@ -60,6 +60,12 @@ struct @extern Eina.Matrix3 {
60 zz: double; [[ZZ matrix value]] 60 zz: double; [[ZZ matrix value]]
61} 61}
62 62
63struct @extern @beta Eina.Range {
64 [[A range sequence of values.]]
65 start: size; [[Start of the range.]]
66 length: size; [[Length of the range.]]
67}
68
63type @extern Eina.Unicode: uint32; [[Eina unicode type. @since 1.24]] 69type @extern Eina.Unicode: uint32; [[Eina unicode type. @since 1.24]]
64 70
65struct @extern @beta Eina.File_Direct_Info; [[Eina file direct information data structure]] 71struct @extern @beta Eina.File_Direct_Info; [[Eina file direct information data structure]]
diff --git a/src/tests/eina/eina_suite.c b/src/tests/eina/eina_suite.c
index dfa2bd1e5f..45316bd462 100644
--- a/src/tests/eina/eina_suite.c
+++ b/src/tests/eina/eina_suite.c
@@ -57,6 +57,7 @@ static const Efl_Test_Case etc[] = {
57 { "Benchmark", eina_test_benchmark }, 57 { "Benchmark", eina_test_benchmark },
58 { "Mempool", eina_test_mempool }, 58 { "Mempool", eina_test_mempool },
59 { "Rectangle", eina_test_rectangle }, 59 { "Rectangle", eina_test_rectangle },
60 { "Range", eina_test_range },
60 { "MatrixSparse", eina_test_matrixsparse }, 61 { "MatrixSparse", eina_test_matrixsparse },
61 { "Tiler", eina_test_tiler }, 62 { "Tiler", eina_test_tiler },
62 { "Strbuf", eina_test_strbuf }, 63 { "Strbuf", eina_test_strbuf },
diff --git a/src/tests/eina/eina_suite.h b/src/tests/eina/eina_suite.h
index 5e7a1bf30f..102de63193 100644
--- a/src/tests/eina/eina_suite.h
+++ b/src/tests/eina/eina_suite.h
@@ -45,6 +45,7 @@ void eina_test_file(TCase *tc);
45void eina_test_benchmark(TCase *tc); 45void eina_test_benchmark(TCase *tc);
46void eina_test_mempool(TCase *tc); 46void eina_test_mempool(TCase *tc);
47void eina_test_rectangle(TCase *tc); 47void eina_test_rectangle(TCase *tc);
48void eina_test_range(TCase *tc);
48void eina_test_matrixsparse(TCase *tc); 49void eina_test_matrixsparse(TCase *tc);
49void eina_test_tiler(TCase *tc); 50void eina_test_tiler(TCase *tc);
50void eina_test_strbuf(TCase *tc); 51void eina_test_strbuf(TCase *tc);
diff --git a/src/tests/eina/eina_test_range.c b/src/tests/eina/eina_test_range.c
new file mode 100644
index 0000000000..a1f84b8a00
--- /dev/null
+++ b/src/tests/eina/eina_test_range.c
@@ -0,0 +1,72 @@
1/* EINA - EFL data type library
2 * Copyright (C) 2020 Ali Alzyod
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library;
16 * if not, see <http://www.gnu.org/licenses/>.
17 */
18
19#ifdef HAVE_CONFIG_H
20# include "config.h"
21#endif
22
23#include <stdio.h>
24
25#include <Eina.h>
26
27#include "eina_suite.h"
28
29EFL_START_TEST(eina_range_intersect_union_test)
30{
31 Eina_Range r1 = EINA_RANGE(0, 10);
32 Eina_Range r2 = EINA_RANGE(5, 15);
33
34 Eina_Range r_intersect = eina_range_intersect(&r1, &r2);
35 ck_assert_uint_eq(r_intersect.start, 5);
36 ck_assert_uint_eq(r_intersect.length, 5);
37
38 Eina_Range r_union = eina_range_union(&r1, &r2);
39 ck_assert_uint_eq(r_union.start, 0);
40 ck_assert_uint_eq(r_union.length, 20);
41}
42EFL_END_TEST
43
44EFL_START_TEST(eina_range_contains_test)
45{
46 Eina_Range r1 = EINA_RANGE(0, 10);
47
48 ck_assert(eina_range_contains(&r1,0));
49 ck_assert(eina_range_contains(&r1,9));
50 ck_assert(!eina_range_contains(&r1,10));
51}
52EFL_END_TEST
53
54EFL_START_TEST(eina_range_equal_test)
55{
56 Eina_Range r1 = EINA_RANGE(0, 10);
57 Eina_Range r2 = EINA_RANGE(0, 10);
58 Eina_Range r3 = EINA_RANGE(0, 9);
59
60 ck_assert(eina_range_equal(&r1, &r2));
61 ck_assert(!eina_range_equal(&r1, &r3));
62}
63EFL_END_TEST
64
65
66void
67eina_test_range(TCase *tc)
68{
69 tcase_add_test(tc, eina_range_intersect_union_test);
70 tcase_add_test(tc, eina_range_contains_test);
71 tcase_add_test(tc, eina_range_equal_test);
72}
diff --git a/src/tests/eina/meson.build b/src/tests/eina/meson.build
index b6ba688e8b..f755be4c2a 100644
--- a/src/tests/eina/meson.build
+++ b/src/tests/eina/meson.build
@@ -29,6 +29,7 @@ eina_test_src = files(
29'eina_test_benchmark.c', 29'eina_test_benchmark.c',
30'eina_test_mempool.c', 30'eina_test_mempool.c',
31'eina_test_rectangle.c', 31'eina_test_rectangle.c',
32'eina_test_range.c',
32'eina_test_list.c', 33'eina_test_list.c',
33'eina_test_matrixsparse.c', 34'eina_test_matrixsparse.c',
34'eina_test_tiler.c', 35'eina_test_tiler.c',