2010-09-22 12:34:35 +00:00
|
|
|
/**
|
|
|
|
Copyright (C) 2010 Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
Written by: Richard Frith-Macdonald <rfm@gnu.org>
|
|
|
|
Date: September 2010
|
|
|
|
|
|
|
|
This file is part of the Performance Library.
|
|
|
|
|
|
|
|
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 3 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 USA.
|
|
|
|
*/
|
|
|
|
#import <Foundation/NSException.h>
|
|
|
|
#import "GSLinkedList.h"
|
|
|
|
|
2010-09-29 14:04:18 +00:00
|
|
|
@implementation GSListLink
|
|
|
|
|
|
|
|
- (void) dealloc
|
2010-09-22 12:34:35 +00:00
|
|
|
{
|
2010-09-29 14:04:18 +00:00
|
|
|
NSAssert(nil != owner, NSInternalInconsistencyException);
|
|
|
|
[item release];
|
|
|
|
[super dealloc];
|
2010-09-22 12:34:35 +00:00
|
|
|
}
|
|
|
|
|
2010-09-29 14:04:18 +00:00
|
|
|
- (id) initCircular
|
2010-09-22 12:34:35 +00:00
|
|
|
{
|
2010-09-29 14:04:18 +00:00
|
|
|
if ((self = [super init]) != nil)
|
2010-09-22 12:34:35 +00:00
|
|
|
{
|
2010-09-29 14:04:18 +00:00
|
|
|
next = previous = self;
|
2010-09-22 12:34:35 +00:00
|
|
|
}
|
2010-09-29 14:04:18 +00:00
|
|
|
return self;
|
2010-09-22 12:34:35 +00:00
|
|
|
}
|
|
|
|
|
2010-09-29 14:04:18 +00:00
|
|
|
- (id) item
|
2010-09-22 12:34:35 +00:00
|
|
|
{
|
2010-09-29 14:04:18 +00:00
|
|
|
return item;
|
2010-09-22 12:34:35 +00:00
|
|
|
}
|
|
|
|
|
2010-09-29 14:04:18 +00:00
|
|
|
- (GSListLink*) next
|
2010-09-22 12:34:35 +00:00
|
|
|
{
|
2010-09-29 14:04:18 +00:00
|
|
|
if (next == self)
|
|
|
|
{
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
return next;
|
2010-09-22 12:34:35 +00:00
|
|
|
}
|
|
|
|
|
2010-09-29 14:04:18 +00:00
|
|
|
- (GSLinkedList*) owner
|
2010-09-22 12:34:35 +00:00
|
|
|
{
|
2010-09-29 14:04:18 +00:00
|
|
|
return owner;
|
|
|
|
}
|
2010-09-22 12:34:35 +00:00
|
|
|
|
2010-09-29 14:04:18 +00:00
|
|
|
- (GSListLink*) previous
|
|
|
|
{
|
|
|
|
if (previous == self)
|
2010-09-22 12:34:35 +00:00
|
|
|
{
|
2010-09-29 14:04:18 +00:00
|
|
|
return nil;
|
2010-09-22 12:34:35 +00:00
|
|
|
}
|
2010-09-29 14:04:18 +00:00
|
|
|
return previous;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setItem: (NSObject*)anItem
|
|
|
|
{
|
|
|
|
id o = item;
|
|
|
|
|
|
|
|
item = [anItem retain];
|
|
|
|
[o release];
|
2010-09-22 12:34:35 +00:00
|
|
|
}
|
|
|
|
|
2010-09-29 14:04:18 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
@implementation GSLinkedList
|
|
|
|
- (void) append: (GSListLink*)link
|
2010-09-22 12:34:35 +00:00
|
|
|
{
|
2010-09-29 14:04:18 +00:00
|
|
|
if (nil == link)
|
2010-09-22 12:34:35 +00:00
|
|
|
{
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
format: @"[%@-%@] nil argument",
|
|
|
|
NSStringFromClass([self class]), NSStringFromSelector(_cmd)];
|
|
|
|
}
|
2010-09-29 14:04:18 +00:00
|
|
|
if (self == link->owner)
|
2010-09-22 12:34:35 +00:00
|
|
|
{
|
2010-09-29 14:04:18 +00:00
|
|
|
if (link != tail)
|
|
|
|
{
|
|
|
|
GSLinkedListRemove(link, self);
|
|
|
|
GSLinkedListInsertAfter(link, self, tail);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (nil != link->owner || nil != link->next || nil != link->previous)
|
|
|
|
{
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
format: @"[%@-%@] other link is still in a list",
|
|
|
|
NSStringFromClass([self class]), NSStringFromSelector(_cmd)];
|
|
|
|
}
|
|
|
|
GSLinkedListInsertAfter(link, self, tail);
|
|
|
|
[link retain];
|
2010-09-22 12:34:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-29 14:04:18 +00:00
|
|
|
- (NSUInteger) count
|
2010-09-22 12:34:35 +00:00
|
|
|
{
|
2010-09-29 14:04:18 +00:00
|
|
|
return count;
|
2010-09-22 12:34:35 +00:00
|
|
|
}
|
|
|
|
|
2010-09-29 14:04:18 +00:00
|
|
|
- (void) dealloc
|
2010-09-22 12:34:35 +00:00
|
|
|
{
|
2010-09-29 14:04:18 +00:00
|
|
|
count = 0;
|
|
|
|
tail = nil;
|
|
|
|
while (nil != head)
|
|
|
|
{
|
|
|
|
GSListLink *link = head;
|
|
|
|
|
|
|
|
head = link->next;
|
|
|
|
head->next = head->previous = nil;
|
|
|
|
head->owner = nil;
|
|
|
|
[head release];
|
|
|
|
}
|
|
|
|
[super dealloc];
|
2010-09-22 12:34:35 +00:00
|
|
|
}
|
|
|
|
|
2010-09-29 14:04:18 +00:00
|
|
|
- (void) empty
|
2010-09-22 12:34:35 +00:00
|
|
|
{
|
2010-09-29 14:04:18 +00:00
|
|
|
GSListLink *link;
|
|
|
|
|
|
|
|
while (nil != (link = head))
|
|
|
|
{
|
|
|
|
head = link->next;
|
|
|
|
link->owner = nil;
|
|
|
|
link->next = link->previous = nil;
|
|
|
|
[link release];
|
|
|
|
}
|
|
|
|
tail = nil;
|
|
|
|
count = 0;
|
2010-09-22 12:34:35 +00:00
|
|
|
}
|
|
|
|
|
2010-09-29 14:04:18 +00:00
|
|
|
- (GSListLink*) findEqual: (NSObject*)object
|
|
|
|
from: (GSListLink*)start
|
|
|
|
back: (BOOL)aFlag
|
2010-09-22 12:34:35 +00:00
|
|
|
{
|
2010-09-29 14:04:18 +00:00
|
|
|
if (nil != start && start->owner != self)
|
|
|
|
{
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
format: @"[%@-%@] start link is not in this list",
|
|
|
|
NSStringFromClass([self class]), NSStringFromSelector(_cmd)];
|
|
|
|
}
|
|
|
|
return GSLinkedListFindEqual(object, self, start, aFlag);
|
2010-09-22 12:34:35 +00:00
|
|
|
}
|
|
|
|
|
2010-09-29 14:04:18 +00:00
|
|
|
- (GSListLink*) findIdentical: (NSObject*)object
|
|
|
|
from: (GSListLink*)start
|
|
|
|
back: (BOOL)aFlag
|
2010-09-22 12:34:35 +00:00
|
|
|
{
|
2010-09-29 14:04:18 +00:00
|
|
|
if (nil != start && start->owner != self)
|
|
|
|
{
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
format: @"[%@-%@] start link is not in this list",
|
|
|
|
NSStringFromClass([self class]), NSStringFromSelector(_cmd)];
|
|
|
|
}
|
|
|
|
return GSLinkedListFindIdentical(object, self, start, aFlag);
|
|
|
|
}
|
2010-09-22 12:34:35 +00:00
|
|
|
|
2010-09-29 14:04:18 +00:00
|
|
|
- (GSListLink*) head
|
|
|
|
{
|
|
|
|
return head;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) insert: (GSListLink*)link after: (GSListLink*)at
|
|
|
|
{
|
|
|
|
if (nil == link)
|
|
|
|
{
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
format: @"[%@-%@] nil link argument",
|
|
|
|
NSStringFromClass([self class]), NSStringFromSelector(_cmd)];
|
|
|
|
}
|
|
|
|
if (nil == at)
|
|
|
|
{
|
|
|
|
at = tail;
|
|
|
|
}
|
|
|
|
if (at->owner != self)
|
|
|
|
{
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
format: @"[%@-%@] 'at' link is not in this list",
|
|
|
|
NSStringFromClass([self class]), NSStringFromSelector(_cmd)];
|
|
|
|
}
|
|
|
|
if (at == link)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (link->owner == self)
|
|
|
|
{
|
|
|
|
GSLinkedListRemove(link, self);
|
|
|
|
GSLinkedListInsertAfter(link, self, at);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (nil != link->owner || nil != link->next || nil != link->previous)
|
|
|
|
{
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
format: @"[%@-%@] other link is still in a list",
|
|
|
|
NSStringFromClass([self class]), NSStringFromSelector(_cmd)];
|
|
|
|
}
|
|
|
|
GSLinkedListInsertAfter(link, self, at);
|
|
|
|
[link retain];
|
|
|
|
}
|
2010-09-22 12:34:35 +00:00
|
|
|
}
|
|
|
|
|
2010-09-29 14:04:18 +00:00
|
|
|
- (void) insert: (GSListLink*)link before: (GSListLink*)at
|
2010-09-22 12:34:35 +00:00
|
|
|
{
|
2010-09-29 14:04:18 +00:00
|
|
|
if (nil == link)
|
|
|
|
{
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
format: @"[%@-%@] nil link argument",
|
|
|
|
NSStringFromClass([self class]), NSStringFromSelector(_cmd)];
|
|
|
|
}
|
|
|
|
if (nil == at)
|
|
|
|
{
|
|
|
|
at = head;
|
|
|
|
}
|
|
|
|
if (at->owner != self)
|
|
|
|
{
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
format: @"[%@-%@] 'at' link is not in this list",
|
|
|
|
NSStringFromClass([self class]), NSStringFromSelector(_cmd)];
|
|
|
|
}
|
|
|
|
if (at == link)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (link->owner == self)
|
|
|
|
{
|
|
|
|
GSLinkedListRemove(link, self);
|
|
|
|
GSLinkedListInsertBefore(link, self, at);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (nil != link->owner || nil != link->next || nil != link->previous)
|
|
|
|
{
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
format: @"[%@-%@] other link is still in a list",
|
|
|
|
NSStringFromClass([self class]), NSStringFromSelector(_cmd)];
|
|
|
|
}
|
|
|
|
GSLinkedListInsertBefore(link, self, at);
|
|
|
|
[link retain];
|
|
|
|
}
|
|
|
|
}
|
2010-09-22 12:34:35 +00:00
|
|
|
|
2010-09-29 14:04:18 +00:00
|
|
|
- (void) removeLink: (GSListLink*)link
|
|
|
|
{
|
|
|
|
if (nil == link)
|
|
|
|
{
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
format: @"[%@-%@] nil link argument",
|
|
|
|
NSStringFromClass([self class]), NSStringFromSelector(_cmd)];
|
|
|
|
}
|
|
|
|
if (link->owner != self)
|
2010-09-22 12:34:35 +00:00
|
|
|
{
|
2010-09-29 14:04:18 +00:00
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
format: @"[%@-%@] link is not in this list",
|
|
|
|
NSStringFromClass([self class]), NSStringFromSelector(_cmd)];
|
2010-09-22 12:34:35 +00:00
|
|
|
}
|
2010-09-29 14:04:18 +00:00
|
|
|
GSLinkedListRemove(link, self);
|
|
|
|
[link release];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (GSListLink*) tail
|
|
|
|
{
|
|
|
|
return tail;
|
2010-09-22 12:34:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2010-09-29 17:32:34 +00:00
|
|
|
GSListLink*
|
|
|
|
GSLinkedListFindEqual(NSObject *object, GSLinkedList *list,
|
|
|
|
GSListLink *from, BOOL back)
|
|
|
|
{
|
|
|
|
if (nil == from)
|
|
|
|
{
|
|
|
|
if (YES == back)
|
|
|
|
{
|
|
|
|
from = list->tail;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
from = list->head;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (nil != object)
|
|
|
|
{
|
|
|
|
BOOL (*imp)(id, SEL, id);
|
|
|
|
|
|
|
|
imp = (BOOL(*)(id,SEL,id))[object methodForSelector: @selector(isEqual:)];
|
|
|
|
if (YES == back)
|
|
|
|
{
|
|
|
|
while (nil != from)
|
|
|
|
{
|
|
|
|
if (YES == (*imp)(object, @selector(isEqual:), from->item))
|
|
|
|
{
|
|
|
|
return from;
|
|
|
|
}
|
|
|
|
from = from->previous;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while (nil != from)
|
|
|
|
{
|
|
|
|
if (YES == (*imp)(object, @selector(isEqual:), from->item))
|
|
|
|
{
|
|
|
|
return from;
|
|
|
|
}
|
|
|
|
from = from->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
GSListLink*
|
|
|
|
GSLinkedListFindIdentical(NSObject *object, GSLinkedList *list,
|
|
|
|
GSListLink *from, BOOL back)
|
|
|
|
{
|
|
|
|
if (nil == from)
|
|
|
|
{
|
|
|
|
if (YES == back)
|
|
|
|
{
|
|
|
|
from = list->tail;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
from = list->head;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (YES == back)
|
|
|
|
{
|
|
|
|
while (nil != from)
|
|
|
|
{
|
|
|
|
if (object == from->item)
|
|
|
|
{
|
|
|
|
return from;
|
|
|
|
}
|
|
|
|
from = from->previous;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while (nil != from)
|
|
|
|
{
|
|
|
|
if (object == from->item)
|
|
|
|
{
|
|
|
|
return from;
|
|
|
|
}
|
|
|
|
from = from->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2010-09-22 12:34:35 +00:00
|
|
|
void
|
2010-09-29 14:04:18 +00:00
|
|
|
GSLinkedListInsertBefore(GSListLink *link, GSLinkedList *list, GSListLink *at)
|
2010-09-22 12:34:35 +00:00
|
|
|
{
|
2010-09-29 14:04:18 +00:00
|
|
|
if (nil == list->head)
|
2010-09-22 12:34:35 +00:00
|
|
|
{
|
2010-09-29 14:04:18 +00:00
|
|
|
list->head = list->tail = link;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
link->previous = at->previous;
|
|
|
|
if (nil == link->previous)
|
|
|
|
{
|
|
|
|
list->head = link;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
link->previous->next = link;
|
|
|
|
}
|
|
|
|
at->previous = link;
|
|
|
|
link->next = at;
|
2010-09-22 12:34:35 +00:00
|
|
|
}
|
2010-09-29 14:04:18 +00:00
|
|
|
link->owner = list;
|
|
|
|
list->count++;
|
2010-09-22 12:34:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-09-29 14:04:18 +00:00
|
|
|
GSLinkedListInsertAfter(GSListLink *link, GSLinkedList *list, GSListLink *at)
|
2010-09-22 12:34:35 +00:00
|
|
|
{
|
2010-09-29 14:04:18 +00:00
|
|
|
if (nil == list->head)
|
2010-09-22 12:34:35 +00:00
|
|
|
{
|
2010-09-29 14:04:18 +00:00
|
|
|
list->head = list->tail = link;
|
2010-09-22 12:34:35 +00:00
|
|
|
}
|
2010-09-29 14:04:18 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
link->next = at->next;
|
|
|
|
if (nil == link->next)
|
|
|
|
{
|
|
|
|
list->tail = link;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
link->next->previous = link;
|
|
|
|
}
|
|
|
|
at->next = link;
|
|
|
|
link->previous = at;
|
|
|
|
}
|
|
|
|
link->owner = list;
|
|
|
|
list->count++;
|
2010-09-22 12:34:35 +00:00
|
|
|
}
|
|
|
|
|
2010-09-29 14:04:18 +00:00
|
|
|
void
|
|
|
|
GSLinkedListRemove(GSListLink *link, GSLinkedList *list)
|
2010-09-22 12:34:35 +00:00
|
|
|
{
|
2010-09-29 14:04:18 +00:00
|
|
|
if (list->head == link)
|
2010-09-22 12:34:35 +00:00
|
|
|
{
|
2010-09-29 14:04:18 +00:00
|
|
|
list->head = link->next;
|
|
|
|
if (nil != list->head)
|
|
|
|
{
|
|
|
|
list->head->previous = nil;
|
|
|
|
}
|
2010-09-22 12:34:35 +00:00
|
|
|
}
|
2010-09-29 14:04:18 +00:00
|
|
|
else
|
2010-09-22 12:34:35 +00:00
|
|
|
{
|
2010-09-29 14:04:18 +00:00
|
|
|
link->previous->next = link->next;
|
2010-09-22 12:34:35 +00:00
|
|
|
}
|
2010-09-29 14:04:18 +00:00
|
|
|
if (list->tail == link)
|
|
|
|
{
|
|
|
|
list->tail = link->previous;
|
|
|
|
if (nil != list->tail)
|
|
|
|
{
|
|
|
|
list->tail->next = nil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
link->next->previous = link->previous;
|
|
|
|
}
|
|
|
|
link->next = link->previous = nil;
|
|
|
|
link->owner = nil;
|
|
|
|
list->count--;
|
2010-09-22 12:34:35 +00:00
|
|
|
}
|
|
|
|
|
2010-09-29 17:32:34 +00:00
|
|
|
extern void
|
|
|
|
GSLinkedListMoveToHead(GSListLink *link, GSLinkedList *list)
|
2010-09-22 12:34:35 +00:00
|
|
|
{
|
2010-09-29 17:32:34 +00:00
|
|
|
if (link != list->head)
|
2010-09-29 14:04:18 +00:00
|
|
|
{
|
2010-09-29 17:32:34 +00:00
|
|
|
if (link == list->tail)
|
2010-09-29 14:04:18 +00:00
|
|
|
{
|
2010-09-29 17:32:34 +00:00
|
|
|
list->tail = link->previous;
|
|
|
|
list->tail->next = nil;
|
2010-09-29 14:04:18 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-09-29 17:32:34 +00:00
|
|
|
link->next->previous = link->previous;
|
|
|
|
link->previous->next = link->next;
|
2010-09-29 14:04:18 +00:00
|
|
|
}
|
2010-09-29 17:32:34 +00:00
|
|
|
link->next = list->head;
|
|
|
|
link->previous = nil;
|
|
|
|
list->head = link;
|
2010-09-29 14:04:18 +00:00
|
|
|
}
|
2010-09-22 12:34:35 +00:00
|
|
|
}
|
|
|
|
|
2010-09-29 17:32:34 +00:00
|
|
|
extern void
|
|
|
|
GSLinkedListMoveToTail(GSListLink *link, GSLinkedList *list)
|
2010-09-22 12:34:35 +00:00
|
|
|
{
|
2010-09-29 17:32:34 +00:00
|
|
|
if (link != list->tail)
|
2010-09-29 14:04:18 +00:00
|
|
|
{
|
2010-09-29 17:32:34 +00:00
|
|
|
if (link == list->head)
|
2010-09-29 14:04:18 +00:00
|
|
|
{
|
2010-09-29 17:32:34 +00:00
|
|
|
list->head = link->next;
|
|
|
|
list->head->previous = nil;
|
2010-09-29 14:04:18 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-09-29 17:32:34 +00:00
|
|
|
link->next->previous = link->previous;
|
|
|
|
link->previous->next = link->next;
|
2010-09-29 14:04:18 +00:00
|
|
|
}
|
2010-09-29 17:32:34 +00:00
|
|
|
link->next = nil;
|
|
|
|
link->previous = list->tail;
|
|
|
|
list->tail = link;
|
2010-09-29 14:04:18 +00:00
|
|
|
}
|
2010-09-22 12:34:35 +00:00
|
|
|
}
|
|
|
|
|