/* Implementation for Objective-C MappedCollector collection object

   Copyright (C) 1993 R. Andrew McCallum <mccallum@cs.rochester.edu>
   Dept. of Computer Science, U. of Rochester, Rochester, NY  14627

   This file is part of the GNU Objective-C Collection library.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.
   
   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with this library; if not, write to the Free
   Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/ 

#include <coll/MappedCollector.h>
#include <coll/Dictionary.h>

@implementation MappedCollector

- initCollection: (id <KeyedCollecting>)aDomain 
  map: (id <KeyedCollecting>)aMap
{
  if (strcmp([aMap contentsDescription], [aDomain keyDescription]))
    [self error:"map's contents are not the same as domain's keys"];
  [super initDescription:[aDomain contentsDescription]
	 keyDescription:[aMap keyDescription]];
  map = aMap;
  domain = aDomain;
  return self;
}

- (elt) elementAtKey: (elt)aKey
{
  return [domain elementAtKey:[map elementAtKey:aKey]];
}

- (elt) replaceElementAtKey: (elt)aKey with: (elt)newElement
{
  return [domain replaceElementAtKey:[map elementAtKey:aKey]
		 with:newElement];
}

- insertElement: (elt)newElement atKey: (elt)aKey
{
  return [domain insertElement:newElement
		 atKey:[map elementAtKey:aKey]];
}

- (elt) removeElementAtKey: (elt)aKey
{
  return [domain removeElementAtKey:[map elementAtKey:aKey]];
}

- (BOOL) includesKey: (elt)aKey
{
  return [domain includesKey:[map elementAtKey:aKey]];
}

- withKeysAndContentsCall: (void(*)(const elt,elt))aFunc 
    whileTrue: (BOOL *)flag
{
  void doIt(elt e)
    {
      elt domainKey = [map elementAtKey:e];
      if ([domain includesKey:domainKey])
	aFunc(e, [domain elementAtKey:domainKey]);
    }
  [map withKeysCall:doIt];
  return self;
}

- (BOOL) getNextKey: (elt*)aKeyPtr content: (elt*)anElementPtr 
  withEnumState: (void**)enumState;
{
  BOOL ret;
  elt mapContent;
  elt domainKey;

  while ((ret = [map getNextKey:aKeyPtr content:&mapContent 
		     withEnumState:enumState])
	 && (![domain includesKey:(domainKey = [map elementAtKey:*aKeyPtr])]))
    ;
  if (!ret)
    return NO;
  *anElementPtr = [domain elementAtKey:domainKey];
  return YES;
}

- species
{
  return [Dictionary class];
}

@end
