<commit_before>/* Copyright (C) 2008 <NAME>.
   This file is part of the Kate codec library.
   Written by <NAME>.

   Use, distribution and reproduction of this library is governed
   by a BSD style source license included with this source in the
   file 'COPYING'. Please read these terms before distributing. */


#define KATE_INTERNAL
#include "kate_internal.h"

#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#include "kate/kate.h"
#include "kate_encode_state.h"

kate_encode_state *kate_encode_state_create(void)
{
  kate_encode_state *kes=(kate_encode_state*)kate_malloc(sizeof(kate_encode_state));
  if (kes) {
    kate_pack_writeinit(&kes->kpb);

    kes->id=-1;

    kes->granulepos=0;
    kes->packetno=-1;

    kes->nmotions=0;
    kes->motions=NULL;
    kes->destroy_motions=NULL;
    kes->motion_indices=NULL;

    kes->furthest_granule=0;

    kes->eos=0;

    kes->ntimings=0;
    kes->timings=NULL;

    kes->overrides.language=NULL;
    kes->overrides.region=NULL;
    kes->overrides.style=NULL;
    kes->overrides.palette=NULL;
    kes->overrides.bitmap=NULL;
    kes->overrides.secondary_style=NULL;
  }
  return kes;
}

int kate_encode_state_clear_overrides(kate_encode_state *kes,const kate_info *ki)
{
  if (!kes || !ki) return KATE_E_INVALID_PARAMETER;

  if (kes->motions) {
    kate_motion_destroy(ki,kes->motions,kes->destroy_motions,kes->nmotions,0);
    kes->motions=NULL;
  }
  if (kes->destroy_motions) {
    kate_free(kes->destroy_motions);
    kes->destroy_motions=NULL;
  }
  if (kes->motion_indices) {
    kate_free(kes->motion_indices);
    kes->motion_indices=NULL;
  }
  kes->nmotions=0;

  if (kes->overrides.language) {
    kate_free(kes->overrides.language);
    kes->overrides.language=NULL;
  }
  kes->overrides.text_encoding=ki->text_encoding;
  kes->overrides.text_directionality=ki->text_directionality;
  kes->overrides.text_markup_type=ki->text_markup_type;

  kes->overrides.region_index=-1;
  kes->overrides.region=NULL;
  kes->overrides.style_index=-1;
  kes->overrides.style=NULL;
  kes->overrides.secondary_style_index=-1;
  kes->overrides.secondary_style=NULL;

  kes->overrides.font_mapping_index=-1;

  kes->overrides.palette_index=-1;
  kes->overrides.palette=NULL;

  kes->overrides.bitmap_index=-1;
  kes->overrides.bitmap=NULL;

  return 0;
}

static int kate_encode_state_add_motion_or_index(kate_encode_state *kes,kate_motion *km,size_t motion,int destroy)
{
  kate_motion **motions;
  int *destroy_motions;
  size_t *motion_indices;

  if (!kes) return KATE_E_INVALID_PARAMETER;

  motions=(kate_motion**)kate_realloc(kes->motions,(kes->nmotions+1)*sizeof(kate_motion*));
  if (!motions) return KATE_E_OUT_OF_MEMORY;
  kes->motions=motions;

  destroy_motions=(int*)kate_realloc(kes->destroy_motions,(kes->nmotions+1)*sizeof(int));
  if (!destroy_motions) return KATE_E_OUT_OF_MEMORY;
  kes->destroy_motions=destroy_motions;

  motion_indices=(size_t*)kate_realloc(kes->motion_indices,(kes->nmotions+1)*sizeof(size_t));
  if (!motion_indices) return KATE_E_OUT_OF_MEMORY;
  kes->motion_indices=motion_indices;

  kes->motions[kes->nmotions]=km;
  kes->destroy_motions[kes->nmotions]=destroy;
  kes->motion_indices[kes->nmotions]=motion;
  ++kes->nmotions;

  return 0;
}

int kate_encode_state_add_motion(kate_encode_state *kes,kate_motion *km,int destroy)
{
  if (!kes || !km) return KATE_E_INVALID_PARAMETER;
  return kate_encode_state_add_motion_or_index(kes,km,0,destroy);
}

int kate_encode_state_add_motion_index(kate_encode_state *kes,size_t motion)
{
  if (!kes) return KATE_E_INVALID_PARAMETER;
  return kate_encode_state_add_motion_or_index(kes,NULL,motion,0);
}

int kate_encode_state_destroy(kate_encode_state *kes)
{
  if (!kes) return KATE_E_INVALID_PARAMETER;

  kate_pack_writeclear(&kes->kpb);
  if (kes->timings) kate_free(kes->timings);
  if (kes->motions) kate_free(kes->motions);
  if (kes->destroy_motions) kate_free(kes->destroy_motions);
  if (kes->motion_indices) kate_free(kes->motion_indices);
  if (kes->overrides.language) kate_free(kes->overrides.language);
  kate_free(kes);

  return 0;
}

