469,592 Members | 1,983 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,592 developers. It's quick & easy.

array inside a array!?

I was going through an Boyer-Moore-Horspool pattern match, I saw a
array inside a array, like the below,

skip[pat[i] ] = patlen - i - 1;

The array is decleared as

int skip[UCHAR_MAX+1];

unsigned char *pat;
I have seen function call inside the array such as calling strlen()
inside a array, but the above one is
strange and new to me as a beginner. Can anybody spend a little time to
explain the above.
BTW, if this a FAQ please refer the question number.

--
"Combination is the heart of chess"
A.Alekhine
Mail to:
sathyashrayan AT gmail DOT com
(AT = @ and DOT = .)
Nov 14 '05 #1
9 2958


sathya wrote:
I was going through an Boyer-Moore-Horspool pattern match, I saw a
array inside a array, like the below,

skip[pat[i] ] = patlen - i - 1;

The array is decleared as

int skip[UCHAR_MAX+1];

unsigned char *pat;

I have seen function call inside the array such as calling strlen()
inside a array, but the above one is
strange and new to me as a beginner. Can anybody spend a little time to
explain the above.
BTW, if this a FAQ please refer the question number.


Sorry I must be more precise in my question. The bellow properties is
well known with the array:

"In an array, LOGICAL ADJACENCY (B follows A) is modeled by
PHYSICAL ADJACENCY (B occurs just after A in memory.)"

As in the case of the above "array inside a array" I cannot figure out
the possibility of logical and
physical adjacency. And is the above kind of "array inside a array" is
UD in std?
--
"Combination is the heart of chess"
A.Alekhine
Mail to:
sathyashrayan AT gmail DOT com
(AT = @ and DOT = .)
Nov 14 '05 #2
sathya wrote:
I was going through an Boyer-Moore-Horspool pattern match, I saw a
array inside a array, like the below,

skip[pat[i] ] = patlen - i - 1;
This is NOT "an array inside an array". This is array indexing where
the index expression is itself an array indexing expression.
I have seen function call inside the array such as calling strlen()
inside a array, but the above one is
strange and new to me as a beginner.


Why? pat[i] is just as much an expression as f(j) or k+1.

--
Chris "electric hedgehog" Dollin
Nov 14 '05 #3
sathya wrote:
I was going through an Boyer-Moore-Horspool pattern match, I saw a
array inside a array, like the below,

skip[pat[i] ] = patlen - i - 1;


I'm not sure about the Boyer-Moore-Horspool pattern match algorithm.

But what the code seems to do is this:

skip[pat[i] ] = patlen - i - 1;

pat[i] gets the ASCII value of the char pointed to by ptr 'pat'.
Index in the array 'skip' to the element (pat[i]), which in this case
is for that particular character, and store whatever 'patlen-i-1' is.
So the ASCII value of that char is actually an index for the skip
array, just like skip[65], 65 ASCII for 'A', would be for char 'A'

pat[i] is actually a number/value which is used as index. The notation
looks like array in array but there's nothing like array in array in C.
Hope that Helps.

Regards,
Taran Tripathi

Nov 14 '05 #4
sathya <sa****@nomail.com> wrote:
I was going through an Boyer-Moore-Horspool pattern match, I saw a
array inside a array, like the below, skip[pat[i] ] = patlen - i - 1; The array is decleared as int skip[UCHAR_MAX+1]; unsigned char *pat;
I have seen function call inside the array such as calling strlen()
inside a array, but the above one is
strange and new to me as a beginner. Can anybody spend a little time to
explain the above.
BTW, if this a FAQ please refer the question number.


I don't think that this requires a FAQ entry. In the above line
"pat[i]" simply evaluates to a number that is used as the index
in the 'skip' array. That's not different from using the result
of a function call or computation as the index. It's basically
the same as writing e.g.

idx = pat[ i ];
skip[ idx ] = patlen - i - 1;

just shorter and does not require an extra variable.

Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #5
On Tue, 14 Dec 2004 17:25:56 +0530
sathya <sa****@nomail.com> wrote:
sathya wrote:
I was going through an Boyer-Moore-Horspool pattern match, I saw
a
array inside a array, like the below,

skip[pat[i] ] = patlen - i - 1;

The array is decleared as

int skip[UCHAR_MAX+1];

unsigned char *pat;

I have seen function call inside the array such as calling strlen()
inside a array, but the above one is
strange and new to me as a beginner. Can anybody spend a little time
to explain the above.
BTW, if this a FAQ please refer the question number.


Sorry I must be more precise in my question. The bellow properties
is well known with the array:

"In an array, LOGICAL ADJACENCY (B follows A) is modeled by
PHYSICAL ADJACENCY (B occurs just after A in memory.)"

As in the case of the above "array inside a array" I cannot figure
out the possibility of logical and
physical adjacency. And is the above kind of "array inside a array"
is UD in std?


It's simple. You do *not* have an array inside an array, you are just
looking in one array to find an index in to the other. It is basically
equivalent to:

int skip[UCHAR_MAX+1];
unsigned char *pat;
unsigned char tmp;

tmp = pat[i];
skip[ tmp ] = patlen - i - 1;

