//
// $Id: Queue.m,v 1.11 2005/05/18 06:46:54 will_mason Exp $
//
// vi: set ft=objc:

/*
 * ObjectiveLib - a library of containers and algorithms for Objective-C
 *
 * Copyright (c) 2004
 * Will Mason
 *
 * Portions:
 *
 * Copyright (c) 1994
 * Hewlett-Packard Company
 *
 * Copyright (c) 1996,1997
 * Silicon Graphics Computer Systems, Inc.
 *
 * Copyright (c) 1997
 * Moscow Center for SPARC Technology
 *
 * Copyright (c) 1999 
 * Boris Fomitchev
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * You may contact the author at will_mason@users.sourceforge.net.
 */

#import "Queue.h"
#import "Algorithm.h"
#import "Macros.h"
#import "Deque.h"
#import "VectorPackage.h"
#import "ContainerCoder.h"
#import "Constants.h"
#import <Foundation/NSCoder.h>

@implementation OLQueue

+ (id) queue
{
    OL_BEGIN_AUTO_CTOR(OLQueue)
        init
    OL_END_AUTO_CTOR;
}

+ (id) queueWithQueue: (OLQueue*)right
{
    OL_BEGIN_AUTO_CTOR(OLQueue)
        initWithQueue: right
    OL_END_AUTO_CTOR;
}

- (id) init
{
    return [self initWithAllocator: [OLAllocator defaultAllocator]];
}

- (id) initWithAllocator: (OLAllocator*)alloc
{
    [super init];
    deque = [[alloc allocateObject: [OLDeque class]] init];
    return self;
}


- (id) initWithCoder: (NSCoder*)decoder
{
    [super init];
    if ([decoder respondsToSelector: @selector(allowsKeyedCoding)] &&
        [decoder allowsKeyedCoding])
    {
        deque = OBJ_RETAIN([decoder decodeObjectForKey: DEQUE_KEY]);
    }
    else
    {
        deque = OBJ_RETAIN([decoder decodeObject]);
    }
    return self;
}

- (id) initWithQueue: (OLQueue*)queue
{
    [super init];
    deque = [queue->deque copyWithAllocator: [queue->deque allocator]];
    return self;
}

- (void) dealloc
{
    [[deque allocator] deallocateObject: deque];
    [super dealloc];
}

- (id) back
{
    return [deque back];
}

- (id) copyWithAllocator: (OLAllocator*)alloc
{
    return [[alloc allocateObject: [OLQueue class]]
        initWithQueue: self];
}

- (id) copyWithZone: (NSZone*)zone
{
    return [[OLQueue allocWithZone: zone] initWithQueue: self];
}

- (BOOL) empty
{
    return [deque empty];
}

- (void) encodeWithCoder: (NSCoder*)encoder
{
    if ([encoder respondsToSelector: @selector(allowsKeyedCoding)] &&
        [encoder allowsKeyedCoding])
    {
        [encoder encodeObject: deque forKey: DEQUE_KEY];
    }
    else
    {
        [encoder encodeObject: deque];
    }
}

- (id) front
{
    return [deque front];
}

- (OLAllocator*) allocator
{
    return [deque allocator];
}

- (BOOL) isEqual: (id)object
{
    return [object isKindOfClass: [OLQueue class]] &&
           [((OLQueue*)object)->deque isEqual: deque];
}

- (void) pop
{
    [deque popFront];
}

- (void) push: (id)object
{
    [deque pushBack: object];
}

- (unsigned) size
{
    return [deque size];
}

@end

@implementation OLPriorityQueue

+ (id) priorityQueue
{
    OL_BEGIN_AUTO_CTOR(OLPriorityQueue)
        init
    OL_END_AUTO_CTOR;
}

+ (id) priorityQueueFrom: (OLForwardIterator*)first to: (OLForwardIterator*)last
{
    OL_BEGIN_AUTO_CTOR(OLPriorityQueue)
        initFrom: first to: last
    OL_END_AUTO_CTOR;
}

+ (id) priorityQueueFrom: (OLForwardIterator*)first to: (OLForwardIterator*)last predicate: (OLCodableFunctor<OLBoolBinaryFunction>*)pred
{
    OL_BEGIN_AUTO_CTOR(OLPriorityQueue)
        initFrom: first to: last predicate: pred
    OL_END_AUTO_CTOR;
}

+ (id) priorityQueueWithPredicate: (OLCodableFunctor<OLBoolBinaryFunction>*)pred
{
    OL_BEGIN_AUTO_CTOR(OLPriorityQueue)
        initWithPredicate: pred
    OL_END_AUTO_CTOR;
}

+ (id) priorityQueueWithPriorityQueue: (OLPriorityQueue*)right
{
    OL_BEGIN_AUTO_CTOR(OLPriorityQueue)
        initWithPriorityQueue: right
    OL_END_AUTO_CTOR;
}

