all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Hash-table elements
@ 2009-09-29  9:52 Nordlöw
  2009-09-29 13:17 ` Pascal J. Bourguignon
  0 siblings, 1 reply; 4+ messages in thread
From: Nordlöw @ 2009-09-29  9:52 UTC (permalink / raw
  To: help-gnu-emacs

Can hash-tables contain references other "global" structures?

I have a hash-table that maps filenames to their metadata,file-scan-
hits/misses.
I don't want to pull-change-push the whole meta-data value whenever I
change parts of these metadata/hits.

Is this possible somehow?

/Nordlöw


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Hash-table elements
  2009-09-29  9:52 Hash-table elements Nordlöw
@ 2009-09-29 13:17 ` Pascal J. Bourguignon
  2009-10-01 14:52   ` Nordlöw
  0 siblings, 1 reply; 4+ messages in thread
From: Pascal J. Bourguignon @ 2009-09-29 13:17 UTC (permalink / raw
  To: help-gnu-emacs

Nordlöw <per.nordlow@gmail.com> writes:

> Can hash-tables contain references other "global" structures?
>
> I have a hash-table that maps filenames to their metadata,file-scan-
> hits/misses.
> I don't want to pull-change-push the whole meta-data value whenever I
> change parts of these metadata/hits.
>
> Is this possible somehow?

Yes.  Only do not modify the keys!  You wouldn't be able to retrieve
them, since changing the state of a key would probably change its hash
value, and therefore the bucket where the hash-table stored it.
But you can do whatever you want on the value.

(require 'cl)
(let ((h (make-hash-table)))

   (let ((value (list 1 2 3))
         (key   :my-list))

      (setf (gethash key h) value)
      (setf (car value) 0
            (cdr value) 'z)
      (gethash key h)))
--> (0 . z)

     
-- 
__Pascal Bourguignon__


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Hash-table elements
  2009-09-29 13:17 ` Pascal J. Bourguignon
@ 2009-10-01 14:52   ` Nordlöw
  2009-10-01 15:18     ` Pascal J. Bourguignon
  0 siblings, 1 reply; 4+ messages in thread
From: Nordlöw @ 2009-10-01 14:52 UTC (permalink / raw
  To: help-gnu-emacs

On Sep 29, 3:17 pm, p...@informatimago.com (Pascal J. Bourguignon)
wrote:
> Nordlöw <per.nord...@gmail.com> writes:
> > Can hash-tables contain references other "global" structures?
>
> > I have a hash-table that maps filenames to their metadata,file-scan-
> > hits/misses.
> > I don't want to pull-change-push the whole meta-data value whenever I
> > change parts of these metadata/hits.
>
> > Is this possible somehow?
>
> Yes.  Only do not modify the keys!  You wouldn't be able to retrieve
> them, since changing the state of a key would probably change its hash
> value, and therefore the bucket where the hash-table stored it.
> But you can do whatever you want on the value.
>
> (require 'cl)
> (let ((h (make-hash-table)))
>
>    (let ((value (list 1 2 3))
>          (key   :my-list))
>
>       (setf (gethash key h) value)
>       (setf (car value) 0
>             (cdr value) 'z)
>       (gethash key h)))
> --> (0 . z)
>
> --
> __Pascal Bourguignon__

So with the use of setf() we realize the reference pattern in Emacs-
Lisp as we use pointers in C?

So the value of (gethash key h) is actually a reference to "value" and
when "value" goes out of scope the interpreter knows that it is
referenced from another structure and does not delete its contents?

I have understood it correctly?

/Nordlöw


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Hash-table elements
  2009-10-01 14:52   ` Nordlöw
@ 2009-10-01 15:18     ` Pascal J. Bourguignon
  0 siblings, 0 replies; 4+ messages in thread
From: Pascal J. Bourguignon @ 2009-10-01 15:18 UTC (permalink / raw
  To: help-gnu-emacs

Nordlöw <per.nordlow@gmail.com> writes:

> On Sep 29, 3:17 pm, p...@informatimago.com (Pascal J. Bourguignon)
> wrote:
>> Nordlöw <per.nord...@gmail.com> writes:
>> > Can hash-tables contain references other "global" structures?
>>
>> > I have a hash-table that maps filenames to their metadata,file-scan-
>> > hits/misses.
>> > I don't want to pull-change-push the whole meta-data value whenever I
>> > change parts of these metadata/hits.
>>
>> > Is this possible somehow?
>>
>> Yes.  Only do not modify the keys!  You wouldn't be able to retrieve
>> them, since changing the state of a key would probably change its hash
>> value, and therefore the bucket where the hash-table stored it.
>> But you can do whatever you want on the value.
>>
>> (require 'cl)
>> (let ((h (make-hash-table)))
>>
>>    (let ((value (list 1 2 3))
>>          (key   :my-list))
>>
>>       (setf (gethash key h) value)
>>       (setf (car value) 0
>>             (cdr value) 'z)
>>       (gethash key h)))
>> --> (0 . z)
>>
>> --
>> __Pascal Bourguignon__
>
> So with the use of setf() we realize the reference pattern in Emacs-
> Lisp as we use pointers in C?
>
> So the value of (gethash key h) is actually a reference to "value" and
> when "value" goes out of scope the interpreter knows that it is
> referenced from another structure and does not delete its contents?
>
> I have understood it correctly?


Yes.  

However, some data types are immutable.  (Out of blue, I can think
only of numbers in emacs lisp that are immutable).  For these
immutable data types, since you cannot modify them, the system could
make copy of them, instead of using references.  


-- 
__Pascal Bourguignon__


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2009-10-01 15:18 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-09-29  9:52 Hash-table elements Nordlöw
2009-09-29 13:17 ` Pascal J. Bourguignon
2009-10-01 14:52   ` Nordlöw
2009-10-01 15:18     ` Pascal J. Bourguignon

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.