Class: LemonGraph::NodeMap

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
ext/lemongraph/lemongraph.cc

Instance Method Summary collapse

Constructor Details

#initialize(graph, default_value = nil) ⇒ NodeMap #initialize(graph) {|node, nodemap| ... } ⇒ NodeMap

Returns a new node map related to graph.

If no block is given, default_value will be returned when a value is fetched for a node for which no value was set.

If a block is given, it will be called if needed to provide values for nodes for which no value was set, instead of returning default_value.

Overloads:

  • #initialize(graph, default_value = nil) ⇒ NodeMap

    Parameters:

    • graph (Graph, Digraph)

      the graph this node map is to be bound to

    • default_value (Object) (defaults to: nil)

      the default value returned for nodes with no value.

  • #initialize(graph) {|node, nodemap| ... } ⇒ NodeMap

    Parameters:

    • graph (Graph, Digraph)

      the graph this node map is to be bound to

    Yield Parameters:

    Yield Returns:

    • (Object)

      the default value returned for this node



702
703
704
705
706
707
708
709
710
711
# File 'ext/lemongraph/node_map.cc', line 702

VALUE lemongraph_nodemap_initialize(int argc, VALUE *argv, VALUE self)
{
	VALUE rbg; 
	VALUE dflt = Qnil;
	VALUE proc = Qnil;
	rb_scan_args(argc, argv, "11&", &rbg, &dflt, &proc);
	LemonGraph::NodeMap& nm = lemongraph_nodemap_rb2ref(self);
	nm.initialize(self, rbg, dflt, proc);
	return self;
}

Instance Method Details

#==(other) ⇒ Boolean

Returns true if other is a NodeMap and share the same graph, default value and proc as self and and all entries from other and self are equal (==).

Returns:

  • (Boolean)


992
993
994
995
996
997
998
999
# File 'ext/lemongraph/node_map.cc', line 992

VALUE lemongraph_nodemap_is_equal(VALUE self, VALUE other)
{
	if (rb_obj_is_kind_of(other, c_NodeMap) != Qtrue)
		return Qfalse;
	LemonGraph::NodeMap& m = lemongraph_nodemap_rb2ref(self);
	LemonGraph::NodeMap& o = lemongraph_nodemap_rb2ref(other);
	return o.is_equal(m) ? Qtrue : Qfalse;
}

#[](key) ⇒ Object

Returns the value associated with the given node key.

Parameters:

Returns:

  • (Object)


804
805
806
807
808
# File 'ext/lemongraph/node_map.cc', line 804

VALUE lemongraph_nodemap_get(VALUE self, VALUE key)
{
	LemonGraph::NodeMap& nm = lemongraph_nodemap_rb2ref(self);
	return nm.get(key);
}

#[]=(key, value) ⇒ Object

Associates the given value with the given node key; returns value.

Parameters:



815
816
817
818
819
820
# File 'ext/lemongraph/node_map.cc', line 815

VALUE lemongraph_nodemap_set(VALUE self, VALUE key, VALUE value)
{
	LemonGraph::NodeMap& nm = lemongraph_nodemap_rb2ref(self);
	nm.set(key, value);
	return value;
}

#clearself

Removes all values.

Returns:

  • (self)


927
928
929
930
931
932
# File 'ext/lemongraph/node_map.cc', line 927

VALUE lemongraph_nodemap_clear(VALUE self)
{
	LemonGraph::NodeMap& nm = lemongraph_nodemap_rb2ref(self);
	nm.clear();
	return self;
}

#defaultObject

Returns the default value set for this node map.

Returns:

  • (Object)


755
756
757
758
759
# File 'ext/lemongraph/node_map.cc', line 755

VALUE lemongraph_nodemap_get_dflt_val(VALUE self)
{
	LemonGraph::NodeMap& nm = lemongraph_nodemap_rb2ref(self);
	return nm.default_value();
}

#default=(dflt) ⇒ Object

Sets the default value for this node map.



764
765
766
767
768
769
# File 'ext/lemongraph/node_map.cc', line 764

VALUE lemongraph_nodemap_set_dflt_val(VALUE self, VALUE dflt)
{
	LemonGraph::NodeMap& nm = lemongraph_nodemap_rb2ref(self);
	nm.set_default_value(dflt);
	return self;
}

#default_procProc?

Returns the default proc set for this node map.

Returns:

  • (Proc, nil)


775
776
777
778
779
# File 'ext/lemongraph/node_map.cc', line 775

VALUE lemongraph_nodemap_get_dflt_proc(VALUE self)
{
	LemonGraph::NodeMap& nm = lemongraph_nodemap_rb2ref(self);
	return nm.default_proc();
}

#default_proc=(proc) ⇒ Object

Sets the default proc for this node map.

If set, the default proc is called when a value is fetched for a node which was not assigned a value. The proc parameters must be the node key and the node map.

