Project

General

Profile

Actions

Feature #1962

closed

Name.getSubName: negative index

Added by Anonymous over 9 years ago. Updated almost 9 years ago.

Status:
Closed
Priority:
Normal
Assignee:
Category:
Base
Target version:
Start date:
Due date:
% Done:

100%

Estimated time:
1.00 h

Description

Name::get already supports a Python-style negative index so that name.get(-3) gets the third component from the back.

getSubName should also support a negative index for the first argument, so that you can say name.getSubName(-3, 2), it returns two components starting third from the back.

Actions #1

Updated by Junxiao Shi over 9 years ago

  • Category set to Base
  • Start date deleted (09/02/2014)

Currently, ndn::Name can represent either an absolute Name, or a relative Name, and there is no distinction between these two.
This may lead to incorrect usage.

We should first make a decision on whether ndn::Name should be able to represent a relative Name.

If yes,

  • add an attribute to indicate whether the Name is relative, and remember how many level should a relative Name go up (ie. how many "../" components are there in the front of the relative URI)
  • support negative index in getSubName, which returns a relative Name
  • allow building an absolute Name from absolute base Name and a relative Name #1840

If no,

  • deprecate getSubName and append(Name) from public API
Actions #2

Updated by Junxiao Shi over 9 years ago

20141022 conference call decides that Name is always absolute.

Name::getSubName and Name::append(Name) APIs are used to translate between routable Names and unroutable Names.

Examples:

Name path("/repo/file1");
Name prefix("/att/alice");

// combine routable Name
Name routable = Name(prefix).path(filename);

// extract unroutable Name
Name path1 = Name(routable).getSubName(prefix.size());

Actions #3

Updated by Junxiao Shi over 9 years ago

  • Status changed from New to Rejected

I'm rejecting this Feature.

Name is always absolute.

The suggested scenario "say name.getSubName(-3, 2) to get two components starting 3 from the back" wants "two components", but that Name isn't absolute.

Actions #4

Updated by Alex Afanasyev over 9 years ago

  • Status changed from Rejected to New

Do not reject this. This is a useful feature, which (at least once) I assumed already exists in API.

Just reword what negative index means: get subname length 2 from the end if the source name.

Actions #5

Updated by Junxiao Shi over 9 years ago

How could an application assure the last N components make a valid absolute Name without first inspecting the length?

Actions #6

Updated by Alex Afanasyev over 9 years ago

App can inspect the length, if it wants/needs. Consider this negative index a convenience method: it's easier and more obvious to write getSubName(-3, 2) instead of name.getSubName(name.size() - 3, 2)

Actions #7

Updated by Junxiao Shi over 9 years ago

Please describe a complete application scenario where a fixed number of components from the back is assured to be a valid absolute Name.

Actions #8

Updated by Junxiao Shi almost 9 years ago

20150612 conference call decides to introduce a PartialName concept.

/** \brief represents a Name
 *  \detail a Name is always absolute
 */
class Name;

/** \brief represents a sequence of Components
 *  \detail a PartialName is not necessarily absolute
 */
typedef Name PartialName;

Name::getSubName should return PartialName, and should support negative index.

Name::append should accept PartialName instead of Name.

Actions #9

Updated by Joao Pereira almost 9 years ago

Basically this issue needs:

  1. Changes in the function Name::getSubName

    1.1 Change the return to PartialName

    1.2 Start accepting negative index

  2. Changes in the function Name::append

    2.1 Change accepted parameter to PartialName

Questions:

  1. What should happen when the negative value if > then the size of the name?
Actions #10

Updated by Junxiao Shi almost 9 years ago

  • Subject changed from Name.getSubName to support a negative index? to Name.getSubName: negative index
  • Description updated (diff)
  • Target version set to v0.4

What should happen when the negative value is greater than the size of the name?

When iStartComponent < 0 && (size() - iStartComponent) < 0, the behavior should be same as if iStartComponent is zero.

Python's subarray operation takes two indices instead of index+count, so it's incomparable.

JavaScript String.prototype.substr treats out-of-range negative index as zero.

> s='abcdefgh'
"abcdefgh"
> s.substr(0,3)
"abc"
> s.substr(1,3)
"bcd"
> s.substr(-7,3)
"bcd"
> s.substr(-8,3)
"abc"
> s.substr(-9,3)
"abc"
> s.substr(-10,3)
"abc"
Actions #11

Updated by Joao Pereira almost 9 years ago

  • Status changed from New to In Progress
  • Assignee set to Joao Pereira
  • Estimated time set to 1.00 h

I'll pick this task up

Actions #12

Updated by Joao Pereira almost 9 years ago

What type should we have in the input parameters? size_t is unsigned int by definition. Shall we try to keep it or shall we change to another type?

Actions #13

Updated by Junxiao Shi almost 9 years ago

Answer to note-12: change the parameter to ssize_t.

Actions #14

Updated by Joao Pereira almost 9 years ago

  • Status changed from In Progress to Code review
  • % Done changed from 0 to 100

Git commit in the following link:

http://gerrit.named-data.net/2125

Actions #15

Updated by Junxiao Shi almost 9 years ago

  • Status changed from Code review to Closed
Actions

Also available in: Atom PDF