1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     ut_ResDictionary.cpp
4 
5   Copyright (C)2009-2010 Nintendo Co., Ltd./HAL Laboratory, Inc.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Revision: 23585 $
14  *---------------------------------------------------------------------------*/
15 
16 #include "precompiled.h"
17 
18 #include <nw/ut/ut_ResDictionary.h>
19 
20 namespace nw {
21 namespace ut {
22 
23 
24 s32
GetIndex(const char * key) const25 ResDicLinear::GetIndex(const char* key) const
26 {
27     if ( (! this->IsValid()) || (! key) ) { return -1; }
28 
29     const ResDicLinearData::ResDicNodeData* x = ref().data;
30 
31     for ( int i = 0; i < s32(ref().numData); ++i )
32     {
33         if ( x->toName.offset != 0 && std::strcmp(key, x->toName.to_ptr()) == 0 )
34         {
35             return static_cast<s32>( i );
36         }
37         ++x;
38     }
39     return -1;
40 }
41 
42 
43 ResDicPatriciaData::ResDicNodeData*
Get(const ResName rhs) const44 ResDicPatricia::Get(const ResName rhs) const
45 {
46     size_t len = rhs.GetLength();
47     const char* s = rhs.GetName();
48     const ResDataType& r = ref();
49 
50     const ResDicPatriciaData::ResDicNodeData* p;
51     const ResDicPatriciaData::ResDicNodeData* x;
52 
53     p = &r.data[0];
54     x = &r.data[p->idxLeft];
55 
56     while(p->ref > x->ref)
57     {
58         p = x;
59         // ビットが1なら右、0なら左をたどる
60 
61         u32 wd = u32(x->ref) >> 3;
62         u32 pos = u32(x->ref) & 7;
63 
64         if (wd < len && ((s[wd] >> pos) & 1))
65         {
66             x = &r.data[x->idxRight];
67         }
68         else
69         {
70             x = &r.data[x->idxLeft];
71         }
72     }
73 
74     if (rhs == ResName(((u8*)&r + x->ofsString - sizeof(u32))))
75     {
76         return const_cast<ResDicPatriciaData::ResDicNodeData*>(x);
77     }
78     return NULL;
79 }
80 
81 ResDicPatriciaData::ResDicNodeData*
Get(const char * s,size_t len) const82 ResDicPatricia::Get(const char* s, size_t len) const
83 {
84     const ResDicPatriciaData::ResDicNodeData* p;
85     const ResDicPatriciaData::ResDicNodeData* x;
86     const ResDataType& r = this->ref();
87 
88     p = &r.data[0];
89     x = &r.data[p->idxLeft];
90 
91     while(p->ref > x->ref)
92     {
93         p = x;
94         // ビットが1なら右、0なら左をたどる
95 
96         u32 wd = u32(x->ref) >> 3;
97         u32 pos = u32(x->ref) & 7;
98 
99         if (wd < len && ((s[wd] >> pos) & 1))
100         {
101             x = &r.data[x->idxRight];
102         }
103         else
104         {
105             x = &r.data[x->idxLeft];
106         }
107     }
108 
109     if (x->ofsString != 0 && !std::strcmp(s, x->ofsString.to_ptr<char>()))
110     {
111         return const_cast<ResDicPatriciaData::ResDicNodeData*>(x);
112     }
113     return NULL;
114 }
115 
116 
117 
118 /* ------------------------------------------------------------------------
119     ResDic
120    ------------------------------------------------------------------------ */
121 
122 } /* namespace ut */
123 } /* namespace nw */
124 
125