Ian Jauslin
summaryrefslogtreecommitdiff
path: root/src/rcc.c
blob: 484e20a6b1922feb05b55d3b412c6ea929229767 (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
/*
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 "rcc.h"
#include <stdio.h>
#include <stdlib.h>
#include "array.h"

// init
int init_RCC(RCC* rccs, int size){
  (*rccs).values=calloc(size,sizeof(long double));
  (*rccs).indices=calloc(size,sizeof(int));
  (*rccs).length=size;
  return(0);
}

int free_RCC(RCC rccs){
  free(rccs.values);
  free(rccs.indices);
  return(0);
}

// set a given element of an rcc
int RCC_set_elem(long double value, int index, RCC* rcc, int pos){
  (*rcc).values[pos]=value;
  (*rcc).indices[pos]=index;
  return(0);
}

int RCC_cpy(RCC input,RCC* output){
  int i;

  init_RCC(output,input.length);
  for(i=0;i<input.length;i++){
    RCC_set_elem(input.values[i], input.indices[i], output, i);
  }
  return(0);
}

// concatenate rccs
int RCC_concat(RCC rccs1, RCC rccs2, RCC* output){
  int i;

  init_RCC(output,rccs1.length+rccs2.length);

  for(i=0;i<rccs1.length;i++){
    RCC_set_elem(rccs1.values[i], rccs1.indices[i], output, i);
  }

  for(i=0;i<rccs2.length;i++){
    RCC_set_elem(rccs2.values[i], rccs2.indices[i], output, i+rccs1.length);
  }
  
  return(0);
}

// append an rcc at the end of another
int RCC_append(RCC input, RCC* output){
  int i;
  for(i=0;i<input.length;i++){
    RCC_set_elem(input.values[i], input.indices[i], output, i+(*output).length);
  }
  (*output).length+=input.length;
  return(0);
}

// print an rcc vector with maximal precision
int RCC_print(RCC rccs){
  int j;

  for(j=0;j<rccs.length;j++){
    printf("%d:%.19Le",rccs.indices[j],rccs.values[j]);
    if(j<rccs.length-1){
      printf(",\n");
    }
    else{
      printf("\n");
    }
  }

  return(0);
}