Reply to topic  [ 2 posts ] 
delete object member 
Author Message
Yorick Guru

Joined: Wed Nov 24, 2004 12:51 pm
Posts: 97
Location: Observatoire de Lyon (France)
Post delete object member
Is there a way to delete a member of a group object? I would like that the name of the member be no longer part of the list returned by `obj(*,)`. Perhaps this is not possible because of the ubiquity between indexing by name and by integer index.

For the moment I use a special object to mark deleted members and use the following routine (I hope the logic of this is pretty clear from the code):
Code:
// Define a unique value to mark nonexistent members (avoiding redefinition):
NONE = (is_pointer(NONE) ? NONE : &[1]);

func is_none(arg) {
  return (arg == NONE);
}

func pop_member(obj, name) {
   if (is_obj(obj, noop(name), 1n) < 0n) return NONE;
   local value;
   eq_nocopy, value, obj(noop(name)); // avoid a copy for arrays
   save, obj, noop(name), NONE;
   return value;
}

func delete_member(obj, name) {
   if (is_obj(obj, noop(name), 1n) >= 0n) save, obj, noop(name), NONE;
}

func has_member(obj, name) {
   return (is_obj(obj, noop(name), 1n) >= 0n);
}

func get_member(obj, name) {
   return (is_obj(obj, noop(name), 1n) < 0n ? NONE : obj(noop(name)));
}

func get_member_names(obj) {
   names = obj(*,);
   n = numberof(names);
   if (n > 0) {
      flags = array(int, n);
      for (k = 1; k <= n; ++k) {
          flags(k) = (is_obj(obj, names(k), 1n) >= 0n);
      }
      return names(where(flags));
   }
}


Fri Jun 05, 2015 1:34 am
Profile WWW
Yorick Master

Joined: Mon Nov 22, 2004 9:43 am
Posts: 354
Location: Livermore, CA, USA
Post Re: delete object member
There is no way to delete members. However, it is cheap to copy oxy objects, so you can write your own delete function which simply copies the object omitting the members you don't want. Beware that even though the copy is fairly cheap, it is obviously proportional to the number of items, so don't attempt to do vast numbers of copies. Since items can be overwritten by nil-values, it isn't obvious to me why you would be desperate to actually remove them. You can write a function to return only the names of non-nil items if you can't be bothered to test in your presumed loop over item names. Something like:

Code:
func non_nil_names(obj)
{
  n = obj(*);
  if (!n) return [];
  names = obj(*,);
  for (i=1 ; i<=n ; ++i) if (is_void(obj(noop(i)))) names(i) = string(0);
  return names(where(names));
}


Sat Jul 25, 2015 12:52 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 2 posts ] 

Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware for PTF.