Ian Jauslin
summaryrefslogtreecommitdiff
blob: fe409c2cf5c3238e45fb7365d0d0637ff9b67e9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
Copyright 2015 Ian Jauslin

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#include "idtable.h"
#include <stdio.h>
#include <stdlib.h>
#include "array.h"
#include "polynomial.h"

// allocate memory
int init_Id_Table(Id_Table* idtable,int size){
  (*idtable).indices=calloc(size,sizeof(int));
  (*idtable).polynomials=calloc(size,sizeof(Polynomial));
  (*idtable).length=0;
  (*idtable).memory=size;
  return(0);
}

// free memory
int free_Id_Table(Id_Table idtable){
  int i;
  for(i=0;i<idtable.length;i++){
    free_Polynomial(idtable.polynomials[i]);
  }
  free(idtable.indices);
  free(idtable.polynomials);

  return(0);
}

// resize the memory allocated to a idtable
int resize_idtable(Id_Table* idtable,int new_size){
  Id_Table new_idtable;
  int i;

  init_Id_Table(&new_idtable,new_size);
  for(i=0;i<(*idtable).length;i++){
    new_idtable.indices[i]=(*idtable).indices[i];
    new_idtable.polynomials[i]=(*idtable).polynomials[i];
  }
  new_idtable.length=(*idtable).length;

  free((*idtable).indices);
  free((*idtable).polynomials);

  *idtable=new_idtable;
  return(0);
}

// copy an idtable
int idtable_cpy(Id_Table input, Id_Table* output){
  init_Id_Table(output,input.length);
  idtable_cpy_noinit(input,output);
  return(0);
}
int idtable_cpy_noinit(Id_Table input, Id_Table* output){
  int i;
  if((*output).memory<input.length){
    fprintf(stderr,"error: trying to copy an idtable of length %d to another with memory %d\n",input.length,(*output).memory);
    exit(-1);
  }
  for(i=0;i<input.length;i++){
    (*output).indices[i]=input.indices[i];
    polynomial_cpy(input.polynomials[i],(*output).polynomials+i);
  }
  (*output).length=input.length;
  
  return(0);
}

// append an element to a idtable
int idtable_append(int index, Polynomial polynomial, Id_Table* output){
  int offset=(*output).length;

  if((*output).length>=(*output).memory){
    resize_idtable(output,2*(*output).memory);
  }

  // copy and allocate
  polynomial_cpy(polynomial,(*output).polynomials+offset);
  (*output).indices[offset]=index;
  // increment length
  (*output).length++;
  return(0);
}
// append an element to a idtable without allocating memory
int idtable_append_noinit(int index, Polynomial polynomial, Id_Table* output){
  int offset=(*output).length;

  if((*output).length>=(*output).memory){
    resize_idtable(output,2*(*output).memory);
  }

  // copy without allocating
  (*output).indices[offset]=index;
  (*output).polynomials[offset]=polynomial;
  // increment length
  (*output).length++;
  return(0);
}

// concatenate two idtables
int idtable_concat(Id_Table input, Id_Table* output){
  int i;
  for(i=0;i<input.length;i++){
    idtable_append(input.indices[i],input.polynomials[i],output);
  }
  return(0);
}