Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
V
vector
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
daniel.rodrigue1
vector
Commits
4109643a
Commit
4109643a
authored
Feb 23, 2022
by
Daniel Rodriguez
Browse files
Options
Downloads
Patches
Plain Diff
fini les fonctions 'basiques'
parent
96733b32
No related branches found
No related tags found
No related merge requests found
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
.gitignore
+2
-0
2 additions, 0 deletions
.gitignore
Makefile
+22
-0
22 additions, 0 deletions
Makefile
main.c
+36
-0
36 additions, 0 deletions
main.c
vector.c
+102
-0
102 additions, 0 deletions
vector.c
vector.h
+26
-0
26 additions, 0 deletions
vector.h
with
188 additions
and
0 deletions
.gitignore
0 → 100644
+
2
−
0
View file @
4109643a
*.o
main
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Makefile
0 → 100644
+
22
−
0
View file @
4109643a
compile
=
gcc
-Wall
-Wextra
-fsanitize
=
address
execName
=
main
libraryName
=
vector
rerun
:
clean run
run
:
build
./
$(
execName
)
rebuild
:
clean build
build
:
$(execName).o $(libraryName).o
$(
compile
)
$^
-o
$(
execName
)
-lm
$(execName).o
:
$(execName).c
$(
compile
)
-c
$<
$(libraryName).o
:
$(libraryName).c $(libraryName).h
$(
compile
)
-c
$<
clean
:
rm
*
.o
$(
execName
)
\ No newline at end of file
This diff is collapsed.
Click to expand it.
main.c
0 → 100644
+
36
−
0
View file @
4109643a
#include
"vector.h"
#include
<stdio.h>
void
print_int
(
int
elem
){
printf
(
"%d"
,
elem
);
}
int
main
(){
vector
*
v
=
vector_create
();
vector_push
(
v
,
1
);
vector_push
(
v
,
2
);
vector_push
(
v
,
3
);
vector_push
(
v
,
4
);
vector_push
(
v
,
5
);
vector_print
(
v
,
print_int
);
vector_pop
(
v
);
vector_print
(
v
,
print_int
);
vector_insert
(
v
,
10
,
2
);
vector_print
(
v
,
print_int
);
vector_remove
(
v
,
1
);
vector_print
(
v
,
print_int
);
vector_remove
(
v
,
0
);
vector_print
(
v
,
print_int
);
vector_empty
(
v
);
vector_print
(
v
,
print_int
);
vector_free
(
v
);
return
EXIT_SUCCESS
;
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
vector.c
0 → 100644
+
102
−
0
View file @
4109643a
#include
<stddef.h>
#include
<stdlib.h>
#include
<stdio.h>
#include
<assert.h>
#include
"vector.h"
#define VECTOR_INIT_CAPACITY 4
struct
_vector
{
type
*
content
;
// actual content of the vector
int
capacity
;
// capacity allocated
int
length
;
// actual length
};
//typedef struct _vector vector;
static
void
handle_bounds
(
vector
*
v
,
int
index
){
if
(
index
<
0
||
index
>=
v
->
length
){
assert
(
false
);
}
}
vector
*
vector_create
(){
vector
*
v
=
malloc
(
sizeof
(
*
v
));
v
->
capacity
=
VECTOR_INIT_CAPACITY
;
v
->
length
=
0
;
v
->
content
=
malloc
(
v
->
capacity
*
sizeof
(
type
));
return
v
;
}
int
vector_length
(
vector
*
v
){
return
v
->
length
;
}
void
vector_push
(
vector
*
v
,
type
element
){
if
(
v
->
length
>=
v
->
capacity
){
v
->
capacity
*=
2
;
v
->
content
=
realloc
(
v
->
content
,
v
->
capacity
*
sizeof
(
type
));
}
v
->
content
[
v
->
length
]
=
element
;
v
->
length
+=
1
;
}
void
vector_pop
(
vector
*
v
){
v
->
length
-=
1
;
if
(
v
->
length
<
0
){
assert
(
false
);
}
if
(
v
->
length
<
(
v
->
capacity
/
4
)){
v
->
capacity
/=
2
;
v
->
content
=
realloc
(
v
->
content
,
v
->
capacity
*
sizeof
(
type
));
}
}
void
vector_set
(
vector
*
v
,
int
index
,
type
element
){
handle_bounds
(
v
,
index
);
v
->
content
[
index
]
=
element
;
}
type
vector_get
(
vector
*
v
,
int
index
){
handle_bounds
(
v
,
index
);
return
v
->
content
[
index
];
}
void
vector_remove
(
vector
*
v
,
int
index
){
handle_bounds
(
v
,
index
);
for
(
int
i
=
index
;
i
<
v
->
length
-
1
;
i
++
){
v
->
content
[
i
]
=
v
->
content
[
i
+
1
];
}
vector_pop
(
v
);
}
void
vector_insert
(
vector
*
v
,
type
element
,
int
index
){
handle_bounds
(
v
,
index
);
vector_push
(
v
,
0
);
for
(
int
i
=
v
->
length
-
1
;
i
>
index
;
i
--
){
v
->
content
[
i
]
=
v
->
content
[
i
-
1
];
}
v
->
content
[
index
]
=
element
;
}
void
vector_empty
(
vector
*
v
){
v
->
length
=
0
;
v
->
capacity
=
VECTOR_INIT_CAPACITY
;
v
->
content
=
realloc
(
v
->
content
,
v
->
capacity
*
sizeof
(
type
));
}
void
vector_free
(
vector
*
v
){
free
(
v
->
content
);
free
(
v
);
}
void
vector_print
(
vector
*
v
,
void
(
*
print
)(
type
)){
printf
(
"["
);
for
(
int
i
=
0
;
i
<
v
->
length
-
1
;
i
++
){
print
(
v
->
content
[
i
]);
printf
(
", "
);
}
if
(
v
->
length
>=
1
){
print
(
v
->
content
[
v
->
length
-
1
]);
}
printf
(
"]
\n
"
);
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
vector.h
0 → 100644
+
26
−
0
View file @
4109643a
#ifndef _VECTOR_H_
#define _VECTOR_H_
#include
<stdlib.h>
#include
<stdbool.h>
typedef
int
type
;
struct
_vector
;
typedef
struct
_vector
vector
;
vector
*
vector_create
();
int
vector_length
(
vector
*
v
);
void
vector_push
(
vector
*
v
,
type
element
);
void
vector_pop
(
vector
*
v
);
void
vector_set
(
vector
*
v
,
int
index
,
type
element
);
type
vector_get
(
vector
*
v
,
int
index
);
void
vector_remove
(
vector
*
v
,
int
index
);
void
vector_insert
(
vector
*
v
,
type
element
,
int
index
);
void
vector_empty
(
vector
*
v
);
void
vector_free
(
vector
*
v
);
void
vector_print
(
vector
*
v
,
void
(
*
print
)(
type
));
vector
*
vector_map
(
vector
*
v
,
type
(
*
f
)(
type
));
vector
*
vector_filter
(
vector
*
v
,
bool
(
*
f
)(
type
));
vector
*
vector_reduce
(
vector
*
v
,
type
neutral
,
type
(
*
f
)(
type
,
type
));
#endif
\ No newline at end of file
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
sign in
to comment