FrontISTR 5.2.0
Large-scale structural analysis program with finit element method
Loading...
Searching...
No Matches
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
45size_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
54int 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
60int 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
96int 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
128int 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}
#define HECMW_ERROR
Definition: hecmw_config.h:66
#define HECMW_SUCCESS
Definition: hecmw_config.h:64
#define NULL
#define HECMW_free(ptr)
Definition: hecmw_malloc.h:24
#define HECMW_malloc(size)
Definition: hecmw_malloc.h:20
int HECMW_set_int_iter_next(struct hecmw_set_int *set, int *value)
void HECMW_set_int_finalize(struct hecmw_set_int *set)
Definition: hecmw_set_int.c:36
int HECMW_set_int_add(struct hecmw_set_int *set, int value)
Definition: hecmw_set_int.c:60
int HECMW_set_int_init(struct hecmw_set_int *set)
Definition: hecmw_set_int.c:16
void HECMW_set_int_iter_init(struct hecmw_set_int *set)
int HECMW_set_int_del(struct hecmw_set_int *set, int value)
Definition: hecmw_set_int.c:96
size_t HECMW_set_int_check_dup(struct hecmw_set_int *set)
Definition: hecmw_set_int.c:78
int HECMW_set_int_is_empty(const struct hecmw_set_int *set)
Definition: hecmw_set_int.c:54
size_t HECMW_set_int_nval(struct hecmw_set_int *set)
Definition: hecmw_set_int.c:45
#define HECMW_assert(cond)
Definition: hecmw_util.h:40
size_t HECMW_varray_int_nval(const struct hecmw_varray_int *varray)
size_t HECMW_varray_int_uniq(struct hecmw_varray_int *varray)
int HECMW_varray_int_search(struct hecmw_varray_int *varray, int value, size_t *index)
void HECMW_varray_int_sort(struct hecmw_varray_int *varray)
int HECMW_varray_int_get(const struct hecmw_varray_int *varray, size_t index)
int HECMW_varray_int_init(struct hecmw_varray_int *varray)
void HECMW_varray_int_finalize(struct hecmw_varray_int *varray)
int HECMW_varray_int_append(struct hecmw_varray_int *varray, int value)
int HECMW_varray_int_delete(struct hecmw_varray_int *varray, size_t index)
struct hecmw_varray_int * vals
Definition: hecmw_set_int.h:12