int kate_encode_state_add_event(kate_encode_state *kes,kate_float start,kate_float end)
{
  kate_event_timing *new_timings;

  if (!kes) return KATE_E_INVALID_PARAMETER;
  if (start<0 || end<0 || end<start) return KATE_E_INVALID_PARAMETER;

  new_timings=(kate_event_timing*)kate_realloc(kes->timings,(kes->ntimings+1)*sizeof(kate_event_timing));
  if (!new_timings) return KATE_E_OUT_OF_MEMORY;
  kes->timings=new_timings;

  kes->timings[kes->ntimings].start=start;
  kes->timings[kes->ntimings].end=end;
  ++kes->ntimings;

  return 0;
}

int kate_encode_state_get_earliest_event(kate_encode_state *kes,kate_float *start,kate_float *end)
{
  size_t n;

  if (!kes || !start) return KATE_E_INVALID_PARAMETER; /* end may be NULL */
  if (!kes->ntimings) return KATE_E_NOT_FOUND;

  for (n=0;n<kes->ntimings;++n) {
    if (n==0 || kes->timings[n].start<*start) {
      *start=kes->timings[n].start;
      if (end) *end=kes->timings[n].end;
    }
  }

  return 0;
}

int kate_encode_state_get_latest_event(kate_encode_state *kes,kate_float *start,kate_float *end)
{
  size_t n;

  if (!kes || !end) return KATE_E_INVALID_PARAMETER; /* start may be NULL */
  if (!kes->ntimings) return KATE_E_NOT_FOUND;

  for (n=0;n<kes->ntimings;++n) {
    if (n==0 || kes->timings[n].end>*end) {
      if (start) *start=kes->timings[n].start;
      *end=kes->timings[n].end;
    }
  }

  return 0;
}

int kate_encode_state_trim_events(kate_encode_state *kes,kate_float t)
{
  size_t n;

  if (!kes) return KATE_E_INVALID_PARAMETER;

  for (n=0;n<kes->ntimings;++n) {
    if (kes->timings[n].end<=t) {
      kes->timings[n--]=kes->timings[--kes->ntimings];
    }
  }

  return 0;
}

<commit_msg>reset id when encoding a new event<commit_after>/* Copyright (C) 2008 <NAME>.
   This file is part of the Kate codec library.
   Written by <NAME>.

   Use, distribution and reproduction of this library is governed
   by a BSD style source license included with this source in the
   file 'COPYING'. Please read these terms before distributing. */


#define KATE_INTERNAL
#include "kate_internal.h"

#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#include "kate/kate.h"
#include "kate_encode_state.h"

kate_encode_state *kate_encode_state_create(void)
{
  kate_encode_state *kes=(kate_encode_state*)kate_malloc(sizeof(kate_encode_state));
  if (kes) {
    kate_pack_writeinit(&kes->kpb);

    kes->id=-1;

    kes->granulepos=0;
    kes->packetno=-1;

    kes->nmotions=0;
    kes->motions=NULL;
    kes->destroy_motions=NULL;
    kes->motion_indices=NULL;

    kes->furthest_granule=0;

    kes->eos=0;

    kes->ntimings=0;
    kes->timings=NULL;

    kes->overrides.language=NULL;
    kes->overrides.region=NULL;
    kes->overrides.style=NULL;
    kes->overrides.palette=NULL;
    kes->overrides.bitmap=NULL;
    kes->overrides.secondary_style=NULL;
  }
  return kes;
}

int kate_encode_state_clear_overrides(kate_encode_state *kes,const kate_info *ki)
{
  if (!kes || !ki) return KATE_E_INVALID_PARAMETER;

  if (kes->motions) {
    kate_motion_destroy(ki,kes->motions,kes->destroy_motions,kes->nmotions,0);
    kes->motions=NULL;
  }
  if (kes->destroy_motions) {
    kate_free(kes->destroy_motions);
    kes->destroy_motions=NULL;
  }
  if (kes->motion_indices) {
    kate_free(kes->motion_indices);
    kes->motion_indices=NULL;
  }
  kes->nmotions=0;

  if (kes->overrides.language) {
    kate_free(kes->overrides.language);
    kes->overrides.language=NULL;
  }
  kes->overrides.text_encoding=ki->text_encoding;
  kes->overrides.text_directionality=ki->text_directionality;
  kes->overrides.text_markup_type=ki->text_markup_type;

  kes->id=-1;

  kes->overrides.region_index=-1;
  kes->overrides.region=NULL;
  kes->overrides.style_index=-1;
  kes->overrides.style=NULL;
  kes->overrides.secondary_style_index=-1;
  kes->overrides.secondary_style=NULL;

  kes->overrides.font_mapping_index=-1;

  kes->overrides.palette_index=-1;
  kes->overrides.palette=NULL;

  kes->overrides.bitmap_index=-1;
  kes->overrides.bitmap=NULL;

  return 0;
}

