Nп/п : 28 из 100
 От   : Lew Pitcher                         2:5075/128        21 авг 23 12:28:13
 К    : wij                                                   21 авг 23 15:30:02
 Тема : Re: Question about hsearch(3) ENTER method
----------------------------------------------------------------------------------
                                                                                 
@MSGID: 1@dont-email.me> 740e954d
@REPLY:
<40b905c7-54ae-4e9f-bd6c-990f8e0e804an@googlegroups.com> 755f22c9
@REPLYADDR Lew Pitcher
<lew.pitcher@digitalfreehold.ca>
@REPLYTO 2:5075/128 Lew Pitcher
@CHRS: CP866 2
@RFC: 1 0
@RFC-Message-ID: 1@dont-email.me>
@RFC-References:
<40b905c7-54ae-4e9f-bd6c-990f8e0e804an@googlegroups.com>
@TZUTC: -0000
@PID: Pan/0.139 (Sexual Chocolate; GIT bf56508
git://git.gnome.org/pan2)
@TID: FIDOGATE-5.12-ge4e8b94
On Sun, 20 Aug 2023 23:05:31 -0700, wij wrote:

> 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)?

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"
--- Pan/0.139 (Sexual Chocolate; GIT bf56508 git://git.gnome.org/pan2)
 * Origin: The Pitcher Digital Freehold (2:5075/128)
SEEN-BY: 5001/100 5005/49 5010/352 5015/255 5019/40
5020/715 848 1042 4441
SEEN-BY: 5020/12000 5030/49 1081 5075/128
@PATH: 5075/128 5020/1042 4441



   GoldED+ VK   │                                                 │   09:55:30    
                                                                                
В этой области больше нет сообщений.

Остаться здесь
Перейти к списку сообщений
Перейти к списку эх