Skip to content

Commit

Permalink
Add const for second variable
Browse files Browse the repository at this point in the history
The second variable is used as a source for copy and it is not modified
therefore it should be marked as s const. Then it is more easy to identify what
is source and what is target and also it is clear that the source variable will
be not modify by the function.

Additionally it allows to use const map or queue to create a copy or other
operations.
  • Loading branch information
j-mracek committed May 15, 2018
1 parent f0ef172 commit 4b88c70
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 27 deletions.
20 changes: 10 additions & 10 deletions src/bitmap.c
Expand Up @@ -32,18 +32,18 @@ map_free(Map *m)
m->size = 0;
}

/* copy constructor t <- s */
/* copy constructor target <- source */
void
map_init_clone(Map *t, Map *s)
map_init_clone(Map *target, const Map *source)
{
t->size = s->size;
if (s->size)
target->size = source->size;
if (source->size)
{
t->map = solv_malloc(s->size);
memcpy(t->map, s->map, s->size);
target->map = solv_malloc(source->size);
memcpy(target->map, source->map, source->size);
}
else
t->map = 0;
target->map = 0;
}

/* grow a map */
Expand All @@ -61,7 +61,7 @@ map_grow(Map *m, int n)

/* bitwise-ands maps t and s, stores the result in t. */
void
map_and(Map *t, Map *s)
map_and(Map *t, const Map *s)
{
unsigned char *ti, *si, *end;
ti = t->map;
Expand All @@ -73,7 +73,7 @@ map_and(Map *t, Map *s)

/* bitwise-ors maps t and s, stores the result in t. */
void
map_or(Map *t, Map *s)
map_or(Map *t, const Map *s)
{
unsigned char *ti, *si, *end;
if (t->size < s->size)
Expand All @@ -87,7 +87,7 @@ map_or(Map *t, Map *s)

/* remove all set bits in s from t. */
void
map_subtract(Map *t, Map *s)
map_subtract(Map *t, const Map *s)
{
unsigned char *ti, *si, *end;
ti = t->map;
Expand Down
8 changes: 4 additions & 4 deletions src/bitmap.h
Expand Up @@ -37,12 +37,12 @@ typedef struct _Map {
#define MAPCLR_AT(m, n) ((m)->map[(n) >> 3] = 0)

extern void map_init(Map *m, int n);
extern void map_init_clone(Map *t, Map *s);
extern void map_init_clone(Map *target, const Map *source);
extern void map_grow(Map *m, int n);
extern void map_free(Map *m);
extern void map_and(Map *t, Map *s);
extern void map_or(Map *t, Map *s);
extern void map_subtract(Map *t, Map *s);
extern void map_and(Map *t, const Map *s);
extern void map_or(Map *t, const Map *s);
extern void map_subtract(Map *t, const Map *s);
extern void map_invertall(Map *m);

static inline void map_empty(Map *m)
Expand Down
22 changes: 11 additions & 11 deletions src/queue.c
Expand Up @@ -36,21 +36,21 @@ queue_init(Queue *q)
}

void
queue_init_clone(Queue *t, Queue *s)
queue_init_clone(Queue *target, const Queue *source)
{
int extra_space;
if (!s->elements)
if (!source->elements)
{
t->alloc = t->elements = 0;
t->count = t->left = 0;
target->alloc = target->elements = 0;
target->count = target->left = 0;
return;
}
extra_space = queue_extra_space(s->count);
t->alloc = t->elements = solv_malloc2(s->count + extra_space, sizeof(Id));
if (s->count)
memcpy(t->alloc, s->elements, s->count * sizeof(Id));
t->count = s->count;
t->left = extra_space;
extra_space = queue_extra_space(source->count);
target->alloc = target->elements = solv_malloc2(source->count + extra_space, sizeof(Id));
if (source->count)
memcpy(target->alloc, source->elements, source->count * sizeof(Id));
target->count = source->count;
target->left = extra_space;
}

void
Expand Down Expand Up @@ -168,7 +168,7 @@ queue_delete2(Queue *q, int pos)
}

void
queue_insertn(Queue *q, int pos, int n, Id *elements)
queue_insertn(Queue *q, int pos, int n, const Id *elements)
{
if (n <= 0)
return;
Expand Down
4 changes: 2 additions & 2 deletions src/queue.h
Expand Up @@ -109,12 +109,12 @@ queue_truncate(Queue *q, int n)

extern void queue_init(Queue *q);
extern void queue_init_buffer(Queue *q, Id *buf, int size);
extern void queue_init_clone(Queue *t, Queue *s);
extern void queue_init_clone(Queue *target, const Queue *source);
extern void queue_free(Queue *q);

extern void queue_insert(Queue *q, int pos, Id id);
extern void queue_insert2(Queue *q, int pos, Id id1, Id id2);
extern void queue_insertn(Queue *q, int pos, int n, Id *elements);
extern void queue_insertn(Queue *q, int pos, int n, const Id *elements);
extern void queue_delete(Queue *q, int pos);
extern void queue_delete2(Queue *q, int pos);
extern void queue_deleten(Queue *q, int pos, int n);
Expand Down

0 comments on commit 4b88c70

Please sign in to comment.