Newsgroups: comp.unix.solaris, comp.unix.programmer, comp.lang.c, comp.std.c
From: o...@goanna.cs.rmit.edu.au (Richard A. O'Keefe)
Date: 1996/10/17
Subject: Re: Bug in qsort-function under Solaris 2.4 ?
bar...@tools.bbnplanet.com (Barry Margolin) writes: Why not? Because for efficiency, the table must be *incrementally* >In article <53iel5$es...@goanna.cs.rmit.edu.au>, >Richard A. O'Keefe <o...@goanna.cs.rmit.edu.au> wrote: >>For strcmp() this is probably frivolous, but efficient implementations of >>strspn(), strcspn(), or strpbrk() *do* rely on modifying a global table; >>this turns an O(m*n) operation into an O(m+n) one and is well worth doing. >While I understand why use of a table makes the implementation efficient, >and further that the standard allows this table to be a global variable, I >don't see why it's necessary for the table to be global to achieve the >efficiency. It could be allocated on the stack, or if it's too big for the >stack it could be malloc'ed on entry and freed on exit from those >functions. It's clear that this is slightly less efficient than not having >to allocate a new table each time, it's just a one-time expense for the >call, and still allows the inner loop to be fast. revised, not reinitialised from scratch each time. Here's the trick, which I invented in about 1982 for use in my Emacs-like unsigned char table[Alphabet_Size]; /* 7 or 8 bit alphabet */ void set_up_table(unsigned char const *set, bool include_NUL) { #define in_current_set(c) (table[c] == cycle) size_t strspn(char const *src, char const *set) { assert(set != 0); size_t strcspn(char const *src, char const *set) { assert(set != 0); with the other variations being obvious. It is clear that the for() loops in str{,c}spn() are O(|src|). If a new table were allocated on each call, the cost of this method The neat thing about the present scheme is that it scales to 16-bit I do not know whether any existing commercial implementations of C -- You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||