<pedantic>
C does not guarantee physical adjacency, only logical adjacency. For
example, on a system with paged memory one location in the array might
be in physical memory whilst the next location, on the other side of a
page boundary, might have been swapped out to disk and not exist in
physical memory at all.
</pedantic>

If you want to look at formal definitions with respect to C you really
need to look at the C standard in my opinion. Google for N869 to find
the publicly available last draft of the C99 standard.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 14 '05 #6
On Tue, 14 Dec 2004 17:25:56 +0530, sathya wrote:


sathya wrote:
I was going through an Boyer-Moore-Horspool pattern match, I saw a
array inside a array, like the below,

skip[pat[i] ] = patlen - i - 1;
This is equivalent to

char tmp;

tmp = pat[i];
skip[tmp] = patlen - i - 1;
The array is decleared as

int skip[UCHAR_MAX+1];

unsigned char *pat;

I have seen function call inside the array such as calling strlen()
inside a array, but the above one is
strange and new to me as a beginner. Can anybody spend a little time to
explain the above.
The expression between the [] is just a number used to index the array.
There is no real sense that it is "inside" the array, this idea seems to
be more confusing than helpful. See the equivalent code where pat[i]
isn't "inside" anything.
BTW, if this a FAQ please refer the question number.

I doubt it, this is simple expression evaluation.
Sorry I must be more precise in my question. The bellow properties is
well known with the array:

"In an array, LOGICAL ADJACENCY (B follows A) is modeled by
PHYSICAL ADJACENCY (B occurs just after A in memory.)"
If you want a discussion of how the algorithm works a newsgroup like
comp.programming would be appropriate.
As in the case of the above "array inside a array" I cannot figure out
the possibility of logical and
physical adjacency. And is the above kind of "array inside a array" is
UD in std?


The code is perfectly well defined, assuming the variables involved have
sensible values and it is in a sensible context in the rest of the program.
Again, look at the equivalent code, there's nothing unusual happening here.

Lawrence

Nov 14 '05 #7
On 14 Dec 2004 04:07:40 -0800
"Taran" <ta************@honeywell.com> wrote:
sathya wrote:
I was going through an Boyer-Moore-Horspool pattern match, I saw a
array inside a array, like the below,

skip[pat[i] ] = patlen - i - 1;
I'm not sure about the Boyer-Moore-Horspool pattern match algorithm.

But what the code seems to do is this:

skip[pat[i] ] = patlen - i - 1;

pat[i] gets the ASCII value of the char pointed to by ptr 'pat'.


Stop right there. The C standard does NOT mandate ASCII, and C has been
implemented on a number of systems that are not ASCII. All pat[1] does
is get the number stored in pat[i] which may have been generated as
something to do with the execution character set, but it might also
just be being used as a byte array.
Index in the array 'skip' to the element (pat[i]), which in this case
is for that particular character,
What makes you think pat contains character rather than, for example,
bytes of a bitmap graphic?
and store whatever 'patlen-i-1' is.
So the ASCII value of that char is actually an index for the skip
array, just like skip[65], 65 ASCII for 'A', would be for char 'A'
Definitely not as mentioned above. Don't assume that all the world is a
PC not that text is the only thing stored in char arrays.
pat[i] is actually a number/value which is used as index. The notation
looks like array in array but there's nothing like array in array in
C. Hope that Helps.


That is correct.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 14 '05 #8
In article <27************@brenda.flash-gordon.me.uk>,
Flash Gordon <sp**@flash-gordon.me.uk> wrote:
What makes you think pat contains character rather than, for example,
bytes of a bitmap graphic?


The fact that he's applying the Boyer-Moore algorithm to it?

Of course, you can apply it to any sequence, but it's common to think
of characters in this context.

-- Richard
Nov 14 '05 #9
On 14 Dec 2004 14:27:19 GMT
ri*****@cogsci.ed.ac.uk (Richard Tobin) wrote:
In article <27************@brenda.flash-gordon.me.uk>,
Flash Gordon <sp**@flash-gordon.me.uk> wrote:
What makes you think pat contains character rather than, for example,
bytes of a bitmap graphic?


The fact that he's applying the Boyer-Moore algorithm to it?

Of course, you can apply it to any sequence, but it's common to think
of characters in this context.


Well, this is comp.lang.c not an algorithms group and I happen not to
have needed to do pattern matching on text and I know that text
processing is not the only place one does pattern matching. Taran stated
that he did not know the Boyer-Moore-Horspool algorithm, therefor s/he
obviously had no way of knowing that it was likely to be a character,
and all the references to ASCII showed a lack of understanding of what a
char is in C.

So I stand by my question, how did Taran (not you or the OP) know that
pat contained characters?

I add the further question, how does anyone other than the OP know that
the execution character set is ASCII?
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 14 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Marco | last post: by
4 posts views Thread by Mark Hannon | last post: by
6 posts views Thread by Abhishek Srivastava | last post: by
9 posts views Thread by shaun | last post: by
7 posts views Thread by Jim Carlock | last post: by
8 posts views Thread by dan.winsor | last post: by
3 posts views Thread by Spoon | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.