Commit 1ba518bd authored by Boris Mühmer (ADESTIS)'s avatar Boris Mühmer (ADESTIS) 💬
Browse files

imported tgz from https://maurer-berlin.eu/nU/

parent 868d92fe
Loading
Loading
Loading
Loading
+33 −0
Original line number Diff line number Diff line
package achan

// (c) Christian Maurer   v. 171106 - license see nU.go

import ("sync"; . "nU/obj"; "nU/buf")

type asynchronousChannel struct {
  Any
  buf.Buffer
  ch chan Any
  sync.Mutex
}

func new_(a Any) AsynchronousChannel {
  x := new(asynchronousChannel)
  x.Buffer = buf.New(a)
  x.ch = make(chan Any) // synchronous !
  return x
}

func (x *asynchronousChannel) Send (a Any) {
  x.Mutex.Lock()
  x.Buffer.Ins (a)
  x.Mutex.Unlock()
}

func (x *asynchronousChannel) Recv() Any {
  x.Mutex.Lock()
  defer x.Mutex.Unlock()
  a := x.Buffer.Get()
  if a == x.Any { panic("fatal error: all goroutines are asleep - deadlock!") }
  return a
}

achan/def.go

0 → 100644
+15 −0
Original line number Diff line number Diff line
package achan

// (c) Christian Maurer   v. 171104 - license see nU.go

import . "nU/obj"

type AsynchronousChannel interface {

  Send (a Any)

  Recv() Any
}

// Liefert einen neuen asynchronen Kanal.
func New (a Any) AsynchronousChannel { return new_(a) }

adj/adjacencyMatrix.go

0 → 100644
+105 −0
Original line number Diff line number Diff line
package adj

// (c) Christian Maurer   v. 171227 - license see nU.go

import (. "nU/obj"; "nU/col"; "nU/scr")

func (x *adjacencyMatrix) Num() uint {
  return x.uint
}

func (x *adjacencyMatrix) Equiv (Y AdjacencyMatrix) bool {
  y := x.imp(Y)
  if x.uint != y.uint { return false }
  if ! Eq(x.v, y.v) || ! Eq(x.e, y.e) { return false }
  return true
}

func (x *adjacencyMatrix) Edge (i, k uint, e Any) {
  if i >= x.uint || k >= x.uint { return }
  CheckTypeEq (e, x.e)
  x.entry[i][k].edge = Clone(e)
}

func (x *adjacencyMatrix) Vertex (i uint) Any {
  return Clone(x.entry[i][i].vertex)
}

func (x *adjacencyMatrix) Val (i, k uint) uint {
  if i >= x.uint || k >= x.uint {
    return 0
  }
  if Eq (x.entry[i][k].edge, x.e) {
    return 0
  }
  return Val(x.entry[i][k].edge)
}

func (x *adjacencyMatrix) Set (i, k uint, v, e Any) {
  if i >= x.uint || k >= x.uint { return }
  CheckTypeEq (v, x.v)
  CheckTypeEq (e, x.e)
  if i == k {
    x.entry[i][i].vertex = Clone(v)
    x.entry[i][i].edge = Clone(x.e) // no loops
  } else {
    x.entry[i][k].vertex = Clone(x.v) // no vertex
    x.entry[i][k].edge = Clone(e)
  }
}

func (x *adjacencyMatrix) Symmetric() bool {
  for i := uint(0); i < x.uint; i++ {
    for k := uint(0); k < x.uint; k++ {
      if Val(x.entry[i][k].edge) != Val(x.entry[k][i].edge) {
        return false
      }
    }
  }
  return true
}

func (x *adjacencyMatrix) Add (Y AdjacencyMatrix) {
  y := x.imp(Y)
  for i := uint(0); i < x.uint; i++ {
    for k := uint(0); k < x.uint; k++ {
      if i != k {
        if x.Val (i, k) == 0 && y.Val (i, k ) > 0 {
          x.entry[i][k].edge = Clone (y.entry[i][k].edge)
        }
        if Eq (x.entry[i][i].vertex, x.v) {
          x.entry[i][i].vertex = Clone (y.entry[i][i].vertex)
        }
        if Eq (x.entry[k][k].vertex, x.v) {
          x.entry[k][k].vertex = Clone (y.entry[k][k].vertex)
        }
      }
    }
  }
}

