Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
O
OpenXG-RAN
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
lizhongxiao
OpenXG-RAN
Commits
f25fa70f
Commit
f25fa70f
authored
Nov 23, 2023
by
Robert Schmidt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
apply clang-format
parent
e8824827
Changes
7
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
148 additions
and
160 deletions
+148
-160
common/utils/alg/find.c
common/utils/alg/find.c
+8
-9
common/utils/alg/find.h
common/utils/alg/find.h
+18
-19
common/utils/alg/foreach.c
common/utils/alg/foreach.c
+2
-4
common/utils/alg/foreach.h
common/utils/alg/foreach.h
+6
-6
common/utils/ds/seq_arr.c
common/utils/ds/seq_arr.c
+30
-33
common/utils/ds/seq_arr.h
common/utils/ds/seq_arr.h
+66
-66
common/utils/ds/tests/test_seq_array.c
common/utils/ds/tests/test_seq_array.c
+18
-23
No files found.
common/utils/alg/find.c
View file @
f25fa70f
...
...
@@ -27,24 +27,23 @@ SOFTWARE.
#include <stdlib.h>
#include <stdio.h>
elm_arr_t
find_if_arr_it
(
seq_arr_t
*
arr
,
void
*
start_it
,
void
*
end_it
,
void
*
value
,
bool
(
*
f
)(
const
void
*
,
const
void
*
))
elm_arr_t
find_if_arr_it
(
seq_arr_t
*
arr
,
void
*
start_it
,
void
*
end_it
,
void
*
value
,
bool
(
*
f
)(
const
void
*
,
const
void
*
))
{
assert
(
arr
!=
NULL
);
while
(
start_it
!=
end_it
)
{
if
(
f
(
value
,
start_it
))
while
(
start_it
!=
end_it
)
{
if
(
f
(
value
,
start_it
))
return
(
elm_arr_t
){.
found
=
true
,
.
it
=
start_it
};
start_it
=
seq_arr_next
(
arr
,
start_it
);
start_it
=
seq_arr_next
(
arr
,
start_it
);
}
// If I trusted the average developer I should return it=start_it, but I don't.
return
(
elm_arr_t
){.
found
=
false
,
.
it
=
NULL
};
}
elm_arr_t
find_if_arr
(
seq_arr_t
*
arr
,
void
*
value
,
bool
(
*
f
)(
const
void
*
,
const
void
*
))
elm_arr_t
find_if_arr
(
seq_arr_t
*
arr
,
void
*
value
,
bool
(
*
f
)(
const
void
*
,
const
void
*
))
{
assert
(
arr
!=
NULL
);
void
*
start_it
=
seq_arr_front
(
arr
);
void
*
end_it
=
seq_arr_end
(
arr
);
return
find_if_arr_it
(
arr
,
start_it
,
end_it
,
value
,
f
);
}
common/utils/alg/find.h
View file @
f25fa70f
...
...
@@ -28,7 +28,7 @@ SOFTWARE.
#include <stdbool.h>
#include "../ds/seq_arr.h"
typedef
struct
{
typedef
struct
{
void
*
it
;
bool
found
;
}
elm_arr_t
;
...
...
@@ -41,8 +41,8 @@ typedef struct{
* @param value Pointer to the value that will be used by the predicate
* @param f Function representing the predicate
* @return Whether the predicate was fullfilled and the iterator to the element if true
*/
elm_arr_t
find_if_arr
(
seq_arr_t
*
arr
,
void
*
value
,
bool
(
*
f
)(
const
void
*
value
,
const
void
*
it
));
*/
elm_arr_t
find_if_arr
(
seq_arr_t
*
arr
,
void
*
value
,
bool
(
*
f
)(
const
void
*
value
,
const
void
*
it
));
/**
* @brief Find elements in an array in the semi-open range [start_it, end_it)
...
...
@@ -52,8 +52,7 @@ elm_arr_t find_if_arr(seq_arr_t* arr, void* value, bool(*f)(const void* value, c
* @param value Pointer to the value usied in the predicate
* @param f Function representing the predicate
* @return Whether the predicate was fullfilled and the iterator to the element if true
*/
elm_arr_t
find_if_arr_it
(
seq_arr_t
*
arr
,
void
*
start_it
,
void
*
end_it
,
void
*
value
,
bool
(
*
f
)(
const
void
*
value
,
const
void
*
it
));
*/
elm_arr_t
find_if_arr_it
(
seq_arr_t
*
arr
,
void
*
start_it
,
void
*
end_it
,
void
*
value
,
bool
(
*
f
)(
const
void
*
value
,
const
void
*
it
));
#endif
common/utils/alg/foreach.c
View file @
f25fa70f
...
...
@@ -33,10 +33,8 @@ void for_each(seq_arr_t* arr, void* value, void (*f)(void* value, void* it))
void
*
it
=
seq_arr_front
(
arr
);
void
*
end
=
seq_arr_end
(
arr
);
while
(
it
!=
end
)
{
while
(
it
!=
end
)
{
f
(
value
,
it
);
it
=
seq_arr_next
(
arr
,
it
);
it
=
seq_arr_next
(
arr
,
it
);
}
}
common/utils/alg/foreach.h
View file @
f25fa70f
...
...
@@ -32,7 +32,7 @@ SOFTWARE.
* @param arr The sequence container
* @param value Pointer to the value that will be used by the function applied
* @param fn_apply Function called by every element in the sequence container
*/
*/
void
for_each
(
seq_arr_t
*
arr
,
void
*
value
,
void
(
*
fn_apply
)(
void
*
value
,
void
*
it
));
#endif
common/utils/ds/seq_arr.c
View file @
f25fa70f
...
...
@@ -29,30 +29,27 @@ SOFTWARE.
#include <stdio.h>
#include <string.h>
static
const
size_t
MIN_SIZE
=
8
;
static
const
size_t
MIN_SIZE
=
8
;
static
void
maybe_expand
(
seq_arr_t
*
arr
)
static
void
maybe_expand
(
seq_arr_t
*
arr
)
{
if
(
arr
->
size
+
1
==
arr
->
cap
)
{
arr
->
data
=
realloc
(
arr
->
data
,
2
*
arr
->
cap
*
arr
->
elt_size
);
if
(
arr
->
size
+
1
==
arr
->
cap
)
{
arr
->
data
=
realloc
(
arr
->
data
,
2
*
arr
->
cap
*
arr
->
elt_size
);
assert
(
arr
->
data
!=
NULL
&&
"realloc failed to allocate memory"
);
arr
->
cap
*=
2
;
arr
->
cap
*=
2
;
}
}
static
void
maybe_shrink
(
seq_arr_t
*
arr
)
static
void
maybe_shrink
(
seq_arr_t
*
arr
)
{
const
float
occ
=
(
float
)
arr
->
size
/
(
float
)
arr
->
cap
;
if
(
arr
->
size
>
MIN_SIZE
&&
occ
<
0
.
25
)
{
if
(
arr
->
size
>
MIN_SIZE
&&
occ
<
0
.
25
)
{
assert
(
arr
->
cap
>
MIN_SIZE
);
seq_arr_t
tmp
=
{.
data
=
NULL
,
.
size
=
arr
->
size
,
.
elt_size
=
arr
->
elt_size
,
.
cap
=
arr
->
cap
/
2
};
seq_arr_t
tmp
=
{.
data
=
NULL
,
.
size
=
arr
->
size
,
.
elt_size
=
arr
->
elt_size
,
.
cap
=
arr
->
cap
/
2
};
tmp
.
data
=
calloc
(
tmp
.
cap
,
tmp
.
cap
);
assert
(
tmp
.
data
!=
NULL
&&
"Memory exhausted"
);
assert
(
arr
->
size
<=
tmp
.
cap
);
memcpy
(
tmp
.
data
,
arr
->
data
,
arr
->
size
*
arr
->
elt_size
);
memcpy
(
tmp
.
data
,
arr
->
data
,
arr
->
size
*
arr
->
elt_size
);
free
(
arr
->
data
);
memcpy
(
arr
,
&
tmp
,
sizeof
(
seq_arr_t
));
}
...
...
@@ -67,15 +64,15 @@ void seq_arr_init(seq_arr_t* arr, size_t elt_size) //__attribute__(malloc)
assert
(
arr
->
data
!=
NULL
);
}
void
seq_arr_free
(
seq_arr_t
*
arr
,
void
(
*
free_func
)(
void
*
)
)
void
seq_arr_free
(
seq_arr_t
*
arr
,
void
(
*
free_func
)(
void
*
))
{
assert
(
arr
!=
NULL
);
assert
(
arr
->
data
!=
NULL
);
if
(
free_func
!=
NULL
)
{
if
(
free_func
!=
NULL
)
{
void
*
start_it
=
seq_arr_front
(
arr
);
void
*
end_it
=
seq_arr_end
(
arr
);
while
(
start_it
!=
end_it
)
{
while
(
start_it
!=
end_it
)
{
free_func
(
start_it
);
start_it
=
seq_arr_next
(
arr
,
start_it
);
}
...
...
@@ -88,7 +85,7 @@ void seq_arr_push_back(seq_arr_t* arr, void* data, size_t len)
{
assert
(
arr
!=
NULL
);
assert
(
arr
->
data
!=
NULL
);
//assert(data != NULL);
//
assert(data != NULL);
assert
(
len
==
arr
->
elt_size
);
maybe_expand
(
arr
);
...
...
@@ -122,12 +119,13 @@ void seq_arr_erase_it(seq_arr_t* arr, void* start_it, void* end_it, void (*free_
assert
(
end_it
!=
NULL
);
assert
(
end_it
>=
start_it
);
if
(
start_it
==
end_it
)
return
;
if
(
start_it
==
end_it
)
return
;
if
(
free_func
!=
NULL
)
{
if
(
free_func
!=
NULL
)
{
void
*
start_it
=
seq_arr_front
(
arr
);
void
*
end_it
=
seq_arr_end
(
arr
);
while
(
start_it
!=
end_it
)
{
while
(
start_it
!=
end_it
)
{
free_func
(
start_it
);
start_it
=
seq_arr_next
(
arr
,
start_it
);
}
...
...
@@ -138,7 +136,7 @@ void seq_arr_erase_it(seq_arr_t* arr, void* start_it, void* end_it, void (*free_
memmove
(
start_it
,
end_it
,
num_bytes_move
);
const
int
num_bytes_erase
=
end_it
-
start_it
;
const
int32_t
num_elm_erase
=
num_bytes_erase
/
arr
->
elt_size
;
const
int32_t
num_elm_erase
=
num_bytes_erase
/
arr
->
elt_size
;
assert
(
num_elm_erase
>
0
);
arr
->
size
-=
num_elm_erase
;
...
...
@@ -170,7 +168,7 @@ void* seq_arr_end(seq_arr_t const* arr)
{
assert
(
arr
!=
NULL
);
assert
(
arr
->
data
!=
NULL
);
return
&
arr
->
data
[
arr
->
size
*
arr
->
elt_size
];
return
&
arr
->
data
[
arr
->
size
*
arr
->
elt_size
];
}
void
*
seq_arr_at
(
seq_arr_t
const
*
arr
,
uint32_t
pos
)
...
...
@@ -178,7 +176,7 @@ void* seq_arr_at(seq_arr_t const* arr, uint32_t pos)
assert
(
arr
!=
NULL
);
assert
(
arr
->
data
!=
NULL
);
assert
(
pos
<
arr
->
size
);
return
arr
->
data
+
pos
*
arr
->
elt_size
;
return
arr
->
data
+
pos
*
arr
->
elt_size
;
}
ptrdiff_t
seq_arr_dist
(
seq_arr_t
const
*
arr
,
void
const
*
first
,
void
const
*
last
)
...
...
@@ -186,8 +184,7 @@ ptrdiff_t seq_arr_dist(seq_arr_t const* arr, void const* first, void const* last
assert
(
arr
!=
NULL
);
assert
(
first
!=
NULL
);
assert
(
last
!=
NULL
);
const
ptrdiff_t
pos
=
(
last
-
first
)
/
arr
->
elt_size
;
const
ptrdiff_t
pos
=
(
last
-
first
)
/
arr
->
elt_size
;
assert
(
pos
>
-
1
);
return
pos
;
}
common/utils/ds/seq_arr.h
View file @
f25fa70f
...
...
@@ -34,7 +34,7 @@ SOFTWARE.
#include <stdlib.h>
#include <stdint.h>
typedef
struct
seq_arr_s
{
typedef
struct
seq_arr_s
{
uint8_t
*
data
;
size_t
size
;
const
size_t
elt_size
;
...
...
@@ -46,7 +46,7 @@ typedef struct seq_arr_s{
* @brief Constructor.
* @param arr The sequence container
* @param elm_sz value returned by the sizeof operator of the type that the container will hold e.g., sizeof(int).
*/
*/
void
seq_arr_init
(
seq_arr_t
*
arr
,
size_t
elm_sz
);
/**
...
...
@@ -54,22 +54,23 @@ void seq_arr_init(seq_arr_t* arr, size_t elm_sz);
* @brief Destructor.
* @param arr The sequence container
* @param free_func Function called for every element while destructing e.g., useful to free memory of deep objects.
*/
*/
void
seq_arr_free
(
seq_arr_t
*
arr
,
void
(
*
free_func
)(
void
*
it
));
/**
* @brief Push back elements into the sequence container. Value semantic. i.e., the void* data will be shallowly copied in the array.
* @brief Push back elements into the sequence container. Value semantic. i.e., the void* data will be shallowly copied in the
* array.
* @param arr The sequence container
* @param data Pointer to the data to be copied
* @param elm_sz Size of the element to be copied e.g., sizeof(int)
*/
*/
void
seq_arr_push_back
(
seq_arr_t
*
arr
,
void
*
data
,
size_t
elm_sz
);
/**
* @brief Erase the element pointed by it
* @param arr The sequence container
* @param it Iterator to the element to erase
*/
*/
void
seq_arr_erase
(
seq_arr_t
*
,
void
*
it
);
/**
...
...
@@ -77,7 +78,7 @@ void seq_arr_erase(seq_arr_t*, void* it);
* @param arr The sequence container
* @param it Iterator to the element to erase
* @param free_func Function to call while erasing element
*/
*/
void
seq_arr_erase_deep
(
seq_arr_t
*
arr
,
void
*
it
,
void
(
*
free_func
)(
void
*
it
));
/**
...
...
@@ -87,14 +88,14 @@ void seq_arr_erase_deep(seq_arr_t* arr, void* it, void (*free_func)(void* it));
* @param last Iterator to the last element. Note that this element will NOT be erased
* @param f Function that will be called by every element while erasing. Useful for deep copies. Pass NULL if shallow
* erased required
*/
*/
void
seq_arr_erase_it
(
seq_arr_t
*
arr
,
void
*
first
,
void
*
last
,
void
(
*
free_func
)(
void
*
it
));
/**
* @brief Elements in the array
* @param arr The sequence container
* @return The number of elements in the sequence container
*/
*/
size_t
seq_arr_size
(
seq_arr_t
const
*
arr
);
/////
...
...
@@ -105,7 +106,7 @@ size_t seq_arr_size(seq_arr_t const* arr);
* @brief First iterator
* @param arr The sequence container
* @return The iterator to the first element in the sequence container
*/
*/
void
*
seq_arr_front
(
seq_arr_t
const
*
arr
);
/**
...
...
@@ -113,14 +114,14 @@ void* seq_arr_front(seq_arr_t const* arr);
* @param arr The sequence container
* @param it Iterator to a valid element
* @return The iterator to the next element in the sequence container
*/
*/
void
*
seq_arr_next
(
seq_arr_t
const
*
arr
,
void
const
*
it
);
/**
* @brief End iterator
* @param arr The sequence container
* @return The iterator to one past the last element in the sequence container
*/
*/
void
*
seq_arr_end
(
seq_arr_t
const
*
arr
);
/**
...
...
@@ -128,7 +129,7 @@ void* seq_arr_end(seq_arr_t const* arr);
* @param arr The sequence container
* @param pos The position of the element in the sequence container
* @return The iterator to the element
*/
*/
void
*
seq_arr_at
(
seq_arr_t
const
*
arr
,
uint32_t
pos
);
/**
...
...
@@ -137,8 +138,7 @@ void* seq_arr_at(seq_arr_t const* arr, uint32_t pos);
* @param first Iterator to first element
* @param last Iterator to last element
* @return The distance among iterators
*/
*/
ptrdiff_t
seq_arr_dist
(
seq_arr_t
const
*
arr
,
void
const
*
first
,
void
const
*
last
);
#endif
common/utils/ds/tests/test_seq_array.c
View file @
f25fa70f
...
...
@@ -8,33 +8,29 @@
To compile: gcc test_seq_array.c ../seq_arr.c ../../alg/find.c ../../alg/foreach.c
*/
static
bool
eq_int
(
const
void
*
value
,
const
void
*
it
)
static
bool
eq_int
(
const
void
*
value
,
const
void
*
it
)
{
const
int
*
v
=
(
const
int
*
)
value
;
const
int
*
v
=
(
const
int
*
)
value
;
const
int
*
i
=
(
const
int
*
)
it
;
return
*
v
==
*
i
;
}
static
void
sum_elm
(
void
*
value
,
void
*
it
)
static
void
sum_elm
(
void
*
value
,
void
*
it
)
{
int
*
sum
=
(
int
*
)
value
;
int
*
elm
=
(
int
*
)
it
;
*
sum
+=
*
elm
;
}
static
int
compar
(
const
void
*
m0
,
const
void
*
m1
)
static
int
compar
(
const
void
*
m0
,
const
void
*
m1
)
{
int
*
a
=
(
int
*
)
m0
;
int
*
b
=
(
int
*
)
m1
;
int
*
a
=
(
int
*
)
m0
;
int
*
b
=
(
int
*
)
m1
;
if
(
*
a
<
*
b
)
if
(
*
a
<
*
b
)
return
-
1
;
if
(
*
a
==
*
b
)
if
(
*
a
==
*
b
)
return
0
;
return
1
;
...
...
@@ -46,7 +42,7 @@ int main()
seq_arr_init
(
&
arr
,
sizeof
(
int
));
// Insert data and expand
for
(
int
i
=
0
;
i
<
100
;
++
i
)
for
(
int
i
=
0
;
i
<
100
;
++
i
)
seq_arr_push_back
(
&
arr
,
&
i
,
sizeof
(
int
));
// Check inserted data
...
...
@@ -58,7 +54,7 @@ int main()
int
value
=
50
;
elm_arr_t
elm
=
find_if_arr
(
&
arr
,
&
value
,
eq_int
);
assert
(
elm
.
found
==
true
);
//Check
//
Check
assert
(
*
(
int
*
)
elm
.
it
==
50
);
assert
(
seq_arr_dist
(
&
arr
,
seq_arr_front
(
&
arr
),
elm
.
it
)
==
50
);
...
...
@@ -74,11 +70,11 @@ int main()
assert
(
*
(
int
*
)
seq_arr_at
(
&
arr
,
50
)
==
51
);
// Erase range and force shrink
seq_arr_erase_it
(
&
arr
,
seq_arr_front
(
&
arr
),
seq_arr_at
(
&
arr
,
90
)
,
NULL
);
seq_arr_erase_it
(
&
arr
,
seq_arr_front
(
&
arr
),
seq_arr_at
(
&
arr
,
90
)
,
NULL
);
assert
(
seq_arr_size
(
&
arr
)
==
9
);
assert
(
*
(
int
*
)
seq_arr_front
(
&
arr
)
==
91
);
//Recall that C functions qsort and bsearch, also work
//
Recall that C functions qsort and bsearch, also work
int
key
=
95
;
void
*
base
=
seq_arr_front
(
&
arr
);
size_t
nmemb
=
seq_arr_size
(
&
arr
);
...
...
@@ -92,4 +88,3 @@ int main()
return
EXIT_SUCCESS
;
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment