/ src / chunk.c
chunk.c
  1  #ifndef FLASHLIGHT_CHUNK
  2  #define FLASHLIGHT_CHUNK
  3  #include "chunk.h"
  4  
  5  int f_chunk_new(f_chunk** out, unsigned long int current, f_bytes_node* firstref, f_bytes_node* lastref)
  6  {
  7    f_chunk* init = malloc(sizeof(*init));
  8    if (init == NULL)
  9    {
 10      return -1;
 11    }
 12  
 13    init->current = current;
 14    init->first = firstref;
 15    init->last = lastref;
 16    init->line_count = 0;
 17    if (firstref != NULL)
 18    {
 19      init->empty = false;
 20    }
 21    else
 22    {
 23      init->empty = true;
 24    }
 25  
 26    *out = init;
 27    return 0;
 28  }
 29  
 30  void f_chunk_free(f_chunk** chunk)
 31  {
 32    f_chunk* head = *chunk;
 33    free(head);
 34    *chunk = NULL;
 35  }
 36  
 37  /* 
 38    free's a chunk and all of its children.
 39    you probably don't want to call this,
 40    since a chunk is an aggregation of pointers to pointers.
 41  */
 42  void f_chunk_free_all(f_chunk* chunk)
 43  {
 44    f_bytes_node_free(&chunk->first);
 45    f_chunk_free(&chunk);
 46  }
 47  
 48  void f_chunks_swap(f_bytes_node** last, f_bytes_node** child)
 49  {
 50    (*last)->next = *child;
 51  }
 52  
 53  int f_chunk_array_reverse_reduce(f_chunk** out, int idx, f_chunk** chunks, size_t len)
 54  { 
 55    f_chunk* result = malloc(sizeof(f_chunk));
 56  
 57    if (result == NULL)
 58    {
 59      return -1;
 60    }
 61  
 62    if (len == 1)
 63    {
 64      *result = *chunks[0];
 65      (result)->current = idx;
 66      *out = result;
 67      return 0;
 68    }
 69  
 70    // clone first chunks last.
 71    (result)->last = NULL;
 72    (result)->first = NULL;
 73  
 74    f_bytes_node* current = NULL;
 75  
 76    int i = 0;
 77    bool head = true;
 78    bool empty = true;
 79  
 80    while (i < len)
 81    {
 82      if (chunks[i]->empty)
 83      {
 84        i++;
 85        continue;
 86      }
 87  
 88      if (current == NULL)
 89      {
 90        current = chunks[i]->first;
 91  
 92        if (head)
 93        {
 94          result->last = chunks[i]->last;
 95          head = false;
 96        }
 97      }
 98      else
 99      {
100        f_bytes_node* last = chunks[i]->last;
101        f_bytes_node* parent = chunks[i]->first;
102  
103        f_chunks_swap(&last, &current);
104        result->first = parent;
105        current = parent;
106      }
107      empty = false;
108      i++;
109    }
110  
111    result->empty = empty;
112    result->current = idx;
113    *out = result;
114    return 0;
115  }
116  
117  int f_chunk_array_new(f_chunk*** out, unsigned long len)
118  {
119    f_chunk** arr = malloc(sizeof(*arr) * len);
120  
121    if (arr == NULL)
122    {
123      return -1;
124    }
125  
126    *out = arr;
127    return 0;
128  }
129  
130  void f_chunk_array_free(f_chunk** chunks, size_t len)
131  {
132    for (int i=0; i<len; i++)
133    {
134      free(chunks[i]);
135    }
136    free(chunks);
137  }
138  
139  void f_chunk_array_free_all(f_chunk** chunks, size_t len)
140  {
141    f_chunk_free_all(chunks[0]);
142    free(chunks);
143  }
144  
145  #endif