FrontISTR  5.7.0
Large-scale structural analysis program with finit element method
hecmw_set_int.c
Go to the documentation of this file.
1 /*****************************************************************************
2  * Copyright (c) 2019 FrontISTR Commons
3  * This software is released under the MIT License, see LICENSE.txt
4  *****************************************************************************/
5 
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <errno.h>
10 #include "hecmw_util.h"
11 #include "hecmw_malloc.h"
12 #include "hecmw_config.h"
13 #include "hecmw_varray_int.h"
14 #include "hecmw_set_int.h"
15 
17  HECMW_assert(set);
18 
19  set->vals =
20  (struct hecmw_varray_int *)HECMW_malloc(sizeof(struct hecmw_varray_int));
21  if (set->vals == NULL) {
22  return HECMW_ERROR;
23  }
24 
26 
27  set->checked = 1;
28  set->sorted = 1;
29 
30  set->in_iter = 0;
31  set->iter = 0;
32 
33  return HECMW_SUCCESS;
34 }
35 
37  HECMW_assert(set);
38 
40  HECMW_free(set->vals);
41 
42  return;
43 }
44 
45 size_t HECMW_set_int_nval(struct hecmw_set_int *set) {
46  HECMW_assert(set);
47 
48  if (!set->checked) {
50  }
51  return HECMW_varray_int_nval(set->vals);
52 }
53 
54 int HECMW_set_int_is_empty(const struct hecmw_set_int *set) {
55  HECMW_assert(set);
56 
57  return HECMW_varray_int_nval(set->vals) == 0 ? 1 : 0;
58 }
59 
60 int HECMW_set_int_add(struct hecmw_set_int *set, int value) {
61  HECMW_assert(set);
62 
63  size_t nval = HECMW_varray_int_nval(set->vals);
64  if (nval > 0 && set->sorted) {
65  int val_prev = HECMW_varray_int_get(set->vals, nval - 1);
66 
67  if (val_prev > value) set->sorted = set->checked = 0;
68 
69  if (set->checked && val_prev == value) set->checked = 0;
70  }
71 
72  if (HECMW_varray_int_append(set->vals, value) != HECMW_SUCCESS)
73  return HECMW_ERROR;
74 
75  return HECMW_SUCCESS;
76 }
77 
79  size_t i, n_dup = 0;
80 
81  HECMW_assert(set);
82 
83  if (set->checked) return 0;
84 
85  if (!set->sorted) {
87  set->sorted = 1;
88  }
89 
90  n_dup = HECMW_varray_int_uniq(set->vals);
91  set->checked = 1;
92 
93  return n_dup;
94 }
95 
96 int HECMW_set_int_del(struct hecmw_set_int *set, int value) {
97  size_t index;
98 
99  HECMW_assert(set);
100 
101  if (!set->checked) {
102  HECMW_assert(!set->in_iter);
104  }
105 
106  if (HECMW_varray_int_search(set->vals, value, &index) != HECMW_SUCCESS)
107  return HECMW_ERROR;
108 
109  HECMW_varray_int_delete(set->vals, index);
110 
111  if (index < set->iter) set->iter--;
112 
113  return HECMW_SUCCESS;
114 }
115 
117  HECMW_assert(set);
118 
119  if (!set->checked) {
121  }
122 
123  set->in_iter = 1;
124  set->iter = 0;
125  return;
126 }
127 
128 int HECMW_set_int_iter_next(struct hecmw_set_int *set, int *value) {
129  size_t nval = HECMW_varray_int_nval(set->vals);
130  HECMW_assert(set);
131  HECMW_assert(set->in_iter);
132  HECMW_assert(set->iter <= nval);
133 
134  if (set->iter == nval) {
135  set->in_iter = 0;
136  set->iter = 0;
137  return 0;
138  }
139 
140  *value = HECMW_varray_int_get(set->vals, set->iter);
141  set->iter++;
142 
143  return 1;
144 }
hecmw_set_int::in_iter
int in_iter
Definition: hecmw_set_int.h:17
hecmw_set_int::vals
struct hecmw_varray_int * vals
Definition: hecmw_set_int.h:12
hecmw_malloc.h
HECMW_set_int_finalize
void HECMW_set_int_finalize(struct hecmw_set_int *set)
Definition: hecmw_set_int.c:36
HECMW_set_int_is_empty
int HECMW_set_int_is_empty(const struct hecmw_set_int *set)
Definition: hecmw_set_int.c:54
HECMW_varray_int_search
int HECMW_varray_int_search(struct hecmw_varray_int *varray, int value, size_t *index)
Definition: hecmw_varray_int.c:141
HECMW_set_int_iter_init
void HECMW_set_int_iter_init(struct hecmw_set_int *set)
Definition: hecmw_set_int.c:116
HECMW_set_int_del
int HECMW_set_int_del(struct hecmw_set_int *set, int value)
Definition: hecmw_set_int.c:96
HECMW_malloc
#define HECMW_malloc(size)
Definition: hecmw_malloc.h:20
HECMW_varray_int_delete
int HECMW_varray_int_delete(struct hecmw_varray_int *varray, size_t index)
Definition: hecmw_varray_int.c:287
hecmw_set_int.h
HECMW_varray_int_sort
void HECMW_varray_int_sort(struct hecmw_varray_int *varray)
Definition: hecmw_varray_int.c:136
hecmw_set_int::sorted
int sorted
Definition: hecmw_set_int.h:15
HECMW_set_int_add
int HECMW_set_int_add(struct hecmw_set_int *set, int value)
Definition: hecmw_set_int.c:60
HECMW_set_int_check_dup
size_t HECMW_set_int_check_dup(struct hecmw_set_int *set)
Definition: hecmw_set_int.c:78
HECMW_varray_int_get
int HECMW_varray_int_get(const struct hecmw_varray_int *varray, size_t index)
Definition: hecmw_varray_int.c:101
hecmw_varray_int
varray int
Definition: hecmw_varray_int_f.f90:7
HECMW_set_int_nval
size_t HECMW_set_int_nval(struct hecmw_set_int *set)
Definition: hecmw_set_int.c:45
hecmw_varray_int.h
HECMW_set_int_init
int HECMW_set_int_init(struct hecmw_set_int *set)
Definition: hecmw_set_int.c:16
hecmw_config.h
hecmw_set_int
Definition: hecmw_set_int.h:11
hecmw_set_int::iter
size_t iter
Definition: hecmw_set_int.h:18
HECMW_ERROR
#define HECMW_ERROR
Definition: hecmw_config.h:66
HECMW_varray_int_finalize
void HECMW_varray_int_finalize(struct hecmw_varray_int *varray)
Definition: hecmw_varray_int.c:29
HECMW_varray_int_init
int HECMW_varray_int_init(struct hecmw_varray_int *varray)
Definition: hecmw_varray_int.c:18
HECMW_set_int_iter_next
int HECMW_set_int_iter_next(struct hecmw_set_int *set, int *value)
Definition: hecmw_set_int.c:128
hecmw_set_int::checked
int checked
Definition: hecmw_set_int.h:14
HECMW_SUCCESS
#define HECMW_SUCCESS
Definition: hecmw_config.h:64
HECMW_varray_int_append
int HECMW_varray_int_append(struct hecmw_varray_int *varray, int value)
Definition: hecmw_varray_int.c:89
HECMW_varray_int_nval
size_t HECMW_varray_int_nval(const struct hecmw_varray_int *varray)
Definition: hecmw_varray_int.c:41
NULL
#define NULL
Definition: hecmw_io_nastran.c:30
HECMW_free
#define HECMW_free(ptr)
Definition: hecmw_malloc.h:24
HECMW_assert
#define HECMW_assert(cond)
Definition: hecmw_util.h:40
HECMW_varray_int_uniq
size_t HECMW_varray_int_uniq(struct hecmw_varray_int *varray)
Definition: hecmw_varray_int.c:150
hecmw_util.h