Mentions légales du service

Skip to content
Snippets Groups Projects

Main

Merged SZCZEPANSKI Marin requested to merge main into fullgame
105 files
+ 5115
30
Compare changes
  • Side-by-side
  • Inline
Files
105
+ 94
0
#include "array_seq.h"
sequence* create_sequence (void)
{
/* BUGMULTILINE: not creating the malloc, returning local address */
sequence* seq = (sequence*)malloc(sizeof(sequence));
seq->length = 0;
return seq;
/* BUGALTERNATE
* sequence seq;
* seq.length = 0;
* return &seq;
*/
}
void seq_pp (sequence* seq)
{
printf ("< ");
for (int i=0; i < seq->length-1; i++) {
printf ("%d, ", seq->arr[i]);
}
if (seq->length > 0) {
/* BUGLINEDELETEMORE: forgetting to check there is at least one element */
printf ("%d ", seq->arr[seq->length-1]);
}
/* BUGALSOREMOVE */
printf (">\n");
}
void seq_add_first (sequence* seq, item i)
{
assert (seq->length < LMAX);
for (int i=seq->length; i>0; i--) {
/* BUGLINE: doing the moves in the wrong ordering
* for (int i=0; i<seq->length; i++) {
*/
seq->arr[i] = seq->arr[i-1];
}
seq->arr[0] = i;
seq->length++;
/* BUGLINEDELETE: forgetting to increase length of sequence */
}
void seq_add_last (sequence* seq, item i)
{
assert (seq->length < LMAX);
seq->arr[seq->length] = i;
seq->length++;
}
void seq_add (sequence* seq, item i)
{
seq_add_last (seq, i);
}
void seq_remove_idx (sequence* seq, int idx)
{
assert (idx >= 0 && idx < seq->length);
for (int i=idx; i<seq->length-1; i++) {
/* BUGLINE: going to far in the list, but bug will probably be silent
* for (int i=idx; i<seq->length; i++) {
*/
seq->arr[i] = seq->arr[i+1];
}
seq->length--;
}
int seq_search (sequence* seq, item i)
{
/* search the item */
int idx=0;
for (; idx<seq->length; idx++) {
if (seq->arr[idx] == i) return idx;
}
return -1;
}
void seq_remove (sequence* seq, item i)
{
int idx = seq_search (seq, i);
if (idx != -1) {
/* BUGLINEDELETEMORE: forgetting to check if item belongs to sequence */
seq_remove_idx (seq, idx);
}
/* BUGALSOREMOVE */
}
bool seq_contains (sequence* seq, item i)
{
return seq_search (seq, i) != -1;
}
Loading