<commit_before>// File created: 2011-08-06 15:44:53

#include "space.all.h"

#include <assert.h>
#include <stdbool.h>
#include <string.h>

#include "stdlib.any.h"
#include "space/map-no-place.98.h"
#include "space/place-box.98.h"
#include "space/place-box-for.98.h"

mushspace* mushspace_init(void* vp, mushstats* stats) {
   mushspace *space = vp ? vp : malloc(sizeof *space);
   if (!space)
      return NULL;

   if (stats) {
      space->private_stats = false;
      space->stats = stats;
   } else {
      space->private_stats = true;
      if (!(space->stats = malloc(sizeof *space->stats))) {
         free(space);
         return NULL;
      }
   }

   space->invalidatees = NULL;

   space->box_count = 0;
   space->boxen     = NULL;
   space->bak.data  = NULL;

   // Placate valgrind and such: it's not necessary to define these before the
   // first use.
   space->last_beg = space->last_end = MUSHCOORDS(0,0,0);

   mushmemorybuf_init(&space->recent_buf);

   mushcell_space(
      space->static_box.array, MUSH_ARRAY_LEN(space->static_box.array));
   return space;
}

void mushspace_free(mushspace* space) {
   for (size_t i = space->box_count; i--;)
      free(space->boxen[i].data);
   free(space->boxen);
   mushbakaabb_free(&space->bak);
   if (space->invalidatees) {
      free(space->invalidatees);
      free(space->invalidatees_data);
   }
   if (space->private_stats)
      free(space->stats);
}

mushspace* mushspace_copy(void* vp, const mushspace* space, mushstats* stats) {
   mushspace *copy = vp ? vp : malloc(sizeof *copy);
   if (!copy)
      return NULL;

   // Easiest to do here, so we don't have to worry about whether we need to
   // free copy->stats or not.
   if (space->bak.data && !mushbakaabb_copy(&copy->bak, &space->bak)) {
      free(copy);
      return NULL;
   }

   memcpy(copy, space, sizeof *copy);

   if (stats) {
      copy->private_stats = false;
      copy->stats = stats;
   } else {
      copy->private_stats = true;
      copy->stats = malloc(sizeof *copy->stats);
      if (!copy->stats) {
         free(copy);
         return NULL;
      }
   }

   copy->boxen = malloc(copy->box_count * sizeof *copy->boxen);
   memcpy(copy->boxen, space->boxen, copy->box_count * sizeof *copy->boxen);

   for (size_t i = 0; i < copy->box_count; ++i) {
      mushaabb *box = &copy->boxen[i];
      const mushcell *orig = box->data;
      box->data = malloc(box->size * sizeof *box->data);
      memcpy(box->data, orig, box->size * sizeof *box->data);
   }

   // Invalidatees refer to the original space, not the copy.
   copy->invalidatees = NULL;

   return copy;
}

mushcell mushspace_get(const mushspace* space, mushcoords c) {
   if (mushstaticaabb_contains(c))
      return mushstaticaabb_get(&space->static_box, c);<commit_msg>Zero-initialize privately allocated mushstats<commit_after>// File created: 2011-08-06 15:44:53

#include "space.all.h"

#include <assert.h>
#include <stdbool.h>
#include <string.h>

#include "stdlib.any.h"
#include "space/map-no-place.98.h"
#include "space/place-box.98.h"
#include "space/place-box-for.98.h"

mushspace* mushspace_init(void* vp, mushstats* stats) {
   mushspace *space = vp ? vp : malloc(sizeof *space);
   if (!space)
      return NULL;

   if (stats) {
      space->private_stats = false;
      space->stats = stats;
   } else {
      space->private_stats = true;
      if (!(space->stats = malloc(sizeof *space->stats))) {
         free(space);
         return NULL;
      }
      *space->stats = (mushstats){0};
   }

   space->invalidatees = NULL;

   space->box_count = 0;
   space->boxen     = NULL;
   space->bak.data  = NULL;

   // Placate valgrind and such: it's not necessary to define these before the
   // first use.
   space->last_beg = space->last_end = MUSHCOORDS(0,0,0);

   mushmemorybuf_init(&space->recent_buf);

   mushcell_space(
      space->static_box.array, MUSH_ARRAY_LEN(space->static_box.array));
   return space;
}

void mushspace_free(mushspace* space) {
   for (size_t i = space->box_count; i--;)
      free(space->boxen[i].data);
   free(space->boxen);
   mushbakaabb_free(&space->bak);
   if (space->invalidatees) {
      free(space->invalidatees);
      free(space->invalidatees_data);
   }
   if (space->private_stats)
      free(space->stats);
}

mushspace* mushspace_copy(void* vp, const mushspace* space, mushstats* stats) {
   mushspace *copy = vp ? vp : malloc(sizeof *copy);
   if (!copy)
      return NULL;

   // Easiest to do here, so we don't have to worry about whether we need to
   // free copy->stats or not.
   if (space->bak.data && !mushbakaabb_copy(&copy->bak, &space->bak)) {
      free(copy);
      return NULL;
   }

   memcpy(copy, space, sizeof *copy);

   if (stats) {
      copy->private_stats = false;
      copy->stats = stats;
   } else {
      copy->private_stats = true;
      copy->stats = malloc(sizeof *copy->stats);
      if (!copy->stats) {
         free(copy);
         return NULL;
      }
      *space->stats = (mushstats){0};
   }

   copy->boxen = malloc(copy->box_count * sizeof *copy->boxen);
   memcpy(copy->boxen, space->boxen, copy->box_count * sizeof *copy->boxen);

   for (size_t i = 0; i < copy->box_count; ++i) {
      mushaabb *box = &copy->boxen[i];
      const mushcell *orig = box->data;
      box->data = malloc(box->size * sizeof *box->data);
      memcpy(box->data, orig, box->size * sizeof *box->data);
   }

   // Invalidatees refer to the original space, not the copy.
   copy->invalidatees = NULL;

   return copy;
}

mushcell mushspace_get(const mushspace* space, mushcoords c) {
   if (mushstaticaabb_contains(c))
      return mushstaticaabb_get(&space->static_box, c);
{'fraction_non_alphanumeric': 0.15301800211789623, 'fraction_numerical': 0.009177550300035298, 'mean_word_length': 2.9326856349757113, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 113, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6216782', 'n_tokens_mistral': 2058, 'n_tokens_neox': 1990, 'n_words': 450}