//
// $Id: Stack.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 "Stack.h"
#import "ContainerCoder.h"
#import "Constants.h"
#import <Foundation/NSCoder.h>

@implementation OLStack

+ (id) stack
{
    OL_BEGIN_AUTO_CTOR(OLStack)
        init
    OL_END_AUTO_CTOR;
}

+ (id) stackWithStack: (OLStack*)right
{
    OL_BEGIN_AUTO_CTOR(OLStack)
        initWithStack: right
    OL_END_AUTO_CTOR;
}

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

- (id) initWithAllocator: (OLAllocator*)alloc
{
    [super init];
    deque = [[alloc allocateObject: [OLDeque class]] initWithAllocator: alloc];
    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) initWithStack: (OLStack*)stack
{
    [super init];
    deque = [stack->deque copyWithAllocator: [stack->deque allocator]];
    return self;
}

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

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

- (id) copyWithZone: (NSZone*)zone
{
    return [[OLStack allocWithZone: zone]
        initWithStack: 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];
    }
}

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

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

}

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

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

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

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

@end
