Logo

Programming-Idioms

Set b to true if the maps m and n have the same key/value entries, false otherwise.
New implementation

Be concise.

Be useful.

All contributions dictatorially edited by webmasters to match personal tastes.

Please do not paste any copyright violating material.

Please try to avoid dependencies to third-party libraries and frameworks.

Other implementations
#include <map>
b = m == n;
import "maps"
b := maps.Equal(m, n)
import java.util.Map;
boolean b = m.equals(n);
fgl
type
  TMap = specialize TFPGMap<TKey, TData>;

function ContentEquals(m,n : TMap): Boolean;
var
  key: TKey;
  data, data2: TData;
  idx: integer;
begin
  Result := False;
  if m.count <> n.count then
    Exit;
  for idx := 0 to m.count-1 do
  begin
    key := m.keys[idx];
    Result := n.TryGetData(key, data2);
    if Result then
    begin
      data := m.data[idx];
      Result := data2 = data;
    end;
    if not Result then
      Exit;
  end;
end;

begin  
  ...
  b := ContentEquals(m,n);
use Data::Compare;
$b = Compare( \%hash1, \%hash2 );
sub hcmp_numeric {
    my ($h, $g) = @_;

    my $hc = keys %$h;
    my $gc = keys %$g;

    return 0 unless $hc == $gc;
    return 0 unless $hc == grep { exists $g->{$_} } keys %$h;
    die 'non-scalar value detected' 
        if 0 < grep { ref $h->{$_} or ref $g->{$_} } keys %$h;
    return 0 unless $hc == grep { $h->{$_} == $g->{$_} } keys %$h;
    return 1;
}
b = m == n
b = m == n