Skip to content

Commit

Permalink
- Extended the Hash.delete() method to detele more than one key a time.
Browse files Browse the repository at this point in the history
new file:   scripts/topological_sort.sf
  • Loading branch information
trizen committed Sep 9, 2015
1 parent cb48960 commit 8a17915
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
1 change: 1 addition & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,7 @@ scripts/sysopen_and_sysread.sf
scripts/tennis_tournament.sf
scripts/test_all.pl
scripts/token_recursive.sf
scripts/topological_sort.sf
scripts/triangle_path.sf
scripts/trizen's_pair_numbers.sf
scripts/two_dimensional_array.sf
Expand Down
7 changes: 2 additions & 5 deletions lib/Sidef/Types/Hash/Hash.pm
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,8 @@ package Sidef::Types::Hash::Hash {
*add = \&append;

sub delete {
my ($self, $key) = @_;
if (exists $self->{data}{$key}) {
return (delete $self->{data}{$key})->get_value;
}
return;
my ($self, @keys) = @_;
Sidef::Types::Array::List->new(delete @{$self->{data}}{map { $_->get_value } @keys});
}

sub _iterate {
Expand Down
48 changes: 48 additions & 0 deletions scripts/topological_sort.sf
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/ruby

#
## http://rosettacode.org/wiki/Topological_sort
#

func print_topo_sort (deps) {
var ba = Hash.new;
deps.each { |before, afters|
afters.each { |after|
if (before != after) {
ba[before][after] = 1;
};
ba[after] \\= Hash.new;
}
};

loop {
var afters = ba.keys.grep { ba[_].values.len == 0 }.sort;
afters.len || break;
say afters.join(" ");
ba.delete(afters...);
ba.values.each { |v| v.delete(afters...) };
};

say (ba.len ? "Cicle found! #{ba.keys.sort}" : "---");
}

var deps = Hash.new(
des_system_lib => < std synopsys std_cell_lib des_system_lib dw02
dw01 ramlib ieee >,
dw01 => < ieee dw01 dware gtech >,
dw02 => < ieee dw02 dware >,
dw03 => < std synopsys dware dw03 dw02 dw01 ieee gtech >,
dw04 => < dw04 ieee dw01 dware gtech >,
dw05 => < dw05 ieee dware >,
dw06 => < dw06 ieee dware >,
dw07 => < ieee dware >,
dware => < ieee dware >,
gtech => < ieee gtech >,
ramlib => < std ieee >,
std_cell_lib => < ieee std_cell_lib >,
synopsys => < >
);

print_topo_sort(deps);
deps[:dw01].append('dw04'); # Add unresolvable dependency
print_topo_sort(deps);

0 comments on commit 8a17915

Please sign in to comment.