Skip to content

Commit

Permalink
First cut at moving the code over to C++11.
Browse files Browse the repository at this point in the history
  • Loading branch information
cseed committed Jul 27, 2012
1 parent 50a2438 commit fdb87ca
Show file tree
Hide file tree
Showing 36 changed files with 955 additions and 459 deletions.
9 changes: 5 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
BISON = /opt/local/bin/bison
FLEX = /opt/local/bin/flex

CXX = g++
# CXX = g++
CXX = clang++ -fno-color-diagnostics --stdlib=libc++ --std=c++11

INCLUDES = -I/opt/local/include -I.

# OPTFLAGS = -g
OPTFLAGS = -O2 -g
OPTFLAGS = -g
# OPTFLAGS = -O2 -g
# OPTFLAGS = -O2 -DNDEBUG

LDFLAGS = -L/opt/local/lib
Expand Down Expand Up @@ -38,7 +39,7 @@ ALGEBRA_HEADERS = algebra/algebra.h algebra/grading.h algebra/module.h \
KNOTKIT_HEADERS = knotkit.h planar_diagram.h dt_code.h knot_diagram.h \
smoothing.h cobordism.h cube.h spanning_tree_complex.h cube_impl.h sseq.h

LIBS = -lgmp
LIBS = -lgmp -lz

all: gss

Expand Down
14 changes: 7 additions & 7 deletions algebra/Q.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ class Q
Q_impl (reader &r)
{
mpq_init (x);
mpz_inp_raw (mpq_numref (x), r.fp);
mpz_inp_raw (mpq_denref (x), r.fp);
r.read_mpz (mpq_numref (x));
r.read_mpz (mpq_denref (x));
}

~Q_impl () { mpq_clear (x); }

void write_self (writer &w) const
{
mpz_out_raw (w.fp, mpq_numref (x));
mpz_out_raw (w.fp, mpq_denref (x));
w.write_mpz (mpq_numref (x));
w.write_mpz (mpq_denref (x));
}
};

Expand Down Expand Up @@ -163,12 +163,12 @@ class Q

Q div (const Q &d) const { return operator / (d); }

triple<Q, Q, Q> extended_gcd (const Q &q) const
tuple<Q, Q, Q> extended_gcd (const Q &q) const
{
if (*this != 0)
return triple<Q, Q, Q> (*this, 1, 0);
return tuple<Q, Q, Q> (*this, 1, 0);
else
return triple<Q, Q, Q> (q, 0, 1);
return tuple<Q, Q, Q> (q, 0, 1);
}

Q gcd (const Q &q) const
Expand Down
55 changes: 33 additions & 22 deletions algebra/Z.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ class Z
Z_impl (reader &r)
{
mpz_init (x);
mpz_inp_raw (x, r.fp);
r.read_mpz (x);
}

~Z_impl () { mpz_clear (x); }

void write_self (writer &w) const
{
mpz_out_raw (w.fp, x);
w.write_mpz (x);
}
};

Expand All @@ -48,6 +48,7 @@ class Z
Z &operator = (int x) { impl = new Z_impl (x); return *this; }

bool operator == (const Z &z) const { return mpz_cmp (impl->x, z.impl->x) == 0; }
bool operator != (const Z &z) const { return !operator == (z); }

bool operator == (int y) const { return mpz_cmp_si (impl->x, y) == 0; }
bool operator != (int y) const { return !operator == (y); }
Expand Down Expand Up @@ -91,13 +92,13 @@ class Z
return Z (STEAL, x);
}