Set to nil to reset it so that the default value is returned when needed instead of calling the default proc.

Parameters:

  • proc (Proc, nil)


792
793
794
795
796
797
# File 'ext/lemongraph/node_map.cc', line 792

VALUE lemongraph_nodemap_set_dflt_proc(VALUE self, VALUE proc)
{
	LemonGraph::NodeMap& nm = lemongraph_nodemap_rb2ref(self);
	nm.set_default_proc(proc);
	return self;
}

#delete(key) ⇒ Object

Deletes the entry for the given node and returns its associated value, or the default one.

Parameters:



938
939
940
941
942
# File 'ext/lemongraph/node_map.cc', line 938

VALUE lemongraph_nodemap_delete(VALUE self, VALUE key)
{
	LemonGraph::NodeMap& nm = lemongraph_nodemap_rb2ref(self);
	return nm.del(key);
}

#delete_if {|node, value| ... } ⇒ self #delete_ifEnumerator

Delete entries for which the block returns a truthy value. Returns self, or, if no block is given, an Enumerator is returned.

Overloads:

  • #delete_if {|node, value| ... } ⇒ self

    Calls the block with each node-value pair; deletes each entry for which the block returns a truthy value; returns self

    Yield Parameters:

    Yield Returns:

    • (Boolean)

      whether to delete or not the entry

    Returns:

    • (self)
  • #delete_ifEnumerator

    Returns:

    • (Enumerator)

Returns:

  • (self, Enumerator)


960
961
962
963
964
# File 'ext/lemongraph/node_map.cc', line 960

VALUE lemongraph_nodemap_delete_if(VALUE self)
{
	LemonGraph::NodeMap& nm = lemongraph_nodemap_rb2ref(self);
	return nm.del_if();
}

#each_pair {|node, value| ... } ⇒ self #each_pairEnumerator #each {|node, value| ... } ⇒ self #eachEnumerator

Calls the block, if given, once for each node key and value pair. The nodes and values are passed as parameters to the block.

Returns self, or, if no block is given, an Enumerator is returned.

Overloads:

  • #each_pair {|node, value| ... } ⇒ self

    Yield Parameters:

    Returns:

    • (self)
  • #each_pairEnumerator

    Returns:

    • (Enumerator)
  • #each {|node, value| ... } ⇒ self

    Yield Parameters:

    Returns:

    • (self)
  • #eachEnumerator

    Returns:

    • (Enumerator)

Returns:

  • (self, Enumerator)


917
918
919
920
921
# File 'ext/lemongraph/node_map.cc', line 917

VALUE lemongraph_nodemap_each_pair(VALUE self)
{
	LemonGraph::NodeMap& nm = lemongraph_nodemap_rb2ref(self);
	return nm.each_pair();
}

#each_node {|node| ... } ⇒ self #each_nodeEnumerator

Calls the block, if given, once for each node key for which a value has been set. The nodes are passed as parameters to the block.

Returns self, or, if no block is given, an Enumerator is returned.

Overloads:

  • #each_node {|node| ... } ⇒ self

    Yield Parameters:

    Returns:

    • (self)
  • #each_nodeEnumerator

    Returns:

    • (Enumerator)

Returns:

  • (self, Enumerator)


874
875
876
877
878
# File 'ext/lemongraph/node_map.cc', line 874

VALUE lemongraph_nodemap_each_node(VALUE self)
{
	LemonGraph::NodeMap& nm = lemongraph_nodemap_rb2ref(self);
	return nm.each_node();
}

#each_pair {|node, value| ... } ⇒ self #each_pairEnumerator #each {|node, value| ... } ⇒ self #eachEnumerator

Calls the block, if given, once for each node key and value pair. The nodes and values are passed as parameters to the block.

Returns self, or, if no block is given, an Enumerator is returned.

Overloads:

  • #each_pair {|node, value| ... } ⇒ self

    Yield Parameters:

    Returns:

    • (self)
  • #each_pairEnumerator

    Returns:

    • (Enumerator)
  • #each {|node, value| ... } ⇒ self

    Yield Parameters:

    Returns:

    • (self)
  • #eachEnumerator

    Returns:

    • (Enumerator)

Returns:

  • (self, Enumerator)


917
918
919
920
921
# File 'ext/lemongraph/node_map.cc', line 917

VALUE lemongraph_nodemap_each_pair(VALUE self)
{
	LemonGraph::NodeMap& nm = lemongraph_nodemap_rb2ref(self);
	return nm.each_pair();
}

#each_value {|value| ... } ⇒ self #each_valueEnumerator

Calls the block, if given, once for each value that has been set. The values are passed as parameters to the block.

Returns self, or, if no block is given, an Enumerator is returned.

