Macaulay2 Engine
Loading...
Searching...
No Matches
buffer.cpp
Go to the documentation of this file.
1#include "buffer.hpp"
2#include "ringelem.hpp"
3#include <cstdio>
4#include <cstring>
5#include <cassert>
6#include <mpfr.h>
7#include <mpfi.h>
8
9void buffer::expand(int newcap)
10{
11 int n = 2 * _capacity;
12 if (newcap > n) n = newcap;
13 char *newbuf = newarray_atomic(char, n);
14 _capacity = n;
15 memcpy(newbuf, _buf, _size);
17 _buf = newbuf;
18}
19
20M2_string buffer::to_string() { return M2_tostringn(_buf, _size); }
21void buffer::put(char c)
22{
23 if (_capacity <= _size + 1) expand(1);
24 _buf[_size++] = c;
25}
26
27void buffer::put(const char *s, long len)
28{
29 int len0 = static_cast<int>(len);
30 if (_capacity <= _size + len0 + 1) expand(_size + len0 + 1);
31 memcpy(_buf + _size, s, len0);
32 _size += len0;
33}
34
35void buffer::put(const char *s) { put(s, strlen(s)); }
36void buffer::put(int n)
37{
38 const int N = 100;
39 char s[N];
40 snprintf(s, N, "%d", n);
41 put(s, strlen(s));
42}
43
44void buffer::put(int n, int width)
45{
46 const int N = 100;
47 char s[N];
48 snprintf(s, N, "%*d", width, n);
49 put(s, strlen(s));
50}
51
52void buffer::put(long n)
53{
54 const int N = 100;
55 char s[N];
56 snprintf(s, N, "%ld", n);
57 put(s, strlen(s));
58}
59
60void buffer::put(double n)
61{
62 const int N = 100;
63 char s[N];
64 snprintf(s, N, "%g", n);
65 put(s, strlen(s));
66}
67
68void buffer::put(long n, int width)
69{
70 const int N = 100;
71 char s[N];
72 snprintf(s, N, "%*ld", width, n);
73 put(s, strlen(s));
74}
75
76void buffer::put(unsigned int n)
77{
78 const int N = 100;
79 char s[N];
80 snprintf(s, N, "%u", n);
81 put(s, strlen(s));
82}
83
84void buffer::put(unsigned long n)
85{
86 const int N = 100;
87 char s[N];
88 snprintf(s, N, "%lu", n);
89 put(s, strlen(s));
90}
91
92void buffer::put(unsigned long long n)
93{
94 const int N = 100;
95 char s[N];
96 snprintf(s, N, "%llu", n);
97 put(s, strlen(s));
98}
99
100void buffer::put(unsigned int n, int width)
101{
102 const int N = 100;
103 char s[N];
104 snprintf(s, N, "%*u", width, n);
105 put(s, strlen(s));
106}
107
108void buffer::put(unsigned long n, int width)
109{
110 const int N = 100;
111 char s[N];
112 snprintf(s, N, "%*lu", width, n);
113 put(s, strlen(s));
114}
115
116void buffer::put(mpfr_srcptr x)
117{
118 int n;
119 std::string s;
120
121 n = mpfr_snprintf(nullptr, 0, "%Rg", x);
122 s.resize(n);
123 mpfr_snprintf(s.data(), s.size() + 1, "%Rg", x);
124 put(s);
125}
126
127void buffer::put(mpfi_srcptr x)
128{
129 put('[');
130 put(&x->left);
131 put(',');
132 put(&x->right);
133 put(']');
134}
135
137{
138 if (x->re !=0 || (x->re == 0 && x->im == 0)) {
139 put(x->re);
140 if (x->im > 0)
141 put('+');
142 }
143
144 if (x->im != 0) {
145 if (x->im == -1)
146 put('-');
147 else if (x->im != 1)
148 put(x->im);
149 put('i');
150 }
151
152}
153
155{
156 if (mpfr_cmp_si(&x->re, 0) !=0 ||
157 (mpfr_cmp_si(&x->re, 0) == 0 && mpfr_cmp_si(&x->im, 0) == 0)) {
158 put(&x->re);
159 if (mpfr_cmp_si(&x->im, 0) > 0)
160 put('+');
161 }
162
163 if (mpfr_cmp_si(&x->im, 0) != 0) {
164 if (mpfr_cmp_si(&x->im, -1) == 0)
165 put('-');
166 else if (mpfr_cmp_si(&x->im, 1) != 0)
167 put(&x->im);
168 put('i');
169 }
170}
171
173{
174 if (!mpfi_is_zero(&x->re) ||
175 mpfi_is_zero(&x->re) && mpfi_is_zero(&x->im)) {
176 put(&x->re);
177 if (!mpfi_is_zero(&x->im))
178 put('+');
179 }
180
181 if (!mpfi_is_zero(&x->im)) {
182 put(&x->im);
183 put('i');
184 }
185}
186
187// Local Variables:
188// compile-command: "make -C $M2BUILDDIR/Macaulay2/e "
189// indent-tabs-mode: nil
190// End:
Append-only GC-backed byte buffer used throughout the engine for text output.
int _capacity
Definition buffer.hpp:57
int _size
Definition buffer.hpp:56
void put(const char *s)
Definition buffer.cpp:35
void expand(int newcap)
Definition buffer.cpp:9
M2_string to_string()
Definition buffer.cpp:20
char * _buf
Definition buffer.hpp:58
void freemem(void *s)
Definition m2-mem.cpp:103
void size_t s
Definition m2-mem.cpp:271
M2_string M2_tostringn(char *s, int n)
Definition m2-types.cpp:40
#define newarray_atomic(T, len)
Definition newdelete.hpp:91
volatile int x
cc_struct const * cc_srcptr
Definition ringelem.hpp:61
cc_doubles_struct const * cc_doubles_srcptr
Definition ringelem.hpp:68
cci_struct const * cci_srcptr
Definition ringelem.hpp:77
ring_elem — the universal value type carried by every Ring* in the engine.