[asterisk-dev] [Code Review]: RFC for proposed astobj2 API container enhancements
rmudgett
reviewboard at asterisk.org
Thu Mar 29 18:07:47 CDT 2012
> On March 29, 2012, 9:50 a.m., Mark Michelson wrote:
> > /trunk/include/asterisk/astobj2.h, lines 938-946
> > <https://reviewboard.asterisk.org/r/1835/diff/1/?file=26924#file26924line938>
> >
> > How do you plan to detect a duplicate object?
>
> rmudgett wrote:
> Duplicate objects are easily detectable in sorted containers. Unsorted containers must be searched. I will note that the duplicate option is only applicable to sorted containers.
>
> Mark Michelson wrote:
> To clarify my question more, how do you differentiate between duplicate objects and objects with duplicate keys?
>
> I suppose that my definition of what a duplicate key is may be different from yours. For instance, if you have an object type foo, and it has a field bar within it like:
>
> struct foo {
> char *bar;
> };
>
> Then it is reasonable to assume that we might compare foos by using the comparison function:
>
> int cmp_foo(void *obj, void *arg, int flags)
> {
> struct foo *foo1 = obj;
> struct foo *foo2 = arg;
> return !strcmp(foo1->bar, foo2->bar) ? CMP_MATCH : 0;
> }
>
> Similarly, we may choose to hash foos using the hash function
>
> int hash_foo(const void *obj, int flags)
> {
> struct foo *foo = obj;
> return ast_str_hash(foo->bar);
> }
>
> Now, how do we determine if two foos are duplicates? Is it because cmp_foo() returns CMP_MATCH? Doesn't that just mean the objects have duplicate keys? Or would two foos be considered to have duplicate keys if hash_foo() returns the same value for two foos? If the latter, then what does it mean for an object to have duplicate keys if it is not in a hash container?
>
> rmudgett wrote:
> Your elaboration further points out reasons why the container must be provided a sort_fn to use the duplicate options. Otherwise there is no way to tell if an object is a duplicate except if the object pointer is equal.
>
> Mark Michelson wrote:
> If I've distilled what you've just said correctly, then sort_fn() is what is used to determine whether an item is a duplicate object, but the cmp_fn() is what is used to determine whether two objects have duplicate keys. So for some objects, it's possible these definitions could overlap, but for others, it's not quite the case.
The cmp_fn is used to select objects from the container. They either match the selection criteria or they don't (CMP_MATCH returned). The cmp_fn also can abort a container traversal (CMP_STOP returned).
The sort_fn is used to sort objects in the container. An object is equal/duplicate when the sort_fn returns zero.
The hash_fn is a specialized function for hash containers to determine what bucket to look in for an object.
If an ao2_callback has an OBJ_KEY or OBJ_POINTER flag then the hash_fn and sort_fn are used to locate an object in the container. The cmp_fn then is used to determine if any located objects are to be selected.
All three functions must handle the OBJ_KEY and OBJ_POINTER flags if they are used because all three are given the ao2_callback arg pointer as the object/key.
- rmudgett
-----------------------------------------------------------
This is an automatically generated e-mail. To reply, visit:
https://reviewboard.asterisk.org/r/1835/#review5906
-----------------------------------------------------------
On March 29, 2012, 5:06 p.m., rmudgett wrote:
>
> -----------------------------------------------------------
> This is an automatically generated e-mail. To reply, visit:
> https://reviewboard.asterisk.org/r/1835/
> -----------------------------------------------------------
>
> (Updated March 29, 2012, 5:06 p.m.)
>
>
> Review request for Asterisk Developers, Mark Michelson and Matt Jordan.
>
>
> Summary
> -------
>
> RFC to add proposed API enhancements for containers.
>
> API allows for sorted containers, insertion options, duplicate handling options, and traversal order options.
>
> Also has several documentation corrections.
>
>
> Diffs
> -----
>
> /trunk/include/asterisk/astobj2.h 360828
>
> Diff: https://reviewboard.asterisk.org/r/1835/diff
>
>
> Testing
> -------
>
> It compiles but doesn't link. This is a RFC. :)
>
>
> Thanks,
>
> rmudgett
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-dev/attachments/20120329/017961a6/attachment-0001.htm>
More information about the asterisk-dev
mailing list