Skip to content

Commit 8cf58c2

Browse files
committed
Add keys() API to collections for usage with things like findfirst()
1 parent 6eefb9e commit 8cf58c2

File tree

7 files changed

+17
-7
lines changed

7 files changed

+17
-7
lines changed

src/Abstract/DynamicLink.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Export DynamicLinks API
22
export DynamicLinks,
3-
getindex, length, iterate, lastindex, eltype,
3+
getindex, length, iterate, keys, lastindex, eltype,
44
handle, header
55

66
# Export Dynamic Link API
@@ -12,7 +12,7 @@ export RPath,
1212
handle, rpaths, canonical_rpaths, find_library
1313

1414
# Import iteration protocol
15-
import Base: iterate, length, lastindex
15+
import Base: iterate, keys, length, lastindex
1616

1717
"""
1818
DynamicLinks
@@ -28,6 +28,7 @@ given below, with methods that subclasses must implement marked in emphasis:
2828
- *getindex()*
2929
- *lastindex()*
3030
- iterate()
31+
- keys()
3132
- eltype()
3233
3334
### Misc.
@@ -36,6 +37,7 @@ given below, with methods that subclasses must implement marked in emphasis:
3637
abstract type DynamicLinks{H <: ObjectHandle} end
3738

3839
@mustimplement lastindex(dls::DynamicLinks)
40+
keys(dls::DynamicLinks) = 1:length(dls)
3941
iterate(dls::DynamicLinks, idx=1) = idx > length(dls) ? nothing : (dls[idx], idx+1)
4042
length(dls::DynamicLinks) = lastindex(dls)
4143
eltype(::Type{D}) where {D <: DynamicLinks} = DynamicLink
@@ -107,6 +109,7 @@ Return the list of paths that will be searched for shared libraries.
107109
@mustimplement rpaths(rpath::RPath)
108110

109111
lastindex(rpath::RPath) = lastindex(rpaths(rpath))
112+
keys(rpath::RPath) = 1:length(rpath)
110113
iterate(rpaht::RPath, idx=1) = idx > length(rpath) ? nothing : (rpath[idx], idx+1)
111114
length(rpath::RPath) = lastindex(rpath)
112115
eltype(::Type{D}) where {D <: RPath} = String

src/Abstract/Section.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Export Sections API
22
export Sections,
3-
getindex, length, lastindex, iterate, eltype,
3+
getindex, length, lastindex, iterate, keys, eltype,
44
findall, findfirst,
55
handle, header, format_string
66

@@ -15,7 +15,7 @@ export SectionRef,
1515

1616
# Import Base methods for extension
1717
import Base: read, seek, seekstart, eof, length, eltype, findall,
18-
findfirst, iterate, lastindex
18+
findfirst, iterate, keys, lastindex
1919

2020
"""
2121
Sections
@@ -35,6 +35,7 @@ in emphasis:
3535
- *lastindex()*
3636
- length()
3737
- iterate()
38+
- keys()
3839
- eltype()
3940
4041
### Search
@@ -49,6 +50,7 @@ abstract type Sections{H<:ObjectHandle} end
4950
# Fairly simple iteration interface specification
5051
@mustimplement lastindex(sections::Sections)
5152
length(sections::Sections) = lastindex(sections)
53+
keys(sections::Sections) = 1:length(sections)
5254
iterate(sections::Sections, idx=1) = idx > length(sections) ? nothing : (sections[idx], idx+1)
5355
eltype(::Type{S}) where {S <: Sections} = SectionRef
5456

src/Abstract/Segment.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export Segment, deref, segment_name, segment_offset, segment_file_size,
88
export SegmentRef, segment_number
99

1010
# Import Base stuff for extension
11-
import Base: getindex, length, eltype, findfirst, findall, iterate, lastindex
11+
import Base: getindex, length, eltype, findfirst, findall, iterate, keys, lastindex
1212

1313
"""
1414
Segments
@@ -28,6 +28,7 @@ in emphasis:
2828
- *lastindex()*
2929
- length()
3030
- iterate()
31+
- keys()
3132
- eltype()
3233
3334
### Search
@@ -42,6 +43,7 @@ abstract type Segments{H<:ObjectHandle} end
4243
# Fairly simple iteration interface specification
4344
@mustimplement lastindex(segs::Segments)
4445
length(segs::Segments) = lastindex(segs)
46+
keys(segs::Segments) = 1:length(segs)
4547
iterate(segs::Segments, idx=1) = idx > length(segs) ? nothing : (segs[idx], idx+1)
4648
eltype(::Type{S}) where {S <: Segments} = SegmentRef
4749

src/COFF/COFF.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ using StructIO
66
using ObjectFile
77
import ObjectFile: DynamicLink, DynamicLinks, RPath, ObjectHandle, Section, Sections, SectionRef,
88
Segment, Segments, SegmentRef, StrTab, Symbols, SymtabEntry, SymbolRef,
9-
getindex, length, lastindex, iterate, eltype, handle, header, path,
9+
getindex, length, lastindex, iterate, keys, eltype, handle, header, path,
1010
rpaths, canonical_rpaths, find_library, readmeta, seek, seekstart, skip,
1111
iostream, position, read, readuntil, eof, endianness, is64bit, isrelocatable,
1212
isexecutable, islibrary, isdynamic, mangle_section_name, mangle_symbol_name,

src/ELF/ELFRelocation.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ function getindex{T}(s::Relocations{T},n)
5353
RelocationRef{T}(s.sec.handle,unpack(s.sec.handle, T))
5454
end
5555

56+
keys(s::Relocations) = 1:length(s)
5657
iterate(s::Relocations, idx=1) = idx > length(s) ? nothing : (s[idx], idx+1)
5758

5859
# Utilities for extracting information from relocation entries

src/MachO/MachO.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ using StructIO
66
using ObjectFile
77
import ObjectFile: DynamicLink, DynamicLinks, RPath, ObjectHandle, Section, Sections, SectionRef,
88
Segment, Segments, SegmentRef, StrTab, Symbols, SymtabEntry, SymbolRef,
9-
getindex, length, iterate, lastindex, eltype, handle, header, path,
9+
getindex, length, iterate, keys, lastindex, eltype, handle, header, path,
1010
rpaths, canonical_rpaths, find_library, readmeta, seek, seekstart, skip,
1111
iostream, position, read, readuntil, eof, endianness, is64bit, isrelocatable,
1212
isexecutable, islibrary, isdynamic, mangle_section_name, mangle_symbol_name,

src/MachO/MachOLoadCmd.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Allows iteration over the LoadCmds within a Mach-O file.
5454
- getindex()
5555
- lastindex()
5656
- iterate()
57+
- keys()
5758
- length()
5859
- eltype()
5960
@@ -144,6 +145,7 @@ handle(lcs::MachOLoadCmds) = lcs.handle
144145
header(lcs::MachOLoadCmds) = header(handle(lcs))
145146

146147
# Iteration
148+
keys(lcs::MachOLoadCmds) = 1:length(lcs)
147149
iterate(lcs::MachOLoadCmds, idx=1) = idx > length(lcs) ? nothing : (lcs.cmds[idx], idx+1)
148150
lastindex(lcs::MachOLoadCmds) = lastindex(lcs.cmds)
149151
length(lcs::MachOLoadCmds) = length(lcs.cmds)

0 commit comments

Comments
 (0)