Coder Social home page Coder Social logo

ndtrie's People

Contributors

nathanday avatar rsfinn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ndtrie's Issues

Improve memory usage

Each node currently has two mallocs, one for the node and one for its array of children, the node and children can be merge into a single malloc.

Compiler Warnings

Implicit conversion from BOOL to init.
Use of functions that have not been previously declared, need to be changed to static.

Unique objects result

Sometime you want to have more than one way to find the same object, some way to handle the duplicates would be nice, either everyUniqueObject... version of the everyObject... methods or perhaps a setObject:froKeys: equivalent which took multiple keys.

The method removeObjectForKey looks like logically wrong.

Maybe I misunderstand how Trie is commonly defined, however, I think NDTrie has incorrectly operation in method removeObjectForKey.

I wrote some simple unit tests. So take a look at those comments in code.

@implementation NDTrieTests
{
    NDMutableTrie *_trie;
}

- (void)setUp
{
    [super setUp];

    NDMutableTrie *trie = [NDMutableTrie trie];
    [trie addString:@"12"];
    [trie addString:@"1234"];
    _trie = trie;
}

- (void)tearDown
{    
    [super tearDown];
}

- (void)testRemoveKey
{
    [_trie addString:@"123"];
    NSLog(@"trie: %@", _trie); // trie: { 12, 123, 1234 }

    [_trie removeObjectForKey:@"1234"]; // incorrectly removes all objects...

    id object = [_trie objectForKey:@"1234"];
    STAssertNil(object, @"no object for 1234");

    object = [_trie objectForKey:@"12"];
    STAssertEqualObjects(object, @"12", @"Got object for 12"); // fail..

    object = [_trie objectForKey:@"123"];
    STAssertEqualObjects(object, @"123", @"Got object for 123"); // fail..

    NSLog(@"testRemoveKey: %@", _trie);
    // testRemoveKey: { }
    // where it should be { 12, 123 }
}

- (void)testRemoveKeyHasChildren
{
    [_trie addString:@"123"];
    NSLog(@"trie: %@", _trie); // trie: { 12, 123, 1234 }

    [_trie removeObjectForKey:@"123"]; // has no effect...

    id object = [_trie objectForKey:@"123"];
    STAssertNil(object, @"no object for 123"); // fail..

    object = [_trie objectForKey:@"1234"];
    STAssertEqualObjects(object, @"1234", @"Got object for 1234");

    NSLog(@"testRemoveKeyHasChildren: %@", _trie);
    // testRemoveKeyHasChildren: { 12, 123, 1234 }
    // where it should be { 12, 1234 }
}

@end

Won't build on iPhone Simulator

It looks like OBJC2 doesn't get recognized on the iPhone simulator, beats me why, and it falls to the implementation without NSFastEnumerations - which isn't building. I get the following errors, amongst others:

/NDTrie.m:159:0 /NDTrie.m:159: error: too few arguments to function 'setObjectForKey'
/NDTrie.m:516:0 /NDTrie.m:516: warning: class 'NDTrie' does not fully implement the 'NSFastEnumeration' protocol

If I just remove the checks for OBJC2_ and remove the legacy code, it builds and works.

Also, I found the following post which might help understand why this is happening on the Simulator (last one on the page):
http://discussions.apple.com/thread.jspa?threadID=1952943

Prefix methods are broken with case insensitive tries

The "prefix" methods (e.g. -everyObjectForKeyWithPrefix:) appear not to work properly when the trie is created as case insensitive. For example, in the test project's main.m (function testSetOne()) replace line 34 from:

    NDTrie* theTrie = [NDTrie trieWithArray:theTestTrueStrings];

to:

    NDTrie* theTrie = [[NDTrie alloc] initWithCaseInsensitive:YES array:theTestTrueStrings];

This should not change the behavior of the test cases (in particular, theTestTrueStrings contains only lowercase strings), but in fact the following assertions will fail:

  • line 57 (testing -everyObjectForKeyWithPrefix:);
  • line 60 (ditto);
  • line 210 (testing -objectEnumeratorForKeyWithPrefix:);
  • line 220 (ditto).

Inspecting the return values, it appears as though the prefix checking no longer works, and only full string matches succeed. For example, in line 57 theTempArray contains @[ "cat" ] instead of @[@"cat", @"caterpillar", @"catalog"] as expected.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.