static int kate_encode_state_add_motion_or_index(kate_encode_state *kes,kate_motion *km,size_t motion,int destroy)
{
  kate_motion **motions;
  int *destroy_motions;
  size_t *motion_indices;

  if (!kes) return KATE_E_INVALID_PARAMETER;

  motions=(kate_motion**)kate_realloc(kes->motions,(kes->nmotions+1)*sizeof(kate_motion*));
  if (!motions) return KATE_E_OUT_OF_MEMORY;
  kes->motions=motions;

  destroy_motions=(int*)kate_realloc(kes->destroy_motions,(kes->nmotions+1)*sizeof(int));
  if (!destroy_motions) return KATE_E_OUT_OF_MEMORY;
  kes->destroy_motions=destroy_motions;

  motion_indices=(size_t*)kate_realloc(kes->motion_indices,(kes->nmotions+1)*sizeof(size_t));
  if (!motion_indices) return KATE_E_OUT_OF_MEMORY;
  kes->motion_indices=motion_indices;

  kes->motions[kes->nmotions]=km;
  kes->destroy_motions[kes->nmotions]=destroy;
  kes->motion_indices[kes->nmotions]=motion;
  ++kes->nmotions;

  return 0;
}

int kate_encode_state_add_motion(kate_encode_state *kes,kate_motion *km,int destroy)
{
  if (!kes || !km) return KATE_E_INVALID_PARAMETER;
  return kate_encode_state_add_motion_or_index(kes,km,0,destroy);
}

int kate_encode_state_add_motion_index(kate_encode_state *kes,size_t motion)
{
  if (!kes) return KATE_E_INVALID_PARAMETER;
  return kate_encode_state_add_motion_or_index(kes,NULL,motion,0);
}

int kate_encode_state_destroy(kate_encode_state *kes)
{
  if (!kes) return KATE_E_INVALID_PARAMETER;

  kate_pack_writeclear(&kes->kpb);
  if (kes->timings) kate_free(kes->timings);
  if (kes->motions) kate_free(kes->motions);
  if (kes->destroy_motions) kate_free(kes->destroy_motions);
  if (kes->motion_indices) kate_free(kes->motion_indices);
  if (kes->overrides.language) kate_free(kes->overrides.language);
  kate_free(kes);

  return 0;
}

int kate_encode_state_add_event(kate_encode_state *kes,kate_float start,kate_float end)
{
  kate_event_timing *new_timings;

  if (!kes) return KATE_E_INVALID_PARAMETER;
  if (start<0 || end<0 || end<start) return KATE_E_INVALID_PARAMETER;

  new_timings=(kate_event_timing*)kate_realloc(kes->timings,(kes->ntimings+1)*sizeof(kate_event_timing));
  if (!new_timings) return KATE_E_OUT_OF_MEMORY;
  kes->timings=new_timings;

  kes->timings[kes->ntimings].start=start;
  kes->timings[kes->ntimings].end=end;
  ++kes->ntimings;

  return 0;
}

int kate_encode_state_get_earliest_event(kate_encode_state *kes,kate_float *start,kate_float *end)
{
  size_t n;

  if (!kes || !start) return KATE_E_INVALID_PARAMETER; /* end may be NULL */
  if (!kes->ntimings) return KATE_E_NOT_FOUND;

  for (n=0;n<kes->ntimings;++n) {
    if (n==0 || kes->timings[n].start<*start) {
      *start=kes->timings[n].start;
      if (end) *end=kes->timings[n].end;
    }
  }

  return 0;
}

int kate_encode_state_get_latest_event(kate_encode_state *kes,kate_float *start,kate_float *end)
{
  size_t n;

  if (!kes || !end) return KATE_E_INVALID_PARAMETER; /* start may be NULL */
  if (!kes->ntimings) return KATE_E_NOT_FOUND;

  for (n=0;n<kes->ntimings;++n) {
    if (n==0 || kes->timings[n].end>*end) {
      if (start) *start=kes->timings[n].start;
      *end=kes->timings[n].end;
    }
  }

  return 0;
}

int kate_encode_state_trim_events(kate_encode_state *kes,kate_float t)
{
  size_t n;

  if (!kes) return KATE_E_INVALID_PARAMETER;

  for (n=0;n<kes->ntimings;++n) {
    if (kes->timings[n].end<=t) {
      kes->timings[n--]=kes->timings[--kes->ntimings];
    }
  }

  return 0;
}


{'fraction_non_alphanumeric': 0.14188137644821028, 'fraction_numerical': 0.0068303648625280995, 'mean_word_length': 5.123345685547909, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 212, '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': '7213600', 'n_tokens_mistral': 4777, 'n_tokens_neox': 4597, 'n_words': 655}