func (x *adjacencyMatrix) Full() bool {
  for i := uint(0); i < x.uint; i++ {
    full := false
    for k := uint(0); k < x.uint; k++ {
      full = full || x.Val (i, k) > 0
    }
    if ! full {
      return false
    }
  }
  return true
}

func (x *adjacencyMatrix) Write() {
  for i := uint(0); i < x.uint; i++ {
    for k := uint(0); k < x.uint; k++ {
      if i == k {
        scr.ColourF (col.Magenta())
      } else {
        scr.ColourF (col.Yellow())
      }
      scr.WriteNat (Val(x.entry[i][k].edge), i, 2 * k)
    }
  }
}

adj/def.go

0 → 100644
+80 −0
Original line number Diff line number Diff line
package adj

// (c) Christian Maurer   v. 171125 - license see nU.go

import . "nU/obj"

type AdjacencyMatrix interface {
// Sqare matrices with pairs (v, e) as entries,
// where v is atomic or implements Object and
// and e has a uint-type or implements Valuator.
// The entry of a matrix x in row i and column k is called x(i,k).
// Any such matrix defines a graph in the following way:
// x(i,k) = (e, v) means
// for i == k:
//     v is a vertex in the graph (in this case e is the pattern edge of x.
// for i != k:
//     There is an edge outgoing from the i-th and incoming at the k-th vertex
//     of the graph with value e, iff v is not equal to the pattern vertex of x.
//     In this case v is the pattern vertex of x.
// The patterns are those objects, that are given to a New as parameters;
// they must not be used as vertices or edges resp.

  Object

// Returns the number of rows/columns of x, defined by New.
  Num() uint

// Returns true, iff x and y have the same number of rows/columns
// and equal vertex patterns and equal edge patterns.
  Equiv (y AdjacencyMatrix) bool

// Pre: e is of the type of the pattern edge of x.
// If i or k >= x.Num(), nothing has happened.
// Otherwise: x(i,k) is the pair (v, e) with v = pattern vertex of x,
// i.e. in the corresponding graph there is an edge with the value of e
// from its i-th vertex to its k-th vertex, iff e is not equal to the pattern edge of x.
  Edge (i, k uint, e Any)

// Returns the first element in the pair x(i,i), i.e. a vertex.
  Vertex (i uint) Any

// Pre: i, k < x.Num().
// Returns 0, iff for x(i,k) = (v, e) e is the pattern edge of x,
// returns otherwise the value of e.
  Val (i, k uint) uint

// Pre: v has the type as the pattern vertex of x and
//      e has the type of the pattern edge of x.
// If i or k >= x.Num(), nothing has happened.
// Otherwise: x(i,k) == (v, e).
  Set (i, k uint, v, e Any)

// Returns true, iff x(i,k) == x(k,i) for all i, k < x.Num(),
// i.e. the corresponding graph is undirected.
  Symmetric() bool

// Pre: x and y are equivalent.
// x contains all entries of x and additionally all entries of y
// with a value > 0 (the values of entries of x are overwritten
// by the values of corresponding entries in x. // XXX ? ? ?
  Add (y AdjacencyMatrix)

// Returns true, iff each row of x contains at least one entry
// with (v, e) with Val(e) > 0, i.e. iff in the corresponding
// graph every node has at least one outgoing edge.
  Full () bool

// Vor.: Die Einträge von x sind vom Typ uint
//       oder implementieren Valuator.
// x ist auf dem Bildschirm ausgegeben.
  Write()
}

// Pre: n > 0; e is of a uint-type or implements Valuator;
//      v is atomic or implements Object.
// v is the pattern vertex of x
// Returns an n*n-matrix with the pattern vertex v
// and the pattern edge e. All it's entries have the value
// (v, e).
func New (n uint, v, e Any) AdjacencyMatrix { return new_(n,v,e) }

