File: intarray.hpp

package info (click to toggle)
macaulay2 1.21%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 133,096 kB
  • sloc: cpp: 110,377; ansic: 16,306; javascript: 4,193; makefile: 3,821; sh: 3,580; lisp: 764; yacc: 590; xml: 177; python: 140; perl: 114; lex: 65; awk: 3
file content (140 lines) | stat: -rw-r--r-- 2,931 bytes parent folder | download | duplicates (2)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// (c) 1995  Michael E. Stillman
#ifndef _intarray_hh_
#define _intarray_hh_

#include "style.hpp"
#include "buffer.hpp"
#include "mem.hpp"
#include <cassert>

const int init_intarray_size = 16;

class intarray
{
  int *entries;
  int len;  // Allocated length
  int max;  // Current length
 public:
  intarray() : entries(NULL), len(0), max(0) {}
  intarray(int i_size) : max(0)
  {
    int n = init_intarray_size;
    if (i_size > n) n = i_size;
    entries = reinterpret_cast<int *>(doubles->new_elem(sizeof(int) * n));
    len = static_cast<int>(doubles->allocated_size(entries) / sizeof(int));
  }

  intarray(const intarray &a) : max(a.max)
  {
    if (a.len == 0)
      {
        entries = NULL;
        len = 0;
      }
    else
      {
        entries =
            reinterpret_cast<int *>(doubles->new_elem(sizeof(int) * a.max));
        len = static_cast<int>(doubles->allocated_size(entries) / sizeof(int));
        for (int i = 0; i < max; i++) entries[i] = a.entries[i];
      }
  }

  ~intarray()
  {
    doubles->delete_elem(entries);
    entries = 0;
  }

  void remove()
  {
    doubles->delete_elem(entries);
    entries = 0;
  }

  void expand(int newtop);

  int length() const { return max; }
  void shrink(int newmax)
  {
    if (newmax < max) max = newmax;
  }

  int operator[](int i) const
  {
    assert(i < max);
    return entries[i];
  }

  int &operator[](int i)
  {
    assert(i < max);
    return entries[i];
  }

  int *raw()
  {
    assert(entries != NULL);
    return entries;
  }
  const int *raw() const
  {
    assert(entries != NULL);
    return entries;
  }

  void append(int t)
  {
    if (max == len) expand(max);
    entries[max++] = t;
  }

  int *alloc(int extra)
  // make room for 'extra' elements, return pointer to first,
  // and set 'max' to be max+extra.
  {
    if (len <= max + extra) expand(len + extra);
    int *result = entries + max;
    max += extra;
    return result;
  }

  int *copy(int lngth, const int *a)
  {
    int *t = alloc(lngth);
    int *result = t;
    for (int i = 0; i < lngth; i++) *t++ = *a++;
    return result;
  }

  intarray &operator=(const intarray &a)
  {
    if (&a == this) return *this;
    doubles->delete_elem(entries);
    int n = init_intarray_size;
    if (a.max > n) n = a.max;
    max = a.max;
    entries = reinterpret_cast<int *>(doubles->new_elem(sizeof(int) * n));
    len = static_cast<int>(doubles->allocated_size(entries) / sizeof(int));
    for (int i = 0; i < max; i++) entries[i] = a.entries[i];
    return *this;
  }

  int operator==(const intarray &a) const
  {
    if (max != a.max) return 0;
    for (int i = 0; i < max; i++)
      if (entries[i] != a.entries[i]) return 0;
    return 1;
  }

  int operator!=(const intarray &a) const { return !(operator==(a)); }
  void text_out(buffer &o) const;
};

#endif

// Local Variables:
// compile-command: "make -C $M2BUILDDIR/Macaulay2/e "
// indent-tabs-mode: nil
// End: