Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: NamedIndex and NamedVector types #1190

Closed
wants to merge 17 commits into from
Closed

Conversation

HarlanH
Copy link
Contributor

@HarlanH HarlanH commented Aug 20, 2012

These are relatively simple data types that are useful for data structures that are worked with in the REPL. NamedIndex is a mapping from either names (strings) or integer indexes to integer indexes. NamedVector wraps that in a type that also includes an arbitrary vector. These structures also include grouped names, which refer to groups of other names, and can be used transparently. The majority of this code is by Tom Short @tshort. NamedIndex is used by JuliaData.

NamedVector is missing most functionality but the basics, but thought I'd get comments before writing that code.

Simple example of NamedVector (see also the test suite):

julia> nams = ["one", "two", "three", "four"]
4-element ASCIIString Array:
 "one"  
 "two"  
 "three"
 "four" 

julia> vals = [11, 22, 33, 44]
4-element Int64 Array:
 11
 22
 33
 44

julia> t1 = NamedVector() # need constructors other than the default empty constructor!


julia> for i = 1:4
           t1[nams[i]] = vals[i]
       end

julia> print(t1)
1, one: 11
2, two: 22
3, three: 33
4, four: 44

julia> t1["one"]
11

julia> t1[2]
22

julia> t1[1:3]
3-element Any Array:
 11
 22
 33

julia> t1[["four", "two"]]
2-element Any Array:
 44
 22

initial commit, including simple test suite:

```julia
load("named.jl")
load("test.jl")
tests("test/test_named.jl")
```
@StefanKarpinski
Copy link
Member

Love it.

@StefanKarpinski
Copy link
Member

I'm completely down to merge this, but I'm holding off because of the RFC subject line. Whenever you're good to go, Harlan, this is in. I've always wanted this kind of thing. Very handy.

@toivoh
Copy link
Contributor

toivoh commented Aug 20, 2012

Where does the mapping from name to index come from? Insertion order?

@HarlanH
Copy link
Contributor Author

HarlanH commented Aug 20, 2012

@toivoh , yes, mostly. Typical use cases are setting all the names at once, replacing a name, or appending a new index. For the applications we've used this for (DataFrame and a project I'm doing for work), inserting in the middle hasn't come up.

@timholy
Copy link
Member

timholy commented Aug 20, 2012

Looks nice. I presume the main extra functionality this adds over Dict is the ability to access elements either by index or name?

Very minor suggestion: you can avoid two Dict lookups, the first for checking to see if it has a name and the second to find the index associated with it, using code like this (cribbed from the options code):

index = Base.ht_keyindex(o.key2index,s)
    if index > 0
...

@HarlanH
Copy link
Contributor Author

HarlanH commented Aug 20, 2012

@timholy Yes, that's right. It has fast access by name (via a Dict) and even faster access by index. It just passes integers (and Ranges, and Boolean vectors...) through to the usual array referencing code. Thanks for the reminder on ht_keyindex()! I'll take a look and see where that can be used.

@HarlanH
Copy link
Contributor Author

HarlanH commented Sep 4, 2012

Jeff/Tom, I pushed some changes here that pull the implementation of groups out into a separate Dict, as suggested. Seems to work fine. I want to do one more thing on this before it could be merged in, which is make groups work for NamedVectors.

values() gives the underlying array values
find() gives the position of the value in the array, or 0
@StefanKarpinski
Copy link
Member

This is why we definitely need multiple inheritance. Huge language change though.

@JeffBezanson
Copy link
Member

I don't think something can be both a Vector and Associative, the way we've defined them so far. A vector implicitly has consecutive integers as keys, and an Associative has arbitrary keys. Iterating an Associative yields (key,value) tuples, and a vector just gives values.

@HarlanH
Copy link
Contributor Author

HarlanH commented Sep 12, 2012

Yes, that's right. My intention for this data type (or, really, Tom's
intention), is a vector that has subsidiary and less-efficient associative
elements for (mostly) interactive use. Iterating should give values, not
tuples.

I've been slammed and haven't been able to work on this stuff recently.
Hopefully soon!

On Wed, Sep 12, 2012 at 3:13 PM, Jeff Bezanson [email protected]:

I don't think something can be both a Vector and Associative, the way
we've defined them so far. A vector implicitly has consecutive integers as
keys, and an Associative has arbitrary keys. Iterating an Associative
yields (key,value) tuples, and a vector just gives values.


Reply to this email directly or view it on GitHubhttps://github.com//pull/1190#issuecomment-8506417.

@tshort
Copy link
Contributor

tshort commented Sep 12, 2012

It can still inherit from an AbstractVector and have iteration defined like an Associative type. I'm not sure whether Vector or Associative iteration is best in this case. The main reason for inheriting from an AbstractVector is so that sum(nv) works and nv1 .* nv2 works. I'd probably lean towards vector-style iteration in this case to make such operations more likely to work without defining a custom method.

One option to make these two types of iterations more compatible is to switch the (k,v) order around to (v,k) on Associative iteration. Then, if you leave off the key, you still get the value, and it would be like vector indexing.

so "sum(x::NamedVector)" now works.
all tests pass.
needed to add a promote_rule(ByteString,ASCIIString) -- should that go elsewhere?
added similar(NamedVector), which is weird, but seems to work.
some duplicated code to deal with warnings -- is there a better way?
repl_show() just calls show().
@HarlanH
Copy link
Contributor Author

HarlanH commented Sep 21, 2012

OK, could use some more feedback. See the comments on the most recent commit. Added groups for NamedVector, changed NamedVector to inherit from AbstractVector, and added more tests.

@HarlanH
Copy link
Contributor Author

HarlanH commented Nov 14, 2012

Will make this a package instead. Closing pull request.

@HarlanH HarlanH closed this Nov 14, 2012
KristofferC added a commit that referenced this pull request Feb 25, 2025
)

Stdlib: LinearAlgebra
URL: https://github.com/JuliaLang/LinearAlgebra.jl.git
Stdlib branch: master
Julia branch: master
Old commit: e7da19f
New commit: f781708
Julia version: 1.13.0-DEV
LinearAlgebra version: 1.12.0(Does not match)
Bump invoked by: @KristofferC
Powered by:
[BumpStdlibs.jl](https://github.com/JuliaLang/BumpStdlibs.jl)

Diff:
JuliaLang/LinearAlgebra.jl@e7da19f...f781708

```
$ git log --oneline e7da19f..f781708
f781708 use the custom sysimage when running documentation (and doctests) as well (#1226)
8fdbfd5 Add `getindex` for `SymTridiagonal` using a `BandIndex` (#1223)
91b8845 Use `BandIndex` directly in `diagzero` call in `getindex` (#1222)
ef7ef3a Restrict triangular type aliases to `AbstractMatrix`es (#1220)
af7a9ac Use `BandIndex` directly in `diagzero` call in `getindex` for banded matrices
579b5f7 Specialize Diagonal * Adjoint (#1207)
5cf41c4 Indirection in matrix multiplication to avoid ambiguities (#1210)
0a9c164 Remove specialized `issymmetric`/`ishermitian` for `Diagonal{<:Number}` (#1213)
ff5648a Make unitary matrices in `svd`/`eigen` of `Diagonal` be unitless (#1155)
e096a03 Don't mutate arrays in symmetric trig functions (#1206)
c234bed Loop over `diagind` in `diag` for banded matrices (#1211)
57785c7 More resizing for truncating return values from LAPACK (#1190)
b464203 Materialize adjoint in mul with `HermOrSym` (#1191)
16d9d61 Restrict Diagonal sqrt branch to positive diag (#1203)
baa48b7 Verbose `showerror` for `SingularException` (#1204)
e0b59a7 Remove `LinearAlgebra` qualifications in `cholesky.jl` (#1209)
95d009b Remove `LinearAlgebra` qualifications in `cholesky.jl`
c550974 change to pivot
ed35a37 Add fast path in generic matmul (#1202)
8c7fe68 Detailed `showerror` for `SingularException`
2a1696a Explicitly declare type constructor imports (#1196)
101f766 Added note to BLAS.[g|s]et_num_threads about Apple Accelerate not supporting it  (#1195)
5aca26f Simplify `getproperty` for `Cholesky*` (#1197)
924dda4 remove copy-allocation on accessing `cholesky` factors (`.L`, `.U`) (#1186)
6f02532 Use `BLAS.trsm!` instead of `LAPACK.trtrs!` in left-triangular solves (#1194)
```

Co-authored-by: KristofferC <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants