Discussion:
Question about hsearch(3) ENTER method
(too old to reply)
wij
2023-08-21 06:05:31 UTC
Permalink
manpage of hsearch(3) says:
...
The argument action determines what hsearch() does after an unsuccess‐
ful search. This argument must either have the value ENTER, meaning
insert a copy of item (and return a pointer to the new hash table entry
as the function result), or the value FIND, meaning that NULL should be
returned. (If action is FIND, then data is ignored.)
...

When I want to insert an item into the hash table, I invokes hsearch(item,ENTER).
But I only want to insert the item when it is not in the hash table. How can I
do (Only invokes hsearch once. The manpage seems not mentioning the condition I
am looking for)?
Kenny McCormack
2023-08-21 10:50:09 UTC
Permalink
Post by wij
...
The argument action determines what hsearch() does after an unsuccess
ful search. This argument must either have the value ENTER, meaning
insert a copy of item (and return a pointer to the new hash table entry
as the function result), or the value FIND, meaning that NULL should be
returned. (If action is FIND, then data is ignored.)
...
When I want to insert an item into the hash table, I invokes
hsearch(item,ENTER). But I only want to insert the item when it is not
in the hash table. How can I do (Only invokes hsearch once. The manpage
seems not mentioning the condition I am looking for)?
I had never heard of these functions before reading your post. They look
interesting, so thanks for that.

Re: your question. It looks to me that it will "just work". I.e., if the
entry is already there, FIND & ENTER do the same thing.

P.S. The main thing I think is missing from these functions is a "delete"
capability. Oh, and an "edit" capability.
--
Just like Donald Trump today, Jesus Christ had a Messiah complex.

And, in fact, the similarities between the two figures are quite striking.
For example, both have a ragtag band of followers, whose faith cannot be shaken.
Lew Pitcher
2023-08-21 12:28:13 UTC
Permalink
Post by wij
...
The argument action determines what hsearch() does after an unsuccess‐
ful search. This argument must either have the value ENTER, meaning
insert a copy of item (and return a pointer to the new hash table entry
as the function result), or the value FIND, meaning that NULL should be
returned. (If action is FIND, then data is ignored.)
...
When I want to insert an item into the hash table, I invokes hsearch(item,ENTER).
But I only want to insert the item when it is not in the hash table. How can I
do (Only invokes hsearch once. The manpage seems not mentioning the condition I
am looking for)?
The version of hsearch() (and it's manpage) that I have available to me says:
"The hsearch() function searches the hash table for an item with the
same key as item (where "the same" is determined using strcmp(3)), and
if successful returns a pointer to it.
...
The field key points to a null-terminated string which is the search
key. The field data points to data that is associated with that key.

The argument action determines what hsearch() does after an unsuccess-
ful search. This argument must either have the value ENTER, meaning
insert a copy of item (and return a pointer to the new hash table entry
as the function result), or the value FIND, meaning that NULL should be
returned. (If action is FIND, then data is ignored.)"

It seems pretty clear: hsearch() will either
a) find the matching entry in the hash table, and return a pointer to the
struct entry that defines the key and data of the matching entry, or
b) not find the matching entry, and return NULL (when ACTION is set to FIND), or
c) not find the matching entry, insert the supplied struct entry into the
hash table, and return a pointer to it (when ACTION is set to ENTER)

I believe that you are looking for the (c) behaviour; set ACTION to ENTER
when you "want to insert an item into the hash table", and to FIND when you
want to retrieve it.

HTH
--
Lew Pitcher
"In Skills We Trust"
wij
2023-08-21 13:30:04 UTC
Permalink
...
The argument action determines what hsearch() does after an unsuccess‐
ful search. This argument must either have the value ENTER, meaning
insert a copy of item (and return a pointer to the new hash table entry
as the function result), or the value FIND, meaning that NULL should be
returned. (If action is FIND, then data is ignored.)
...
When I want to insert an item into the hash table, I invokes hsearch(item,ENTER).
But I only want to insert the item when it is not in the hash table. How can I
do (Only invokes hsearch once. The manpage seems not mentioning the condition I
am looking for)?
"The hsearch() function searches the hash table for an item with the
same key as item (where "the same" is determined using strcmp(3)), and
if successful returns a pointer to it.
...
The field key points to a null-terminated string which is the search
key. The field data points to data that is associated with that key.
The argument action determines what hsearch() does after an unsuccess-
ful search. This argument must either have the value ENTER, meaning
insert a copy of item (and return a pointer to the new hash table entry
as the function result), or the value FIND, meaning that NULL should be
returned. (If action is FIND, then data is ignored.)"
It seems pretty clear: hsearch() will either
a) find the matching entry in the hash table, and return a pointer to the
struct entry that defines the key and data of the matching entry, or
b) not find the matching entry, and return NULL (when ACTION is set to FIND), or
c) not find the matching entry, insert the supplied struct entry into the
hash table, and return a pointer to it (when ACTION is set to ENTER)
I believe that you are looking for the (c) behaviour; set ACTION to ENTER
when you "want to insert an item into the hash table", and to FIND when you
want to retrieve it.
HTH
--
Lew Pitcher
"In Skills We Trust"
None of the a,b,c option would work.
The usecase is like I am writting some kind of interpreter:

Type a=1;
Type a=2;

When the first "a=1" is encountered, I ENTER "a" into the hash table.
When the second "a=2" is encountered, the ENTER (because the parser saw 'Type')
should fail because it declares the same name of variable twice.
Lew Pitcher
2023-08-21 14:14:05 UTC
Permalink
Post by Lew Pitcher
...
The argument action determines what hsearch() does after an unsuccess‐
ful search. This argument must either have the value ENTER, meaning
insert a copy of item (and return a pointer to the new hash table entry
as the function result), or the value FIND, meaning that NULL should be
returned. (If action is FIND, then data is ignored.)
...
When I want to insert an item into the hash table, I invokes hsearch(item,ENTER).
But I only want to insert the item when it is not in the hash table. How can I
do (Only invokes hsearch once. The manpage seems not mentioning the condition I
am looking for)?
[snip]
Post by Lew Pitcher
It seems pretty clear: hsearch() will either
a) find the matching entry in the hash table, and return a pointer to the
struct entry that defines the key and data of the matching entry, or
b) not find the matching entry, and return NULL (when ACTION is set to FIND), or
c) not find the matching entry, insert the supplied struct entry into the
hash table, and return a pointer to it (when ACTION is set to ENTER)
I believe that you are looking for the (c) behaviour; set ACTION to ENTER
when you "want to insert an item into the hash table", and to FIND when you
want to retrieve it.
None of the a,b,c option would work.
Let me make sure that I understand the issue here
Type a=1;
Type a=2;
When the first "a=1" is encountered, I ENTER "a" into the hash table.
So, at the first call to hsearch(,ENTER), you supply a pointer to a
struct entry {
char *key;
void *data;
} something;
initialized so that something.key points to the name "a", and something.data
points to the data you want to set "a" to (presumably, 1). That hsearch()
function returns a non-NULL value (which should be a pointer to your
struct entry something;) indicating that it saved the given hashtable entry.
When the second "a=2" is encountered, the ENTER (because the parser saw 'Type')
should fail because it declares the same name of variable twice.
Here, you should either first FIND, and decide what to do if FIND is successful,
or ENTER and just re-use the struct entry * that hsearch() returned to you.

I guess the choice depends on what your interpreter is expecting to do:
If
Type a=2;
is supposed to define a new something, and fail if that something already
exists, then you should hsearch(,FIND), and fail if FIND succeeded (or,
alternately, hsearch(,ENTER), and check whether the returned pointer has
a ->data that differs from the value you prepared for the hsearch() call).

OTOH, if
Type a=2;
is permitted to update/replace/overwrite the prior
Type a=1;
then you just hsearch(,ENTER) and update the returned ->data with the
new value for the key "a".

HTH
--
Lew Pitcher
"In Skills We Trust"
wij
2023-08-22 02:11:29 UTC
Permalink
Post by Lew Pitcher
Post by Lew Pitcher
...
The argument action determines what hsearch() does after an unsuccess‐
ful search. This argument must either have the value ENTER, meaning
insert a copy of item (and return a pointer to the new hash table entry
as the function result), or the value FIND, meaning that NULL should be
returned. (If action is FIND, then data is ignored.)
...
When I want to insert an item into the hash table, I invokes hsearch(item,ENTER).
But I only want to insert the item when it is not in the hash table. How can I
do (Only invokes hsearch once. The manpage seems not mentioning the condition I
am looking for)?
[snip]
Post by Lew Pitcher
It seems pretty clear: hsearch() will either
a) find the matching entry in the hash table, and return a pointer to the
struct entry that defines the key and data of the matching entry, or
b) not find the matching entry, and return NULL (when ACTION is set to FIND), or
c) not find the matching entry, insert the supplied struct entry into the
hash table, and return a pointer to it (when ACTION is set to ENTER)
I believe that you are looking for the (c) behaviour; set ACTION to ENTER
when you "want to insert an item into the hash table", and to FIND when you
want to retrieve it.
None of the a,b,c option would work.
Let me make sure that I understand the issue here
Type a=1;
Type a=2;
When the first "a=1" is encountered, I ENTER "a" into the hash table.
So, at the first call to hsearch(,ENTER), you supply a pointer to a
struct entry {
char *key;
void *data;
} something;
initialized so that something.key points to the name "a", and something.data
points to the data you want to set "a" to (presumably, 1). That hsearch()
function returns a non-NULL value (which should be a pointer to your
struct entry something;) indicating that it saved the given hashtable entry.
When the second "a=2" is encountered, the ENTER (because the parser saw 'Type')
should fail because it declares the same name of variable twice.
Here, you should either first FIND, and decide what to do if FIND is successful,
or ENTER and just re-use the struct entry * that hsearch() returned to you.
If
Type a=2;
is supposed to define a new something, and fail if that something already
exists, then you should hsearch(,FIND), and fail if FIND succeeded (or,
alternately, hsearch(,ENTER), and check whether the returned pointer has
a ->data that differs from the value you prepared for the hsearch() call).
OTOH, if
Type a=2;
is permitted to update/replace/overwrite the prior
Type a=1;
then you just hsearch(,ENTER) and update the returned ->data with the
new value for the key "a".
HTH
--
Lew Pitcher
"In Skills We Trust"
class HSearch {
::hsearch_data m_htab;
size_t m_maxe;

public:
//....

template<typename T>
WY__WARNRET__ Errno declare(const char* key, T data) {
static_assert(sizeof(T)<=sizeof(void*));
static char kbuf[256];
if(key==NULL) {
WY_RETURN( EFAULT );
}
if(m_maxe==0) {
WY_RETURN( EBADF);
}
const size_t klen= strlen(key);
if(klen>=sizeof(kbuf)) {
WY_RETURN( ENAMETOOLONG );
}
memcpy(kbuf,key,klen);
kbuf[klen]=0;
::ENTRY item,*retv;
item.key= const_cast<char*>(kbuf);
if(::hsearch_r(item, ENTER,&retv,&m_htab)==0) {
WY_RETURN(errno);
}
if(retv->key!=kbuf) {
WY_RETURN(EEXIST);
}
union {
void * ptr;
T val;
} tt;
tt.val=data;
retv->key= const_cast<char*>(key);
retv->data= tt.ptr;
return Ok;
};

};

I think hsearch can't do what I want (directly). The work-around is copy the
key to a static buffer. The overhead is equ. to a single strcmp, so, fine.
The limit is string length.
I think the implement of declare(..) should comply with the spec. of hsearch
(to be exactly, hsearch_r), though such compliance is not explicitly stated.
How do you think?

Loading...