Bläddra i källkod

Fix a parenthesis mistake in memmove len arg when inserting or deleting in a Map

master
mtardy 4 år sedan
förälder
incheckning
5962b6ce40
1 ändrade filer med 3 tillägg och 7 borttagningar
  1. +3
    -7
      object.c

+ 3
- 7
object.c Visa fil

@ -194,14 +194,13 @@ oop map_get(oop map, oop key)
return get(map, Map, elements)[pos].value; return get(map, Map, elements)[pos].value;
} }
#define MAP_CHUNK_SIZE 8
#define MAP_CHUNK_SIZE 4
oop map_insert(oop map, oop key, oop value, size_t pos) oop map_insert(oop map, oop key, oop value, size_t pos)
{ {
assert(is(Map, map)); assert(is(Map, map));
assert(key); assert(key);
assert(value); assert(value);
printf("inserting in map, size:%zu, capacity:%zu\n", get(map, Map, size), get(map, Map, capacity));
if (pos > get(map, Map, size)) { // don't need to check for pos < 0 because size_t is unsigned if (pos > get(map, Map, size)) { // don't need to check for pos < 0 because size_t is unsigned
fprintf(stderr, "\nTrying to insert in a map out of bound\n"); fprintf(stderr, "\nTrying to insert in a map out of bound\n");
assert(-1); assert(-1);
@ -209,16 +208,13 @@ oop map_insert(oop map, oop key, oop value, size_t pos)
// check capacity and expand if needed // check capacity and expand if needed
if (get(map, Map, size) >= get(map, Map, capacity)) { if (get(map, Map, size) >= get(map, Map, capacity)) {
printf("expanding, size:%zu, capacity:%zu\n", get(map, Map, size), get(map, Map, capacity));
size_t newCapacity = get(map, Map, capacity) + MAP_CHUNK_SIZE; size_t newCapacity = get(map, Map, capacity) + MAP_CHUNK_SIZE;
set(map, Map, elements, memcheck(realloc(get(map, Map, elements), sizeof(struct Pair) * newCapacity))); set(map, Map, elements, memcheck(realloc(get(map, Map, elements), sizeof(struct Pair) * newCapacity)));
set(map, Map, capacity, newCapacity); set(map, Map, capacity, newCapacity);
printf("expanded, size:%zu, capacity:%zu\n", get(map, Map, size), get(map, Map, capacity));
} }
// insert // insert
printf("before memmove\n");
memmove(get(map, Map, elements) + pos + 1, get(map, Map, elements) + pos, sizeof(struct Pair) * get(map, Map, size) - pos);
memmove(get(map, Map, elements) + pos + 1, get(map, Map, elements) + pos, sizeof(struct Pair) * (get(map, Map, size) - pos));
// Maybe this syntax is not very nice and I should access the Pair stuff differently? // Maybe this syntax is not very nice and I should access the Pair stuff differently?
// I mean modifying something on a line that begin with "get"... :/ // I mean modifying something on a line that begin with "get"... :/
get(map, Map, elements)[pos].value = value; get(map, Map, elements)[pos].value = value;
@ -250,7 +246,7 @@ oop map_del(oop map, oop key)
ssize_t pos = map_search(map, key); ssize_t pos = map_search(map, key);
if (pos < 0) return map; if (pos < 0) return map;
if (pos < get(map, Map, size) - 1) { if (pos < get(map, Map, size) - 1) {
memmove(get(map, Map, elements) + pos, get(map, Map, elements) + pos + 1, sizeof(struct Pair) * get(map, Map, size) - pos);
memmove(get(map, Map, elements) + pos, get(map, Map, elements) + pos + 1, sizeof(struct Pair) * (get(map, Map, size) - pos));
} }
set(map, Map, size, --get(map, Map, size)); set(map, Map, size, --get(map, Map, size));
return map; return map;

Laddar…
Avbryt
Spara