Overloads:

  • #each_value {|value| ... } ⇒ self

    Yield Parameters:

    • value (Object)

    Returns:

    • (self)
  • #each_valueEnumerator

    Returns:

    • (Enumerator)

Returns:

  • (self, Enumerator)


892
893
894
895
896
# File 'ext/lemongraph/node_map.cc', line 892

VALUE lemongraph_nodemap_each_value(VALUE self)
{
	LemonGraph::NodeMap& nm = lemongraph_nodemap_rb2ref(self);
	return nm.each_value();
}

#empty?Boolean

Returns true if there are no entries, false otherwise

Returns:

  • (Boolean)


856
857
858
859
860
# File 'ext/lemongraph/node_map.cc', line 856

VALUE lemongraph_nodemap_is_empty(VALUE self)
{
	LemonGraph::NodeMap& nm = lemongraph_nodemap_rb2ref(self);
	return nm.is_empty();
}

#graphGraph, Digraph

Returns the graph this node map is related to.

Returns:



745
746
747
748
749
# File 'ext/lemongraph/node_map.cc', line 745

VALUE lemongraph_nodemap_graph(VALUE self)
{
	LemonGraph::NodeMap& nm = lemongraph_nodemap_rb2ref(self);
	return nm.graph();
}

#has_node?(node) ⇒ Boolean Also known as: node?

Returns true if a value was set for node, otherwise false.

Parameters:

Returns:

  • (Boolean)


971
972
973
974
975
# File 'ext/lemongraph/node_map.cc', line 971

VALUE lemongraph_nodemap_has_node(VALUE self, VALUE node)
{
	LemonGraph::NodeMap& nm = lemongraph_nodemap_rb2ref(self);
	return nm.has_node(node);
}

#has_value?(value) ⇒ Boolean Also known as: value?

Returns true if value is a value in self, otherwise false.

Returns:

  • (Boolean)


981
982
983
984
985
# File 'ext/lemongraph/node_map.cc', line 981

VALUE lemongraph_nodemap_has_value(VALUE self, VALUE value)
{
	LemonGraph::NodeMap& nm = lemongraph_nodemap_rb2ref(self);
	return nm.has_value(value);
}

#hashInteger

Returns the integer hash value for self.

Returns:

  • (Integer)


1005
1006
1007
1008
1009
# File 'ext/lemongraph/node_map.cc', line 1005

VALUE lemongraph_nodemap_hash(VALUE self)
{
	LemonGraph::NodeMap& nm = lemongraph_nodemap_rb2ref(self);
	return nm.hash();
}

#initialize_copy(orig) ⇒ self

Method called by dup and clone methods.

Returns:

  • (self)


718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
# File 'ext/lemongraph/node_map.cc', line 718

VALUE lemongraph_nodemap_initialize_copy(VALUE self, VALUE orig)
{
	rb_call_super(1, &orig);
	LemonGraph::NodeMap& from = lemongraph_nodemap_rb2ref(orig);
	LemonGraph::NodeMap& to   = lemongraph_nodemap_rb2ref(self);
	to.initialize(self, from.graph(), from.default_value(), from.default_proc());
	if (from.is_for_directed_graph()) {
		for (lemon::ListDigraph::NodeIt n(lemongraph_digraph_rb2ref(from.graph())); n != lemon::INVALID; ++n) {
			const LemonGraph::RBValue& v = from[n];
			if (v.is_valid())
				to[n] = v;
		}
	}
	else {
		for (lemon::ListGraph::NodeIt n(lemongraph_graph_rb2ref(from.graph())); n != lemon::INVALID; ++n) {
			const LemonGraph::RBValue& v = from[n];
			if (v.is_valid())
				to[n] = v;
		}
	}
	return self;
}

#lengthInteger

Return the count of entries in slef for which a value has been set

Returns:

  • (Integer)


846
847
848
849
850
# File 'ext/lemongraph/node_map.cc', line 846

VALUE lemongraph_nodemap_length(VALUE self)
{
	LemonGraph::NodeMap& nm = lemongraph_nodemap_rb2ref(self);
	return nm.length();
}

#nodesArray<Graph::Node,Digraph::Node>

Returns an array containing all node keys for which a value has been set.

Returns:



826
827
828
829
830
# File 'ext/lemongraph/node_map.cc', line 826

VALUE lemongraph_nodemap_nodes(VALUE self)
{
	LemonGraph::NodeMap& nm = lemongraph_nodemap_rb2ref(self);
	return nm.keys();
}

#valuesArray<Object>

Returns an array containing all values set for nodes.

Returns:

  • (Array<Object>)


836
837
838
839
840
# File 'ext/lemongraph/node_map.cc', line 836

VALUE lemongraph_nodemap_values(VALUE self)
{
	LemonGraph::NodeMap& nm = lemongraph_nodemap_rb2ref(self);
	return nm.values();
}