- (id) init
{
    OLLess* less = [[[OLAllocator defaultAllocator] allocateObject: [OLLess class]] init];

    [self initWithPredicate: less];
    [[OLAllocator defaultAllocator] deallocateObject: less];
    return self;
}

- (id) initFrom: (OLForwardIterator*)first to: (OLForwardIterator*)last
{
    OLLess* less = [[[OLAllocator defaultAllocator] allocateObject: [OLLess class]] init];

    [self initFrom: first to: last predicate: less];
    [[OLAllocator defaultAllocator] deallocateObject: less];
    return self;
}

- (id) initFrom: (OLForwardIterator*)first to: (OLForwardIterator*)last predicate: (OLCodableFunctor<OLBoolBinaryFunction>*)pred
{
    return [self initFrom: first to: last predicate: pred
        allocator: [OLAllocator defaultAllocator]];
}

- (id) initFrom: (OLForwardIterator*)first to: (OLForwardIterator*)last predicate: (OLCodableFunctor<OLBoolBinaryFunction>*)pred allocator: (OLAllocator*)alloc
{
    OLArrayIterator* begin;
    OLArrayIterator* end;

    [super init];
    predicate = OBJ_RETAIN(pred);
    vector = [[alloc allocateObject: [OLVector class]]
        initFrom: first to: last allocator: alloc];
    begin = [vector beginImpl];
    end = [vector endImpl];
    [OLAlgorithm makeHeapFrom: begin to: end predicate: predicate];
    [[vector allocator] deallocateObject: begin];
    [[vector allocator] deallocateObject: end];
    return self;
}

- (id) initWithCoder: (NSCoder*)decoder
{
    [super init];
    if ([decoder respondsToSelector: @selector(allowsKeyedCoding)] &&
        [decoder allowsKeyedCoding])
    {
        predicate = OBJ_RETAIN([decoder decodeObjectForKey: PREDICATE_KEY]);
        vector = OBJ_RETAIN([decoder decodeObjectForKey: VECTOR_KEY]);
    }
    else
    {
        predicate = OBJ_RETAIN([decoder decodeObject]);
        vector = OBJ_RETAIN([decoder decodeObject]);
    }
    return self;
}

- (id) initWithPredicate: (OLCodableFunctor<OLBoolBinaryFunction>*)pred
{
    return [self initWithPredicate: pred allocator: [OLAllocator defaultAllocator]];
}

- (id) initWithPredicate: (OLCodableFunctor<OLBoolBinaryFunction>*)pred allocator: (OLAllocator*)alloc
{
    [super init];
    predicate = OBJ_RETAIN(pred);
    vector = [[alloc allocateObject: [OLVector class]] init];
    return self;
}

- (id) initWithPriorityQueue: (OLPriorityQueue*)right
{
    [super init];
    predicate = OBJ_RETAIN(right->predicate);
    vector = [right->vector copyWithAllocator: [right->vector allocator]];
    return self;
}

- (void) dealloc
{
    [[vector allocator] deallocateObject: vector];
    OBJ_RELEASE(predicate);
    [super dealloc];
}

- (id) copyWithAllocator: (OLAllocator*)alloc
{
    return [[alloc allocateObject: [OLPriorityQueue class]]
        initWithPriorityQueue: self];
}

- (id) copyWithZone: (NSZone*)zone
{
    return [[OLPriorityQueue allocWithZone: zone]
        initWithPriorityQueue: self];
}

- (BOOL) empty
{
    return [vector empty];
}

- (void) encodeWithCoder: (NSCoder*)encoder
{
    if ([encoder respondsToSelector: @selector(allowsKeyedCoding)] &&
        [encoder allowsKeyedCoding])
    {
        [encoder encodeObject: predicate forKey: PREDICATE_KEY];
        [encoder encodeObject: vector forKey: VECTOR_KEY];
    }
    else
    {
        [encoder encodeObject: predicate];
        [encoder encodeObject: vector];
    }
}

- (OLAllocator*) allocator
{
    return [vector allocator];
}

- (BOOL) isEqual: (id)object
{
    return [object isKindOfClass: [OLPriorityQueue class]] &&
           [((OLPriorityQueue*)object)->vector isEqual: vector];
}

- (void) pop
{
    OLArrayIterator* begin = [vector beginImpl];
    OLArrayIterator* end = [vector endImpl];

    [OLAlgorithm popHeapFrom: begin to: end predicate: predicate];
    [[vector allocator] deallocateObject: begin];
    [[vector allocator] deallocateObject: end];
    [vector popBack];
}

- (void) push: (id)object
{
    OLArrayIterator* begin;
    OLArrayIterator* end;

    [vector pushBack: object];
    begin = [vector beginImpl];
    end = [vector endImpl];
    [OLAlgorithm pushHeapFrom: begin to: end predicate: predicate];
    [[vector allocator] deallocateObject: begin];
    [[vector allocator] deallocateObject: end];
}

- (unsigned) size
{
    return [vector size];
}

- (id) top
{
    return [vector front];
}

@end
