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
Hepia_2021-2022
Prog_Seq
Vector
Commits
3a0bd826
Commit
3a0bd826
authored
3 years ago
by
Alec
Browse files
Options
Downloads
Patches
Plain Diff
kek
parent
401211f2
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
TP6
+0
-0
0 additions, 0 deletions
TP6
main.c
+6
-0
6 additions, 0 deletions
main.c
makefile
+19
-0
19 additions, 0 deletions
makefile
vector.c
+165
-0
165 additions, 0 deletions
vector.c
vector.h
+21
-0
21 additions, 0 deletions
vector.h
with
211 additions
and
0 deletions
TP6
0 → 100755
+
0
−
0
View file @
3a0bd826
File added
This diff is collapsed.
Click to expand it.
main.c
0 → 100644
+
6
−
0
View file @
3a0bd826
#include
"vector.h"
int
main
()
{
return
0
;
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
makefile
0 → 100644
+
19
−
0
View file @
3a0bd826
CC
=
gcc
FLAGS
=
-Wall
-Werror
CFLAGS
=
-g
-std
=
c99
LDFLAGS
=
-fsanitize
=
address
-fsanitize
=
leak
main
:
main.o vector.o
$(
CC
)
main.o vector.o
-o
TP6
$(
CFLAGS
)
$(
LDFLAGS
)
main.o
:
main.c
$(
CC
)
$(
FLAGS
)
$(
CFLAGS
)
$(
LDFLAGS
)
-c
$^
vector.o
:
vector.c vector.h
$(
CC
)
$(
FLAGS
)
$(
CFLAGS
)
$(
LDFLAGS
)
-c
$<
clean
:
rm
-f
*
.o main
rebuild
:
clean TP6
\ No newline at end of file
This diff is collapsed.
Click to expand it.
vector.c
0 → 100644
+
165
−
0
View file @
3a0bd826
/*
* Author : Alec Schmidt
* Date : 22.02.2022
*/
#include
<stdio.h>
#include
<stdlib.h>
#include
<assert.h>
#include
<stdbool.h>
#include
"vector.h"
#define VECTOR_INIT_CAPACITY 4
typedef
struct
_vector
{
type
*
data
;
// actual content of the vector
int
capacity
;
// capacity allocated
int
length
;
// actual length
}
vector
;
vector
vector_create
()
{
vector
v
;
v
.
capacity
=
VECTOR_INIT_CAPACITY
;
v
.
length
=
0
;
v
.
data
=
malloc
(
VECTOR_INIT_CAPACITY
*
sizeof
(
type
));
return
v
;
}
int
vector_lenght
(
vector
*
v
)
{
return
v
->
length
;
}
void
vector_push
(
vector
*
v
,
type
item
)
{
if
(
v
->
length
==
v
->
capacity
)
{
v
->
capacity
*=
2
;
v
->
data
=
realloc
(
v
->
data
,
v
->
capacity
*
sizeof
(
type
));
assert
(
v
->
data
!=
NULL
);
}
v
->
data
[
v
->
length
]
=
item
;
v
->
length
++
;
}
void
vector_pop
(
vector
*
v
,
type
item
)
{
assert
(
v
->
length
>
0
);
if
(
v
->
length
-
1
==
v
->
capacity
/
2
)
{
v
->
capacity
/=
2
;
v
->
data
=
realloc
(
v
->
data
,
v
->
capacity
*
sizeof
(
type
));
assert
(
v
->
data
!=
NULL
);
}
v
->
length
--
;
}
void
vector_set
(
vector
*
v
,
int
index
,
type
element
)
{
assert
(
index
<=
v
->
length
&&
index
>
0
);
v
->
data
[
index
]
=
element
;
}
type
vector_get
(
vector
*
v
,
int
index
)
{
assert
(
index
<=
v
->
length
&&
index
>
0
);
return
v
->
data
[
index
];
}
void
vector_remove
(
vector
*
v
,
int
index
)
{
assert
(
index
<=
v
->
length
&&
index
>
0
);
for
(
int
i
=
index
;
i
<
v
->
length
;
i
++
)
{
v
->
data
[
i
]
=
v
->
data
[
i
+
1
];
}
v
->
length
--
;
}
void
vector_insert
(
vector
*
v
,
type
element
,
int
index
)
{
assert
(
index
<=
v
->
length
&&
index
>
0
);
if
(
v
->
capacity
==
v
->
length
)
{
v
->
capacity
*=
2
;
v
->
data
=
realloc
(
v
->
data
,
v
->
capacity
*
sizeof
(
type
));
assert
(
v
->
data
!=
NULL
);
}
v
->
length
++
;
for
(
size_t
i
=
v
->
length
-
1
;
i
>
index
;
i
--
)
{
v
->
data
[
i
]
=
v
->
data
[
i
-
1
];
}
v
->
data
[
index
]
=
element
;
}
void
vector_empty
(
vector
*
v
)
{
free
(
v
->
data
);
vector
v2
=
vector_create
();
v
=
&
v2
;
}
void
vector_free
(
vector
*
v
)
{
free
(
v
->
data
);
free
(
v
);
}
void
vector_print
(
vector
*
v
,
void
(
*
print
)(
type
))
{
printf
(
"["
);
for
(
int
i
=
0
;
i
<
v
->
length
;
i
++
)
{
print
(
v
->
data
[
i
]);
printf
(
","
);
}
printf
(
"]"
);
}
vector
vector_map
(
vector
*
v
,
type
(
*
f
)(
type
))
{
vector
v2
=
vector_create
();
for
(
int
i
=
0
;
i
<
v
->
length
;
i
++
)
{
vector_push
(
&
v2
,
f
(
v
->
data
[
i
]));
}
return
v2
;
}
vector
vector_filter
(
vector
*
v
,
bool
(
*
f
)(
type
))
{
vector
v2
=
vector_create
();
for
(
int
i
=
0
;
i
<
v
->
length
;
i
++
)
{
if
(
f
(
v
->
data
[
i
]))
{
vector_push
(
&
v2
,
v
->
data
[
i
]);
}
}
return
v2
;
}
type
vector_reduce
(
vector
*
v
,
type
neutral
,
type
(
*
f
)(
type
,
type
))
{
type
result
=
neutral
;
for
(
int
i
=
0
;
i
<
v
->
length
;
i
++
)
{
f
(
neutral
,
v
->
data
[
i
]);
}
return
result
;
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
vector.h
0 → 100644
+
21
−
0
View file @
3a0bd826
#ifndef _VECTOR_H_
#define _VECTOR_H_
typedef
int
type
;
typedef
struct
_vector
vector
;
vector
vector_create
();
int
vector_lenght
(
vector
*
v
);
void
vector_push
(
vector
*
v
,
type
item
);
void
vector_pop
(
vector
*
v
,
type
item
);
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
));
type
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
register
or
sign in
to comment