Z operator / (const Z &z) const
Z operator / (const Z &denom) const
{
if (mpz_cmp_si (z.impl->x, 1) == 0)
if (mpz_cmp_si (denom.impl->x, 1) == 0)
return *this;
else
{
assert (mpz_cmp_si (z.impl->x, -1) == 0);
assert (mpz_cmp_si (denom.impl->x, -1) == 0);

mpz_t x;
mpz_init (x);
Expand All @@ -112,6 +113,7 @@ class Z
return Z (COPY, *this);
}

// *this += z1*z2
Z &muladdeq (const Z &z1, const Z &z2)
{
mpz_addmul (impl->x, z1.impl->x, z2.impl->x);
Expand Down Expand Up @@ -148,33 +150,30 @@ class Z
return *this;
}

bool divides (const Z &n) const
bool divides (const Z &num) const
{
// d = *this
return mpz_divisible_p (n.impl->x, impl->x);
return mpz_divisible_p (num.impl->x, impl->x);
}
bool operator | (const Z &num) const { return divides (num); }

bool operator | (const Z &z) const { return divides (z); }

Z div (const Z &d) const
Z divide_exact (const Z &denom) const
{
// n = *this
// num = *this
mpz_t q;
mpz_init (q);
mpz_divexact (q, impl->x, d.impl->x);
mpz_divexact (q, impl->x, denom.impl->x);
return Z (STEAL, q);
}

triple<Z, Z, Z> extended_gcd (const Z &z) const
tuple<Z, Z> divide_with_remainder (const Z &denom) const
{
mpz_t d, s, t;
mpz_init (d);
mpz_init (s);
mpz_init (t);
mpz_gcdext (d, s, t, impl->x, z.impl->x);
return triple<Z, Z, Z> (Z (STEAL, d),
Z (STEAL, s),
Z (STEAL, t));
// *this = num
mpz_t q, r;
mpz_init (q);
mpz_init (r);
mpz_tdiv_qr (q, r, impl->x, denom.impl->x);
return make_tuple (Z (STEAL, q),
Z (STEAL, r));
}

Z gcd (const Z &z) const
Expand All @@ -191,6 +190,18 @@ class Z
return Z (STEAL, m);
}

tuple<Z, Z, Z> extended_gcd (const Z &z) const
{
mpz_t d, s, t;
mpz_init (d);
mpz_init (s);
mpz_init (t);
mpz_gcdext (d, s, t, impl->x, z.impl->x);
return make_tuple (Z (STEAL, d),
Z (STEAL, s),
Z (STEAL, t));
}

static void show_ring () { printf ("Z"); }
void show_self () const { mpz_out_str (stdout, 10, impl->x); }
void display_self () const { show_self (); newline (); }
Expand Down
6 changes: 3 additions & 3 deletions algebra/Z2.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ class Z2
return Z2 (1);
}

triple<Z2, Z2, Z2> extended_gcd (Z2 x) const
tuple<Z2, Z2, Z2> extended_gcd (Z2 x) const
{
if (v)
return triple<Z2, Z2, Z2> (Z2 (1), Z2 (1), Z2 (0));
return make_tuple (Z2 (1), Z2 (1), Z2 (0));
else
return triple<Z2, Z2, Z2> (Z2 (1), Z2 (0), Z2 (1));
return make_tuple (Z2 (1), Z2 (0), Z2 (1));
}

static void show_ring () { printf ("Z2"); }
Expand Down
15 changes: 8 additions & 7 deletions algebra/Zp.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class Zp
}

bool operator == (const Zp &x) const { return v == x.v; }
bool operator != (const Zp &x) const { return !operator == (x); }

bool operator == (int x) const { return operator == (Zp (x)); }
bool operator != (int x) const { return !operator == (x); }
Expand All @@ -56,11 +57,11 @@ class Zp

Zp recip () const
{
triple<unsigned, int, int> t = unsigned_extended_gcd (v, p);
assert (t.first == 1);
assert ((int)t.first == t.second*(int)v + t.third*(int)p);
tuple<unsigned, int, int> t = unsigned_extended_gcd (v, p);
assert (get<0> (t) == 1);
assert ((int)get<0> (t) == get<1> (t)*(int)v + get<2> (t)*(int)p);

return Zp (t.second);
return Zp (get<1> (t));
}

Zp &operator += (const Zp &x)
Expand Down Expand Up @@ -92,12 +93,12 @@ class Zp

Zp div (const Zp &d) const { return operator / (d); }

triple<Zp, Zp, Zp> extended_gcd (const Zp &x) const
tuple<Zp, Zp, Zp> extended_gcd (const Zp &x) const
{
if (v)
return triple<Zp, Zp, Zp> (v, 1, 0);
return make_tuple (v, Zp (1), Zp (0));
else
return triple<Zp, Zp, Zp> (x, 0, 1);
return make_tuple (x, Zp (0), Zp (1));
}

Zp gcd (const Zp &x) const
Expand Down
32 changes: 16 additions & 16 deletions algebra/algebra.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,51 +39,51 @@ int64_gcd (int64 a, int64 b)
(uint64)std::abs (b));
}

static triple<unsigned, int, int>
static tuple<unsigned, int, int>
extended_gcd_1 (int a, int b)
{
if (b == 0)
return triple<unsigned, int, int> (a, 1, 0);
return tuple<unsigned, int, int> (a, 1, 0);

unsigned t = a % b;
if (!t)
return triple<unsigned, int, int> (b, 0, 1);
return make_tuple (b, 0, 1);
else
{
triple<unsigned, int, int> s = unsigned_extended_gcd (b, t);
tuple<unsigned, int, int> s = unsigned_extended_gcd (b, t);

unsigned d = s.first;
int x = s.third,
y = s.second - s.third * (a / b);
unsigned d = get<0> (s);
int x = get<2> (s),
y = get<1> (s) - get<2> (s) * (a / b);

assert ((int)d == a*x + b*y);

return triple<unsigned, int, int> (d, x, y);
return make_tuple (d, x, y);
}
}

triple<unsigned, int, int>
tuple<unsigned, int, int>
unsigned_extended_gcd (unsigned a, unsigned b)
{
return extended_gcd_1 ((int)a, (int)b);
}

triple<unsigned, int, int>
tuple<unsigned, int, int>
extended_gcd (int a, int b)
{
triple<unsigned, int, int> t = extended_gcd_1 (std::abs (a),
std::abs (b));
unsigned d = t.first;
int x = t.second,
y = t.third;
tuple<unsigned, int, int> t = extended_gcd_1 (std::abs (a),
std::abs (b));
unsigned d = get<0> (t);
int x = get<1> (t),
y = get<2> (t);
if (a < 0)
x *= -1;
if (b < 0)
y *= -1;

assert ((int)d == a*x + b*y);

return triple<unsigned, int, int> (d, x, y);
return make_tuple (d, x, y);
}

unsigned
Expand Down
6 changes: 2 additions & 4 deletions algebra/algebra.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@

#include <gmp.h>

#include <lib/lib.h>

inline int recip (int x)
Expand Down Expand Up @@ -42,9 +40,9 @@ unsigned int_lcm (int a, int b);
uint64 int64_lcm (int64 a, int64 b);

// (d, x, y) = gcd (a, b) where x*a + y*b = d
triple<unsigned, int, int> extended_gcd (int a, int b);
tuple<unsigned, int, int> extended_gcd (int a, int b);

triple<unsigned, int, int> unsigned_extended_gcd (unsigned a, unsigned b);
tuple<unsigned, int, int> unsigned_extended_gcd (unsigned a, unsigned b);

template<class R> class linear_combination;
template<class R> class linear_combination_const_iter;
Expand Down
7 changes: 4 additions & 3 deletions algebra/fraction_field.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ template<class T> class fraction_field
fraction_field &operator = (int x) { num = x; denom = 1; return *this; }

bool operator == (const fraction_field &q) const { return num * q.denom == q.num * denom; }
bool operator != (const fraction_field &q) const { return !operator == (q); }

bool operator == (int x) const { return num == denom * T (x); }
bool operator != (int x) const { return !operator == (x); }
Expand Down Expand Up @@ -187,12 +188,12 @@ template<class T> class fraction_field
return fraction_field (1);
}

triple<fraction_field, fraction_field, fraction_field> extended_gcd (const fraction_field &x) const
tuple<fraction_field, fraction_field, fraction_field> extended_gcd (const fraction_field &x) const
{
if (*this != 0)
return triple<fraction_field, fraction_field, fraction_field> (*this, 1, 0);
return make_tuple (*this, fraction_field (1), fraction_field (0));
else
return triple<fraction_field, fraction_field, fraction_field> (x, 0, 1);
return make_tuple (x, fraction_field (0), fraction_field (1));
}

static void show_ring () { printf ("fraction_field("); T::show_ring (); printf (")"); }
Expand Down
2 changes: 2 additions & 0 deletions algebra/grading.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class grading
}

bool operator == (const grading &gr) const { return h == gr.h && q == gr.q; }
bool operator != (const grading &gr) const { return !operator == (gr); }

bool operator < (const grading &gr) const
{
return h < gr.h
Expand Down
1 change: 1 addition & 0 deletions algebra/linear_combination.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class linear_combination
lc3 -= lc2;
return lc3 == 0;
}
bool operator != (const linear_combination &lc) const { return !operator == (lc); }

bool operator == (int x) const
{
Expand Down
Loading

0 comments on commit fdb87ca

Please sign in to comment.