1// 2// ANTLRUniqueIDMap.m 3// ANTLR 4// 5// Created by Alan Condit on 7/7/10. 6// [The "BSD licence"] 7// Copyright (c) 2010 Alan Condit 8// All rights reserved. 9// 10// Redistribution and use in source and binary forms, with or without 11// modification, are permitted provided that the following conditions 12// are met: 13// 1. Redistributions of source code must retain the above copyright 14// notice, this list of conditions and the following disclaimer. 15// 2. Redistributions in binary form must reproduce the above copyright 16// notice, this list of conditions and the following disclaimer in the 17// documentation and/or other materials provided with the distribution. 18// 3. The name of the author may not be used to endorse or promote products 19// derived from this software without specific prior written permission. 20// 21// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24// IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 32#import "ANTLRUniqueIDMap.h" 33#import "ANTLRTree.h" 34 35@implementation ANTLRUniqueIDMap 36@synthesize lastHash; 37 38+(id)newANTLRUniqueIDMap 39{ 40 ANTLRUniqueIDMap *aNewANTLRUniqueIDMap; 41 42 aNewANTLRUniqueIDMap = [[ANTLRUniqueIDMap alloc] init]; 43 return( aNewANTLRUniqueIDMap ); 44} 45 46+(id)newANTLRUniqueIDMapWithLen:(NSInteger)aBuffSize 47{ 48 ANTLRUniqueIDMap *aNewANTLRUniqueIDMap; 49 50 aNewANTLRUniqueIDMap = [[ANTLRUniqueIDMap alloc] initWithLen:aBuffSize]; 51 return( aNewANTLRUniqueIDMap ); 52} 53 54-(id)init 55{ 56 NSInteger idx; 57 58 if ((self = [super initWithLen:HASHSIZE]) != nil) { 59 fNext = nil; 60 for( idx = 0; idx < HASHSIZE; idx++ ) { 61 ptrBuffer[idx] = nil; 62 } 63 } 64 return( self ); 65} 66 67-(id)initWithLen:(NSInteger)aBuffSize 68{ 69 if ((self = [super initWithLen:aBuffSize]) != nil) { 70 } 71 return( self ); 72} 73 74-(void)dealloc 75{ 76#ifdef DEBUG_DEALLOC 77 NSLog( @"called dealloc in ANTLRUniqueIDMap" ); 78#endif 79 ANTLRNodeMapElement *tmp, *rtmp; 80 NSInteger idx; 81 82 if ( self.fNext != nil ) { 83 for( idx = 0; idx < HASHSIZE; idx++ ) { 84 tmp = ptrBuffer[idx]; 85 while ( tmp ) { 86 rtmp = tmp; 87 tmp = (ANTLRNodeMapElement *)tmp.fNext; 88 [rtmp release]; 89 } 90 } 91 } 92 [super dealloc]; 93} 94 95-(void)deleteANTLRUniqueIDMap:(ANTLRNodeMapElement *)np 96{ 97 ANTLRNodeMapElement *tmp, *rtmp; 98 NSInteger idx; 99 100 if ( self.fNext != nil ) { 101 for( idx = 0; idx < HASHSIZE; idx++ ) { 102 tmp = ptrBuffer[idx]; 103 while ( tmp ) { 104 rtmp = tmp; 105 tmp = tmp.fNext; 106 [rtmp release]; 107 } 108 } 109 } 110} 111 112- (void)clear 113{ 114 ANTLRNodeMapElement *tmp, *rtmp; 115 NSInteger idx; 116 117 for( idx = 0; idx < HASHSIZE; idx++ ) { 118 tmp = ptrBuffer[idx]; 119 while ( tmp ) { 120 rtmp = tmp; 121 tmp = [tmp getfNext]; 122 [rtmp release]; 123 } 124 ptrBuffer[idx] = nil; 125 } 126} 127 128- (NSInteger)count 129{ 130 id anElement; 131 NSInteger aCnt = 0; 132 133 for (int i = 0; i < BuffSize; i++) { 134 if ((anElement = ptrBuffer[i]) != nil) { 135 aCnt += (NSInteger)[anElement count]; 136 } 137 } 138 return aCnt; 139} 140 141- (NSInteger)size 142{ 143 return BuffSize; 144} 145 146-(void)delete_chain:(ANTLRNodeMapElement *)np 147{ 148 if ( np.fNext != nil ) 149 [self delete_chain:np.fNext]; 150 [np release]; 151} 152 153- (id)getNode:(id<ANTLRBaseTree>)aNode 154{ 155 ANTLRNodeMapElement *np; 156 NSInteger idx; 157 158 idx = [(id<ANTLRBaseTree>)aNode type]; 159 np = ptrBuffer[idx]; 160 while ( np != nil ) { 161 if (np.node == aNode) { 162 return( np.index ); 163 } 164 np = np.fNext; 165 } 166 return( nil ); 167} 168 169- (void)putID:(id)anID Node:(id<ANTLRBaseTree>)aNode 170{ 171 ANTLRNodeMapElement *np, *np1; 172 NSInteger idx; 173 174 idx = [(id<ANTLRBaseTree>)aNode type]; 175 idx %= HASHSIZE; 176 np = [[ANTLRNodeMapElement newANTLRNodeMapElementWithIndex:anID Node:aNode] retain]; 177 np1 = ptrBuffer[idx]; 178 np.fNext = np1; 179 ptrBuffer[idx] = np; 180 return; 181} 182 183 184@end 185