adj/object.go

0 → 100644
+113 −0
Original line number Diff line number Diff line
package adj

// (c) Christian Maurer   v. 171125 - license see nU.go

import . "nU/obj"

func (x *adjacencyMatrix) imp (Y Any) *adjacencyMatrix {
  y, ok := Y.(*adjacencyMatrix)
  if ! ok { TypeNotEqPanic (x, Y) }
  CheckTypeEq (x.e, y.e)
  CheckTypeEq (x.v, y.v)
  return y
}

func (x *adjacencyMatrix) Empty() bool {
  for i := uint(0); i < x.uint; i++ {
    for k := uint(0); k < x.uint; k++ {
      if ! Eq (x.entry[i][k].edge, x.e) {
        return false
      }
    }
  }
  return true
}

func (x *adjacencyMatrix) Clr() {
  for i := uint(0); i < x.uint; i++ {
    for k := uint(0); k < x.uint; k++ {
      x.entry[i][k] = pair { x.v, x.e }
    }
  }
}

func (x *adjacencyMatrix) Eq (Y Any) bool {
  y := x.imp (Y)
  if x.Empty() { return y.Empty() }
  for i := uint(0); i < x.uint; i++ {
    if ! Eq (x.Vertex(i), y.Vertex(i)) { return false }
    for k := uint(0); k < x.uint; k++ {
      if ! Eq (x.entry[i][k].edge, y.entry[i][k].edge) ||
         ! Eq (x.entry[i][k].vertex, y.entry[i][k].vertex) {
//      if i != k && x.Val(i,k) != y.Val(i,k) {
        return false
      }
    }
  }
  return true
}

func (x *adjacencyMatrix) Copy (Y Any) {
  y := x.imp (Y)
  x.uint = y.uint
  x.e, x.v = Clone(y.e), Clone(y.v)
  for i := uint(0); i < x.uint; i++ {
    for k := uint(0); k < x.uint; k++ {
      x.entry[i][k].vertex = Clone (y.entry[i][k].vertex)
      x.entry[i][k].edge = Clone (y.entry[i][k].edge)
    }
  }
}

func (x *adjacencyMatrix) Clone() Any {
  y := new_(x.uint, x.v, x.e)
  y.Copy (x)
  return y
}

func (x *adjacencyMatrix) Less (Y Any) bool {
  return false
}

func (x *adjacencyMatrix) Codelen() uint {
  v, e := Codelen(x.v), Codelen(x.e)
  return 4 + (1 + x.uint * x.uint) * (v + e)
}

func (x *adjacencyMatrix) Encode() []byte {
  bs := make ([]byte, x.Codelen())
  v, e := Codelen(x.v), Codelen(x.e)
  copy (bs[:4], Encode (uint32(x.uint)))
  i := uint(4)
  copy (bs[i:i+v], Encode (x.v))
  i += v
  copy (bs[i:i+e], Encode (x.e))
  i += e
  for j := uint(0); j < x.uint; j++ {
    for k := uint(0); k < x.uint; k++ {
      copy (bs[i:i+v], Encode (x.entry[j][k].vertex))
      i += v
      copy (bs[i:i+e], Encode (x.entry[j][k].edge))
      i += e
    }
  }
  return bs
}

func (x *adjacencyMatrix) Decode (bs []byte) {
  v, e := Codelen(x.v), Codelen(x.e)
  x.uint = uint(Decode (uint32(0), bs[:4]).(uint32))
  i := uint(4)
  x.v = Decode (x.v, bs[i:i+v])
  i += v
  x.e = Decode (x.e, bs[i:i+e])
  i += e
  for j := uint(0); j < x.uint; j++ {
    for k := uint(0); k < x.uint; k++ {
      x.entry[j][k].vertex = Decode (x.v, bs[i:i+v])
      i += v
      x.entry[j][k].edge = Decode (x.e, bs[i:i+e])
      i += e
    }
  }
}
Loading