Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
L
libvector
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
iliya.saroukha
libvector
Commits
ce536fcd
Commit
ce536fcd
authored
1 year ago
by
iliya.saroukha
Browse files
Options
Downloads
Patches
Plain Diff
fix: vec_pop returns the value to the user instead of simply removing it
parent
d9dd6580
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
struct/vec.c
+15
-6
15 additions, 6 deletions
struct/vec.c
struct/vec.h
+1
-1
1 addition, 1 deletion
struct/vec.h
vec_test.c
+4
-1
4 additions, 1 deletion
vec_test.c
with
20 additions
and
8 deletions
struct/vec.c
+
15
−
6
View file @
ce536fcd
...
...
@@ -192,15 +192,23 @@ bool is_in_vec(vec_t *vec, int64_t data) {
}
/**
* @brief Function that removes the last element in a vector
* @brief Function that removes the last element in the vector and returns it
* to the user
*
* @param vec Vector from which to pop the value
* @param vec Vector from which the value will be removed
* @return Last element in the vector
*/
void
vec_pop
(
vec_t
*
vec
)
{
int64_t
vec_pop
(
vec_t
*
vec
)
{
err_init
(
vec
);
if
(
vec
==
NULL
)
{
fprintf
(
stderr
,
"Vector not initialised
\n
"
);
return
-
1
;
}
err_len_zero
(
vec
);
if
(
vec
->
len
==
0
)
{
fprintf
(
stderr
,
"Length of the vector is 0!
\n
"
);
return
-
1
;
}
// needs resizing
// should be extracted into separate function
if
(
vec
->
len
<=
vec
->
capacity
/
2
)
{
...
...
@@ -212,6 +220,7 @@ void vec_pop(vec_t *vec) {
}
vec
->
len
--
;
return
vec
->
data
[
vec
->
len
];
}
/**
...
...
@@ -292,6 +301,6 @@ void vec_destroy(vec_t **vec) {
free
((
*
vec
)
->
data
);
free
((
*
vec
));
(
*
vec
)
=
NULL
;
*
vec
=
NULL
;
}
}
This diff is collapsed.
Click to expand it.
struct/vec.h
+
1
−
1
View file @
ce536fcd
...
...
@@ -20,7 +20,7 @@ extern bool is_in_vec(vec_t *vec, int64_t data);
extern
void
vec_remove
(
vec_t
*
vec
,
size_t
idx
);
extern
void
vec_pop
(
vec_t
*
vec
);
extern
int64_t
vec_pop
(
vec_t
*
vec
);
extern
void
vec_print
(
vec_t
*
vec
);
...
...
This diff is collapsed.
Click to expand it.
vec_test.c
+
4
−
1
View file @
ce536fcd
...
...
@@ -56,6 +56,7 @@ int main(int argc, char **argv) {
}
int64_t
to_push
;
int64_t
item_poped
;
size_t
idx_to_drop
;
size_t
idx_to_insert
;
...
...
@@ -79,7 +80,9 @@ int main(int argc, char **argv) {
vec_remove
(
new_vec
,
idx_to_drop
);
break
;
case
3
:
vec_pop
(
new_vec
);
item_poped
=
vec_pop
(
new_vec
);
fprintf
(
stdout
,
"Last value in the vector: %ld was removed
\n
"
,
item_poped
);
break
;
case
4
:
vec_print
(
new_vec
);
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment