예제 #1
0
파일: node.go 프로젝트: timtadh/sfp
func int32sToSet(list []int32) *set.SortedSet {
	items := set.NewSortedSet(len(list))
	for _, item := range list {
		items.Add(types.Int32(item))
	}
	return items
}
예제 #2
0
파일: node.go 프로젝트: timtadh/sfp
func (n *Node) nodesFromCandidateKids(exts map[int32][]int32) ([]lattice.Node, error) {
	nodes := make([]lattice.Node, 0, 10)
	for item, txs := range exts {
		if len(txs) >= n.dt.Support() && !n.pat.Items.Has(types.Int32(item)) {
			items := n.pat.Items.Copy()
			items.Add(types.Int32(item))
			node := &Node{
				pat: Pattern{items},
				dt:  n.dt,
				txs: txs,
			}
			err := node.Save()
			if err != nil {
				return nil, err
			}
			nodes = append(nodes, node)
		}
	}
	return nodes, nil
}
예제 #3
0
파일: node.go 프로젝트: timtadh/sfp
func (n *Node) allCandidateKids() map[int32][]int32 {
	exts := make(map[int32][]int32)
	for _, tx := range n.txs {
		for _, item := range n.dt.Index[tx] {
			if !n.pat.Items.Has(types.Int32(item)) {
				exts[item] = append(exts[item], tx)
			}
		}
	}
	return exts
}
예제 #4
0
파일: itemset_test.go 프로젝트: timtadh/sfp
func TestParents_123(x *testing.T) {
	t := assert.New(x)
	_, dt, sup := startingPoints(t)
	n123 := &Node{
		items: set.FromSlice([]types.Hashable{types.Int32(1), types.Int32(2), types.Int32(3)}),
		txs:   []int32{1, 2, 3},
	}
	parents, err := n123.Parents(sup, dt)
	t.Nil(err, "%v", err)
	expected := set.FromSlice([]types.Hashable{
		set.FromSlice([]types.Hashable{types.Int32(1), types.Int32(2)}),
		set.FromSlice([]types.Hashable{types.Int32(1), types.Int32(3)}),
		set.FromSlice([]types.Hashable{types.Int32(2), types.Int32(3)}),
	})
	for _, p := range parents {
		has := expected.Has(p.(*Node).items)
		t.True(has, "%v not in %v", p.(*Node).items, expected)
	}
}
예제 #5
0
파일: node.go 프로젝트: timtadh/sfp
func (n *Node) canonCandidateKids() map[int32][]int32 {
	// this works because n.pat.Items is a set.SortedSet
	l, err := n.pat.Items.Get(n.pat.Items.Size() - 1)
	if err != nil {
		log.Fatal(err)
	}
	largest := int32(l.(types.Int32))
	exts := make(map[int32][]int32)
	for _, tx := range n.txs {
		for _, item := range n.dt.Index[tx] {
			if item <= largest {
				continue
			}
			if !n.pat.Items.Has(types.Int32(item)) {
				exts[item] = append(exts[item], tx)
			}
		}
	}
	return exts
}
예제 #6
0
파일: loader.go 프로젝트: timtadh/sfp
func (l *IntLoader) startingPoints(items itemsIter) ([]lattice.Node, error) {
	idx, inv, err := l.indices(items)
	if err != nil {
		return nil, err
	}
	l.sets.Index = idx
	l.sets.InvertedIndex = inv
	nodes := make([]lattice.Node, 0, 10)
	for item, txs := range inv {
		if len(txs) >= l.sets.Support() {
			errors.Logf("INFO", "item %d len(txs) %d", item, len(txs))
			n := &Node{
				pat: Pattern{set.FromSlice([]types.Hashable{types.Int32(item)})},
				dt:  l.sets,
				txs: txs,
			}
			nodes = append(nodes, n)
		}
	}
	return nodes, nil
}
예제 #7
0
파일: node.go 프로젝트: timtadh/sfp
func (n *Node) Parents() ([]lattice.Node, error) {
	if n.pat.Items.Size() == 0 {
		return []lattice.Node{}, nil
	} else if n.pat.Items.Size() == 1 {
		return []lattice.Node{n.dt.empty}, nil
	}
	i := setToInt32s(n.pat.Items)
	if has, err := n.dt.ParentCount.Has(i); err != nil {
		return nil, err
	} else if has {
		return n.cached(n.dt.Parents, i)
	}
	parents := make([]*set.SortedSet, 0, n.pat.Items.Size())
	for item, next := n.pat.Items.Items()(); next != nil; item, next = next() {
		parent := n.pat.Items.Copy()
		parent.Delete(item)
		parents = append(parents, parent)
	}
	nodes := make([]lattice.Node, 0, 10)
	for _, items := range parents {
		if node, err := TryLoadNode(setToInt32s(items), n.dt); err != nil {
			return nil, err
		} else if node != nil {
			nodes = append(nodes, node)
			continue
		}
		ctxs := int32sToSet(n.txs)
		var txs types.Set
		for item, next := items.Items()(); next != nil; item, next = next() {
			mytxs := set.NewSortedSet(len(n.txs) + 10)
			for _, tx := range n.dt.InvertedIndex[item.(types.Int32)] {
				if !ctxs.Has(types.Int32(tx)) {
					mytxs.Add(types.Int32(tx))
				}
			}
			var err error
			if txs == nil {
				txs = mytxs
			} else {
				txs, err = txs.Intersect(mytxs)
				if err != nil {
					return nil, err
				}
			}
		}
		txs, err := txs.Union(ctxs)
		if err != nil {
			return nil, err
		}
		stxs := make([]int32, 0, txs.Size())
		for item, next := txs.Items()(); next != nil; item, next = next() {
			stxs = append(stxs, int32(item.(types.Int32)))
		}
		node := &Node{Pattern{items}, n.dt, stxs}
		err = node.Save()
		if err != nil {
			return nil, err
		}
		nodes = append(nodes, node)
	}
	err := n.cache(n.dt.ParentCount, n.dt.Parents, i, nodes)
	if err != nil {
		return nil, err
	}
	return nodes, nil
}
예제 #8
0
파일: itemset_test.go 프로젝트: timtadh/sfp
func TestKids_1(x *testing.T) {
	t := assert.New(x)
	nodes, dt, sup := startingPoints(t)
	n1 := nodes[0]
	kids, err := n1.Children(sup, dt)
	t.Nil(err)
	expected := set.FromSlice([]types.Hashable{
		set.FromSlice([]types.Hashable{types.Int32(1), types.Int32(2)}),
		set.FromSlice([]types.Hashable{types.Int32(1), types.Int32(3)}),
		set.FromSlice([]types.Hashable{types.Int32(1), types.Int32(10)}),
		set.FromSlice([]types.Hashable{types.Int32(1), types.Int32(11)}),
		set.FromSlice([]types.Hashable{types.Int32(1), types.Int32(12)}),
	})
	var next *Node = nil
	for _, kid := range kids {
		if kid.(*Node).items.Has(types.Int32(2)) {
			next = kid.(*Node)
		}
		has := expected.Has(kid.(*Node).items)
		t.True(has, "%v not in %v", kid.(*Node).items, expected)
	}
	kids, err = next.Children(sup, dt)
	t.Nil(err)
	t.True(len(kids) == 1, "len(kids) %d != 1", len(kids))
	t.True(kids[0].(*Node).items.Equals(
		set.FromSlice([]types.Hashable{types.Int32(1), types.Int32(2),
			types.Int32(3)})))
}